Click here to Skip to main content
Click here to Skip to main content

Expanding Image Animator

By , 15 Mar 2008
 

Introduction

The article illustrates a JavaScript that will help you to animate your images or any display article like a div from a specified size to another size. This enable you to make a dynamic web page where pictures blow up from a thumbnail view to a full image. Just check out my code.

Background

You need to have a basic knowledge of how to run an application, you need IE, and nothing else. To modify the code, you need to have a strong idea about DOM and JavaScript.

Using the code

Well, using this code, you can show an image in your web page with an animation within it. When using this code, there are two things you need to remember: for each image, you need two img tags, one to show the expanded image (screen + value), and another for the reduced image (thumb + value). Passing a value to the expanded image will display the enlarged image to screen+val. So, you need to use the expanded image on an event of thumb + val. And screen + val when you need to use the smaller image.

Just make your code in such a way that the <img> tags to show the thumbnail and the screen have the same value. Here in the example HTML file, I have used 142 as both the screen and thumb, so that the same image gets displayed.

// Thumbnail expansion and reduction animation
//use expandthumb(142, 500, 449) to increase and 
//reducethumb(142) to decrease the thumbnail
//142 represents the name of the thumbimage.. it should be like thumb142
// for reduceimage 
// and the expanded image id will be screen142 for the value 142 in 
//expandimage 
//500 and 449 are the enlarges size of the image

exid = 0;
exstep = 0;
exwdth = 0;
exht = 0;
extp = 0;
exlft = 0;
extot = 0;
extotst = 15;

function expandthumb(thumbid, fullwidth, fullheight) {
  if (extot != 0) {
    clearTimeout(extot);
  }
  if (exid > 0 && exid != thumbid) {
    restorethumb();
  }
  if (exid != thumbid) {
    img = document.getElementById("screen" + thumbid);
    img.style.display = 'block';
    exid = thumbid;
    exstep = 1;
    exwdth = fullwidth;
    exht = fullheight;
    extp = img.offsetTop;
    exlft = img.offsetLeft;
  } else if (exstep < 1) {
    exstep = 1;
  }
  expandstep();
}

function doexpand() {
  img = document.getElementById("screen" + exid);
  thumb = document.getElementById("thumb" + exid);
  myscroll = getScroll();
  if (extp + thumb.height > myscroll.top + myscroll.height) {
    finaltop = myscroll.top + myscroll.height - exht;
  } else {
    finaltop = extp + thumb.height - exht;
  }
  if (finaltop < myscroll.top) { finaltop = myscroll.top; }
  img.style.top = finaltop + ((extp - finaltop) * 
                              (extotst - exstep) / extotst) + 'px';

  if (exlft + thumb.width > myscroll.left + myscroll.width) {
    finalleft = myscroll.left + myscroll.width - exwdth;
  } else {
    finalleft = exlft + thumb.width - exwdth;
  }
  if (finalleft < myscroll.left) { finalleft = myscroll.left; }
  img.style.left = finalleft + ((exlft - finalleft) * 
                                (extotst - exstep) / extotst) + 'px';

  img.width = thumb.width + ((exwdth - thumb.width) * exstep / extotst);
  img.height = thumb.height + ((exht - thumb.height) * exstep / extotst);
}

function restorethumb() {
  img = document.getElementById("screen" + exid);
  img.style.top = '';
  img.style.left = '';
  img.style.display = 'none';
  exid = 0;
}

function expandstep() {
  extot = 0;
  doexpand();
  if (exstep < extotst) {
    exstep++;
    extot = setTimeout("expandstep()", 20);
  }
}

function reducestep() {
  extot = 0;
  doexpand();
  if (exstep > 0) {
    exstep--;
    extot = setTimeout("reducestep()", 20);
  } else {
    restorethumb();
  }
}

function reducethumb(thumbid) {
  if (extot != 0) {
    clearTimeout(extot);
  }
  if (exstep > 0) {
    reducestep();
  }
}

// returns the scroll position and size of the browser
function getScroll() {
  if (document.all && typeof document.body.scrollTop != "undefined") {  
    // IE model
    var ieBox = document.compatMode != "CSS1Compat";
    var cont = ieBox ? document.body : document.documentElement;
    return {
      left:   cont.scrollLeft,
      top:    cont.scrollTop,
      width:  cont.clientWidth,
      height: cont.clientHeight
    };
  } else {
    return {
      left:   window.pageXOffset,
      top:    window.pageYOffset,
      width:  window.innerWidth,
      height: window.innerHeight
    };
  }
}

Here is the HTML file:

<html>
 <head>
  <script language="javascript" type="text/javascript" src="expandimg.js"></script>
 </head>
 <body>
  <img src='mypic.jpg' width=400 height=400 alt='img' border=0 
     id='screen142' onmouseout='reducethumb(142); return false;' 
     style='position: absolute; display: none;'>
  <img src='mypic.jpg' width=100 height=100 alt='mis' border=0 
     id='thumb142' onmouseover='expandthumb(142, 500, 449);'></a>
 </body>
</html>

Here, I have used mouseover to expand an image from thumb142 where 142 is the argument, and same to onmouseout in screen142.

Check out the snapshots of the application:

Screenshot - coolimage1.jpg

This is the first snapshot, which will be seen when the HTML loads for the first time. If I move the mouse over this , it will show:

Screenshot - coolimage2.jpg

Thus, the second image will be seen when the mouse is turned over the image.

Example

You can download another sample page that will enable to see more than one image. Just download this example: demo.

The snapshot of this download is:

Screenshot - coolimage3.jpg

This version can be seen online too .. Just click here.

Points of interest

You can use any number of images in your webpage, only you need to do is to make two img tags for each image to be shown, and then give a unique numbered ID to each of them. Each ID should have a prefix as thumb and screen, where screen is the expanded image and thumb is the reduced image.

History

Well, this is my first release. Let's hope we can make a history of this control. Any suggestions, please feel free to post.

I have added another example so that it would be easier to use the script. Thanks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Abhishek Sur
Team Leader
India India
Member
Did you like his post?
 
Oh, lets go a bit further to know him better.
Visit his Website : www.abhisheksur.com to know more about Abhishek.
 
Abhishek also authored a book on .NET 4.5 Features and recommends you to read it, you will learn a lot from it.
http://bit.ly/EXPERTCookBook
 
Basically he is from India, who loves to explore the .NET world. He loves to code and in his leisure you always find him talking about technical stuffs.
 
Presently he is working in WPF, a new foundation to UI development, but mostly he likes to work on architecture and business classes. ASP.NET is one of his strength as well.
Have any problem? Write to him in his Forum.
 
You can also mail him directly to abhi2434@yahoo.com
 
Want a Coder like him for your project?
Drop him a mail to contact@abhisheksur.com
 
Visit His Blog

Dotnet Tricks and Tips



Dont forget to vote or share your comments about his Writing

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralThanks and Questionmemberaim7312 Jun '08 - 12:25 
Hi. This is a really really good script. Thanks for making it available.
 
Just one question, is there any way for the expanded image to expand to the left or the right of the picture? This way, it´s easier to move your mouse from one thumbnail to the next.
 
If my question isn´t clear, I´m referring to something like the one you see on this sitehttp://www.istockphoto.com/file_search.php?text=home&action=file[^]
 
Once again, thank you.
AnswerRe: Thanks and Question [modified]memberAbhishek sur13 Jul '08 - 22:27 
Well aim73,
Thanks for your reply. Its easy to make like this.
It just you need to place the Image in the appropriate place.
function showImage(imgid,event)
 {
    var targetControl = document.getElementById(imgid);    
    targetControl.style.display = "block";    
    var scrollTop = document.documentElement.scrollTop ? document.documentElement.scrollTop:document.body.scrollTop;                                       
    if(event.clientX !=null && event.clientY !=null)
        {
            targetControl.style.left = event.clientX;
            targetControl.style.top = (scrollTop + event.clientY);
        }
}
 
I think your function will be like this. I didnt debug that .. Please check if this works or not.
And also when mouseover, just call this function as onmouseover="javascript:showimage(this,event)";
 
Just make the image Div a floating one for your requirement.
 
Thanks
 
Abhishek Sur

Web Developer

modified on Monday, July 14, 2008 4:34 AM

Generalasp.netmembereirikr19 Dec '07 - 3:04 
How can I get this to work in asp.net?
It wont expand, just jumps from small to big image.
GeneralRe: asp.netmemberAbhishek sur13 Jul '08 - 22:28 
Well, Use client.. if your browser does support Javascript, it will work I think.
 
Abhishek Sur

Web Developer


QuestionNice attemptmemberksawme27 Sep '07 - 17:48 
Hi:
A very nice attempt. Have a question. Is there any way that a flickering can be avoided when it expands or collapse? Also when we are resizing, is there any effect on the quality loss of the picture?
 
Regards,
Swami
AnswerRe: Nice attemptmemberAbhishek sur27 Sep '07 - 20:31 
You have two question...
1. Well definately, You can avoid flickering the screen. I am resizing the image by taking step as 1. If you want to increase the step value by less than 1 or more than one its effect will change accordingly. Using the value to be less than one will decrease the speed of expansion of the image. So you need to change the timeout value also. Just check out the code
 


function expandstep() {
extot = 0;
doexpand();
if (exstep < extotst) {
exstep+=5;// Increment of Step value
extot = setTimeout("expandstep()", 10);//Timeout value to call expandstep() again.
}
}
 
function reducestep() {
extot = 0;
doexpand();
if (exstep > 0) {
exstep-=5; // Decrement of Step value
extot = setTimeout("reducestep()", 10); //Timeout value to call reducestep() again.
} else {
restorethumb();
}
}

Just see, here I am incrementing the value of exstep by 5 not by 1. It will increase the flickering effect. If you want to eliminate the flickering to occur, just make it .5 in both reducestep and expandstep. Also the setTimeout function will call the function after certain milliseconds. On the next line if you decrease the timeout value, the image will be reduced and expanded much faster. I think now you can use my code easily.
 
2. On the issue of image quality loss, I want to tell you one thing, always use the fullheight and fullwidth arguments as the actual size of the image. Suppose you are displaying an image which is of size 640/480 whereas you made the arguments to expandthumb as 1024 / 768. In that case, the quality of the image will detoriate, because the script will forcefully increase the size of the image using style. I think you got answers to both of your questions. Thanks a lot for your question. Keep in touchBig Grin | :-D
 

Abhishek Sur

Web Developer


Generalscreenshotmvptoxcct18 Sep '07 - 23:59 
a screenshot in the article would be great, but your work is worth it. got my 5
 

GeneralRe: screenshotmemberAbhishek sur19 Sep '07 - 0:02 
Thank you very much.. I am adding a screenshot very shortly and thanks for your appreciation
 

Abhishek Sur
Web Developer

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130513.1 | Last Updated 15 Mar 2008
Article Copyright 2007 by Abhishek Sur
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid