View Full Version : Tough One: Zoom images smoothly without changing framesize
wuffy77
10-16-2007, 02:29 PM
Ok this might be a tough question, or it may be something that's already out there and you can just provide a link! ;)
I've been trying to find a script or write one myself that will zoom images for me.
The issue I've come across is they resize the image frame
So if you go to this page: http://www.jagwearjewelry.com/JS/QuickZoom.htm
And click the image, you'll see it jumps the image size, but that then knocks everything else out.
So if I have a 500 x 500 pixel box and use the script to zoom it, it'll just make a 1000x1000 box with a stretched image.
Is it possible with javascript to scale the image but keep the frame 500 x 500? (ie make the pixels bigger, but not the frame.
Hope this is clear?!?
wuffy77
10-16-2007, 02:37 PM
much like this flash version - http://www.metapictures.it/
You'll see it doesn't resize the window, it just zooms within a frame
diades
10-16-2007, 03:58 PM
Hi wuffy
place the image in a div set to the size that you want to retain (the original image size?). Using css, set the overflow to hidden:
#mydiv{width:100px;height:100px;overflow:hidden}
<div id="mydiv"><img blah.... /></div>
The div, being set to size with the overflow set to hidden should allow the expansion to, um, dissappear. :)
wuffy77
10-16-2007, 04:07 PM
ah! fantastic idea - that should work a treat, thank Diades.
Just another query - is it possible/do you know of a javascript image zoom that does it smoothly? Ie so it doesn't just jump zoom levels, but rather lets you use a slider or something!?
I'm not sure if it's possible at all?
diades
10-16-2007, 05:39 PM
Hi
Try this out, its ok in IE and Opera, needs work in FF. Will try to look at it to sort it out. If anyone else wants to, go ahead.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<!--
Author: Keith Parker (diades)
Company: Webxpertz
Web Site: www.diades.net
www.webxpertz.net
Comments:
-->
<meta name="description" content="Script to expand an element" />
<meta name="keywords" content="JavaScript,expand,image,element" />
<meta name="author" content="Keith Parker (diades)" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA[
w = 0;
h = 0;
changed = false;
magSize = 0;
currImg = "";
time_Out = 0;
function changeSize(oImg,iMag)
{
var magnification = [2,3,4,5]
if(oImg != undefined) currImg = oImg;
if(iMag != undefined) magSize = magnification[iMag];
if(!changed){
w = currImg.offsetWidth;
h = currImg.offsetHeight;
changed = !changed;
}
if(changed)
{
cw = currImg.offsetWidth;
ch = currImg.offsetHeight;
}
if(currImg.width < (w * magSize))
{
currImg.style.width = (cw + 10) + "px";
currImg.style.left = (currImg.style.pixelLeft - 5) + "px";
currImg.style.height = (ch + 10) + "px";
currImg.style.top = (currImg.style.pixelTop - 5) + "px";
time_Out = window.setTimeout(changeSize,50);
}
else
{
window.clearTimeout(time_Out);
}
}
function resetImage(oImg)
{
if(oImg == currImg)
{
oImg.style.width = w + "px";
oImg.style.top = oImg.style.left = "0";
oImg.style.height = h + "px";
}
}
onload=function()
{
document.getElementById("selMagChange").selectedIndex = -1;
}
//]]>
</script>
<style type="text/css">
/*<![CDATA[*/
#oPicDiv{
position:relative;
width:200px;
height:200px;
margin:auto;
overflow:hidden;
border:3px double #800000;
}
#oPic0{
position:relative;
top:0;
left:0;
width:200px;
height:200px;
border:0;
}
/*]]>*/
</style>
</head>
<body>
<div id="oPicDiv"><img id="oPic0" src="pic.png" alt="test pic" /></div>
<form action="javascript:void(0)">
<fieldset>
<label for="selMagChange">Select magnificattion</label>
<select id="selMagChange" onchange="changeSize(document.getElementById('oPic0'),this.selectedIndex)">
<optgroup label="Select the magnification">
<option>x2</option>
<option>x3</option>
<option>x4</option>
<option>x5</option>
</optgroup>
</select>
<input type="button" value="Reset Image" onclick="resetImage(document.getElementById('oPic0'))"
</fieldset>
</form>
</body>
</html>
coothead
10-16-2007, 07:42 PM
Hi there wuffy77,
...do you know of a javascript image zoom that does it smoothly?
Well, the bald headed old fart found this lying around on the server...
http://mysite.orange.co.uk/azygous/zoom.html
...and this is the dusted off code...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>zoom in, zoom out</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
background-color:#000;
font-family:verdana,arial,helvetica,sans-serif;
font-size:1em;
color:#fff;
}
#pic{
width:64px;
height:48px;
position:absolute;
top:50%;
left:50%;
margin-left:-35px;
margin-top:-27px;
border:3px double #f00;
background-color:#000;
}
#text {
width:720px;
margin:20px auto;
text-align:justify;
border:3px double #fff;
padding:20px;
}
</style>
<script type="text/javascript">
var c=64;
var speed=20;
var inc=8;
var bw=6;
var obj;
var zoom;
var unzoom;
window.onload=function() {
obj=document.getElementById('pic')
obj.onmouseover=function() {
zoooom();
}
obj.onmouseout=function() {
unzoooom();
}
}
function unzoooom() {
clearTimeout(zoom);
if(c<64) {
c=64;
clearTimeout(unzoom);
return;
}
obj.style.marginLeft=-(c+bw)/2+'px';
obj.style.marginTop=-3*(c+bw)/8+'px';
obj.style.width=c+'px';
obj.style.height=3*c/4+'px';
c-=inc;
unzoom=setTimeout('unzoooom()',speed);
}
function zoooom() {
clearTimeout(unzoom);
if(c>760) {
c=760;
clearTimeout(zoom);
return;
}
obj.style.marginLeft=-(c+bw)/2+'px';
obj.style.marginTop=-3*(c+bw)/8+'px';
obj.style.width=c+'px';
obj.style.height=3*c/4+'px';
c+=inc;
zoom=setTimeout('zoooom()',speed);
}
</script>
</head>
<body>
<div>
<img id="pic" src="images/blood.jpg" alt=""/>
</div>
<div id="text">
<p>
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras volutpat, purus ac pellentesque
adipiscing, mauris ligula convallis metus, vitae scelerisque nibh orci quis mi. Cum sociis natoque
penatibus et magnis dis parturient montes, nascetur ridiculus mus. Curabitur porttitor aliquam
libero. Quisque molestie ornare sem. Nam euismod sem lacinia ipsum. In pharetra metus ut dolor
cursus aliquam. Maecenas eu ante quis enim tincidunt laoreet. Pellentesque varius nunc in ipsum
pulvinar sollicitudin. Nunc et mi. Donec auctor dignissim orci. Aliquam sed magna. Pellentesque
in dui. In eget elit. Praesent eu lorem.
</p><p>
Cras cursus varius pede. Cras dolor lorem, convallis sed, venenatis ac, aliquam vitae, orci.
Duis diam massa, adipiscing quis, aliquam eget, ornare eu, lectus. Sed rutrum augue non purus.
Integer vel mauris. Nam suscipit molestie lectus. Fusce laoreet interdum eros. Pellentesque sit
amet enim id nunc adipiscing ultricies. Quisque lobortis eleifend elit. Sed eu augue sed felis
vulputate iaculis. Cras lorem felis, lobortis id, accumsan vel, facilisis quis, dolor. Curabitur
aliquet. Nulla facilisi. Proin nunc velit, posuere sit amet, porttitor et, volutpat a, massa.
Maecenas elementum volutpat justo. Pellentesque magna neque, dictum id, rhoncus a, fringilla et,
nulla. Phasellus placerat gravida purus. Pellentesque odio. Sed volutpat vehicula nulla. Quisque
metus urna, semper eget, aliquam ac, feugiat nec, massa.
</p><p>
Nullam pharetra quam quis metus. Proin feugiat lacinia mauris. Cum sociis natoque penatibus et
magnis dis parturient montes, nascetur ridiculus mus. Praesent faucibus erat quis ante. Suspendisse
eu tellus. Donec sit amet ante in nisl dapibus condimentum. Proin diam. Curabitur egestas felis
iaculis lorem. Aliquam sit amet risus ut nulla sollicitudin scelerisque. Mauris viverra hendrerit
augue. Morbi eu sapien sed enim rutrum blandit. Quisque feugiat. Pellentesque luctus sagittis est.
Donec dolor sem, bibendum ac, porta in, rutrum sit amet, dui. Ut libero turpis, tempus nec, venenatis
at, tincidunt ac, sem.
</p><p>
Cras dictum semper ante. Vivamus fermentum, neque non commodo congue, lacus lectus elementum elit,
vitae placerat nisl nibh vitae tortor. In molestie fermentum tellus. Nunc dolor quam, venenatis vel,
gravida sit amet, imperdiet sit amet, mi. Nulla facilisi. Nunc pede orci, elementum eget, facilisis
id, viverra vel, leo. Duis eu mauris eget felis lobortis iaculis. Etiam elit metus, posuere ut, tempor
eget, commodo eget, leo. Vivamus elementum. Quisque fringilla orci sit amet nulla.
</p><p>
Praesent ut magna ut nibh porttitor vestibulum. Donec ac mauris sit amet elit egestas condimentum. Duis
varius euismod orci. Proin sapien turpis, posuere vitae, tincidunt at, iaculis mattis, magna. Proin
rutrum euismod metus. Maecenas ut ante. Pellentesque tincidunt. Quisque nibh mauris, luctus eu,
elementum in, convallis at, ante. Cras vehicula augue vitae nibh. Suspendisse ut metus. Cras sit amet
neque. Morbi fringilla metus in est. Vivamus eleifend, nunc sit amet faucibus lacinia, ligula massa
fringilla tellus, id pulvinar pede enim posuere sem.
</p>
</div>
</body>
</html>
...is that smoooooth enough for you. :D
wuffy77
10-17-2007, 02:16 AM
that's superb guys - coothead - looks like you're just showing off now! ;)
So would your script work with what diades said above (put it in a div container?) so it wouldn't make it's actual box bigger?
I'm also thinking of combining both of yours - would it be possible to have a 'zoom in' text two the right of the image (or anywhere) and this is what triggers the zoom?
Also can I check your zoom stops at 100%? It certainly stops, but I'm guessing from the image - I want it to stop at the true resolution so it doesn't start pixellating.
coothead
10-17-2007, 07:28 AM
Hi there wuffy77,
have a look at this example...
http://mysite.orange.co.uk/azygous/zoom1.html
...and the code...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>zoom in, zoom out</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
background-image:url(images/bodyBg.jpg);
}
#pic{
width:60px; /*initial image width*/
height:42px; /*initial image width*/
position:absolute;
top:50%;
left:50%;
margin-left:-35px;
margin-top:-27px;
border:3px double #fc9;
background-color:#009;
}
#but {
width:100px;
line-height:25px;
border:3px double #fc9;
background-color:#2e475d;
font-family:verdana,arial,helvetica,sans-serif;
font-size:1em;
color:#fee66a;
text-align:center;
cursor:pointer;
margin:30px 30px 0 0;
float:right;
}
</style>
<script type="text/javascript">
/******************************* these are the editable variables *******************************/
var w=770; /*actual image width*/
var h=535; /*actual image height*/
var c=60; /*this value is equal to the inital image width*/
var rat=h/w /*this value is equal to the height of image divided by the width of image*/
var speed=20; /*this value varies the rate of zoom in conjunction with var exp*/
var exp=8; /*this value varies the rate of zoom in conjunction with var speed*/
var bw=6; /*this value is equal to image border width times two*/
/***********************************************************************************************/
var shr=-exp;
var init=c;
var test=true;
var obj;
var obj1;
window.onload=function() {
obj=document.getElementById('pic');
obj1=document.getElementById('but');
obj1.onclick=function() {
if(test==true) {
obj1.firstChild.nodeValue='zoom out';
test=false;
zoooom(exp);
}
else {
obj1.firstChild.nodeValue='zoom in';
test=true;
zoooom(shr);
}
}
}
function zoooom(inc) {
if(c<init) {
c=init;
test=true;
return;
}
if(c>w) {
c=w;
test=false;
return;
}
obj.style.marginLeft=-(c+bw)/2+'px';
obj.style.marginTop=-rat*(c+bw)/2+'px';
obj.style.width=c+'px';
obj.style.height=rat*c+'px';
c+=inc;
inc1=inc;
setTimeout('zoooom(inc1)',speed);
}
</script>
</head>
<body>
<div>
<img id="pic" src="images/buddha.jpg" alt=""/>
</div>
<div id="but">zoom in</div>
</body>
</html>
As the image is absolutely positioned it will have no effect on the other page contents.
I have set the image to stop expanding when it's actual dimensions are reached.
wuffy77
10-17-2007, 11:04 AM
heheh! That's so clever!
Is it possible to have it only expanding when the button is being pressed (ie stop when I let go?)
Not sure if this is hard or just changing a line from 'onclick' to 'whilstclick' or something!!
coothead
10-17-2007, 04:46 PM
Hi there wuffy77,
You've moved the goalposts again. :supereek:
Are you trying to catch me out. ;)
Here is the mousedown/up version...
http://mysite.orange.co.uk/azygous/zoom2.html
...and here is the modified code...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>zoom in, zoom out version three</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<style type="text/css">
body {
background-image:url(images/bodyBg.jpg);
}
#pic{
width:60px; /*initial image width*/
height:42px; /*initial image width*/
position:absolute;
top:50%;
left:50%;
margin-left:-35px;
margin-top:-27px;
border:3px double #fc9;
background-color:#009;
}
#but {
width:100px;
line-height:25px;
border:3px double #fc9;
background-color:#2e475d;
font-family:verdana,arial,helvetica,sans-serif;
font-size:1em;
color:#fee66a;
text-align:center;
cursor:pointer;
margin:30px 30px 0 0;
float:right;
}
</style>
<script type="text/javascript">
/******************************* these are the editable variables *******************************/
var w=770; /*actual image width*/
var h=535; /*actual image height*/
var c=60; /*this value is equal to the inital image width*/
var rat=h/w /*this value is equal to the height of image divided by the width of image*/
var speed=20; /*this value varies the rate of zoom in conjunction with var exp*/
var exp=8; /*this value varies the rate of zoom in conjunction with var speed*/
var bw=6; /*this value is equal to image border width times two*/
/***********************************************************************************************/
var shr=-exp;
var init=c;
var obj;
var obj1;
window.onload=function() {
obj=document.getElementById('pic');
obj1=document.getElementById('but');
obj1.onmousedown=function() {
obj1.firstChild.nodeValue='zoom out';
zoooom(exp);
}
obj1.onmouseup=function() {
obj1.firstChild.nodeValue='zoom in';
zoooom(shr);
}
}
function zoooom(inc) {
if(c<init) {
c=init;
return;
}
if(c>w) {
c=w;
return;
}
obj.style.marginLeft=-(c+bw)/2+'px';
obj.style.marginTop=-rat*(c+bw)/2+'px';
obj.style.width=c+'px';
obj.style.height=rat*c+'px';
c+=inc;
inc1=inc;
setTimeout('zoooom(inc1)',speed);
}
</script>
</head>
<body>
<div>
<img id="pic" src="images/buddha.jpg" alt="">
</div>
<div id="but">zoom in</div>
</body>
</html>
wuffy77
10-17-2007, 04:52 PM
sorry Coothead, don't mean to make things hard! Thanks for your patience!
Works a treat as usual!
coothead
10-17-2007, 04:57 PM
Hi there wuffy77,
sorry coothead, don't mean to make things hard! Thanks for your patience!
I was only joking. ;)
There was nothing difficult about this, the modifications took all of two minutes. :)
wuffy77
10-17-2007, 04:59 PM
well if you're just twiddling your fingers, then have a look here:
http://www.htmlforums.com/client-side-scripting/t-simple-error-check-96815.html
;) ;) ;)
I've spent all night trying to fix this and I'm just about to cry!
coothead
10-17-2007, 05:14 PM
Hi there wuffy77,
I looked in there earlier and saw that diades had provided an answer.
What is wrong with that?
wuffy77
10-17-2007, 05:33 PM
well I was just wanting to double-check it wasn't fixable from the javascript.
I've actually done it myself the long-winded way following diades' tips - it's the JS lines that were messing up, but I don't understand why?!
Does javascript need to be arranged in a particular shape to work?! Seems to go against most programming principles I know (though very little!)
So if I wrote:
<html><p>Hello</p></html>
This should work the same as:
<html>
<p>Hello</p>
</html>
Or even:
<html><p>Hello
</p></html>
Is javascript not like this? and I guess if not, how do you know where to put spaces/carriage returns!?!
coothead
10-17-2007, 06:52 PM
Hi there wuffy77,
Is javascript not like this? And I guess if not, how do you know where to put spaces/carriage returns!?!
No it is not like that. :disagree:
This will work...
<script type="text/javascript">
alert('hello world');
</script>
...but this will not...
<script type="text/javascript">
alert('hello
world');
</script>
...but would need to be modified like this...
<script type="text/javascript">
alert('hello'+
' world');
</script>
...or notice the difference between...
<script type="text/javascript">
var num=203;
alert(num);
</script>
...and...
<script type="text/javascript">
var num=20
3;
alert(num);
</script>
As for your problem, if you really do not want to 'unscrunch' the code, and I cannot think of a good reason for doing it in the first place, then place a semi-colon here...gth;c++) {anc[c].onmouseover=function() {swapImage(this.href);}}};function swapIm
wuffy77
10-18-2007, 02:05 AM
oh my god! See that's why I wanted to doublecheck!
As I say I got it working, but it involved splitting my code into 70 separate lines of VB script and having to pick through and change every " into "" and so on, then Coothead says you just need one flippin semicolon!
Very interesting though - that all makes sense to me now! Thanks for the explanation!
vBulletin® v3.6.7, Copyright ©2000-2009, Jelsoft Enterprises Ltd.