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

JavaScript Image Popup

By , 28 Jan 2009
 

Introduction

Many a times, developers require to show larger popup images when users hover their mouse pointer over a thumbnail image. I will explain here a very simple JavaScript that can be used to show a popup image. The script is tested, and works fine in IE 7.0 and Mozilla Firefox 3.0.

Steps for the implementation

  1. Create a DIV named "imgbox" on the HTML page on which your thumbnail images will be shown. The DIV and the CSS element ID associated with the DIV is shown below:
  2. <div id="imgbox"></div>

    The CSS element:

    #imgbox 
    {
        vertical-align : middle;
        position : absolute;
        border: 1px solid #999;
        background : #FFFFFF; 
        filter: Alpha(Opacity=100);
        visibility : hidden;
        height : 200px;
        width : 200px;
        z-index : 50;
        overflow : hidden;
        text-align : center;
    }
  3. Here is the JavaScript code to show the popup image:
    1. Get the left and top positions of the thumbnail image:
    2. function getElementLeft(elm) 
      {
          var x = 0;
      
          //set x to elm’s offsetLeft
          x = elm.offsetLeft;
      
          //set elm to its offsetParent
          elm = elm.offsetParent;
      
          //use while loop to check if elm is null
          // if not then add current elm’s offsetLeft to x
          //offsetTop to y and set elm to its offsetParent
      
          while(elm != null)
          {
              x = parseInt(x) + parseInt(elm.offsetLeft);
              elm = elm.offsetParent;
          }
          return x;
      }
      
      function getElementTop(elm) 
      {
          var y = 0;
      
          //set x to elm’s offsetLeft
          y = elm.offsetTop;
      
          //set elm to its offsetParent
          elm = elm.offsetParent;
      
          //use while loop to check if elm is null
          // if not then add current elm’s offsetLeft to x
          //offsetTop to y and set elm to its offsetParent
      
          while(elm != null)
          {
              y = parseInt(y) + parseInt(elm.offsetTop);
              elm = elm.offsetParent;
          }
      
          return y;
      }
    3. Get the thumbnail image source, make the DIV visible, increase the height and width to the required size, and attach the image to the DIV.
    4. function Large(obj)
      {
          var imgbox=document.getElementById("imgbox");
          imgbox.style.visibility='visible';
          var img = document.createElement("img");
          img.src=obj.src;
          img.style.width='200px';
          img.style.height='200px';
          
          if(img.addEventListener){
              img.addEventListener('mouseout',Out,false);
          } else {
              img.attachEvent('onmouseout',Out);
          }             
          imgbox.innerHTML='';
          imgbox.appendChild(img);
          imgbox.style.left=(getElementLeft(obj)-50) +'px';
          imgbox.style.top=(getElementTop(obj)-50) + 'px';
      }
    5. Hide the DIV at mouse out.
    6. function Out()
      {
          document.getElementById("imgbox").style.visibility='hidden';
      }
  4. Add a OnMouseOver client-side event call for the thumbnail images to show the popup image on mouse-over.
  5. <img id='img1' src='images/Sample.jpg' onmouseover="Large(this)" />

Comments

Hope this JavaScript would solve your popup image requirement. If you have implemented a popup image feature in some other way, or developed a feature-rich script, please put a link to your CodeProject article so that others can refer it too.

License

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

About the Author

ASPDev200
Founder Tuhina Infomedia Pty Ltd.
Australia Australia
Member
I have my own digital marketing company and own a very successful deals and discount website: www.deals2save.com.au
 
Before starting my own company I worked in the IT industry for around 9 years in various roles starting from developer to project manager.

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   
QuestionGoodmemberengmebeed3 Oct '12 - 21:46 
This is good demo, but could you help me and write the complete code for popup 4 images and have a navigation button (like next, prev, and close).
I want the popup like make the page go to black so i can't see any thing else the image and so on.
Thanks at all.
QuestionFollowing on from JavaScript Image PopupmemberMartin Goodrich23 Sep '12 - 0:21 
Hi this is a really nice little example but I'd like to consider taking it to another level.
 
Consider that I have a div that I am dynamically filling with ImageButtons containing the thumbnail image. I would like to add the same ability to be able to click on one of the links and produce an enlarged image.
 
I've tried so many things but so far I've been unsuccessful. If anybody has an example I would be forever in their debt if they would be kind enough to share it with me.
SuggestionRe: Following on from JavaScript Image PopupmemberASPDev20025 Sep '12 - 13:02 
Hi,
 
I am glad to see that the old article still generates some interests.
 
Well, if you are looking for good image gallery solutions then why don't you try some jquery solutions. One good example is:
 
http://leandrovieira.com/projects/jquery/lightbox/
 
This one is very easy to implement and looks nice.
 
Thanks,
Sudipta Das
Digital Marketing Consultant
Tuhina Infomedia Pty Ltd
www.deals2save.com.au - Best deals in Australia

GeneralMy vote of 5memberNana Nurhayanto17 Oct '10 - 20:58 
very simple and tiny
GeneralCalculating positionmemberAlex.Shnaider2 Feb '09 - 20:01 
The functions getElementLeft() and getElementTop() aren’t work in IE5 if you put the element in table that have more than 1 row. Try to use the getBoundingClientRect() function instead.
JokeWow!memberSmirkinGherkin28 Jan '09 - 23:48 
Where do you get Firefox 5.0 and where can I get that?
GeneralRe: Wow!memberemiaj29 Jan '09 - 12:04 
Probably he comes from the future. Smile | :)
 

GeneralRe: Wow!memberMember 378218930 Jan '09 - 2:04 
http://www.stovesareus.co.uk/catalog/firefox-5-multifuel-woodburning-stove-p-3143.html

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.130523.1 | Last Updated 28 Jan 2009
Article Copyright 2009 by ASPDev200
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid