Image hover using Javascript

More...

In this post you will see how to implement image hover with iframe and div. My first try was with div and AJAX. Unfortunately in IE6, select (drop down menu) always stays on top of the hover image. Good old iframe is solution not only to put behind a select element in IE6, but to fetch a content without AJAX.

Please try the following example with IE6. Just move mouse pointer over the Spider1 or Spider2. You will see in version with div how drop down menu stays over the hover image. Other browsers (FireFox, Opera, Google Chrome ...) will have the same result with both versions.

Spider1 Spider2 - version with div
Spider1 Spider2 - version with iframe

I prepared a small JavaScript code to demonstrate the problem and to offer a solution. When user moves pointer over Spider1, JavaScript code calculates the element position on the page, sets the new div location and innerHTML property and finally shows the div element. After mouse pointer moves away, JavaScript will hide div element.

// define reference to the hidden div element
var div;
 
// set reference to the div element after page is loaded
window.onload = function () {
    div = document.getElementById('hoverbox');
}
 
// show hoverbox
function hoverbox_show(obj, src, type) {
    // define oTop and oLeft variables
    var oTop = 0,
        oLeft = 0;
    // find object position on the page
    do {
        oLeft += obj.offsetLeft;
        oTop += obj.offsetTop;
    } while (obj = obj.offsetParent);
    // set the position of invisible div
    div.style.top  = (oTop  + 20) + 'px';
    div.style.left = (oLeft + 20) + 'px';
    // put iframe with img in div element (this will work in any browser)
    if (type === 'iframe') {
        div.innerHTML = '<iframe marginwidth="0" marginheight="0" frameborder="no" ' +
                          'width="100%" height="100%" scrolling="no" ' +
                          'src="/my/img/' + src + '"></iframe>';
    }
    // or place img inside div element (IE6 has problem with select overlay)
    else {
        div.innerHTML = '<img src="/my/img/' + src + '"/>';
    }
    // show hoverbox
    div.style.visibility = 'visible';
}
 
// hide hoverbox (hide div element)
function hoverbox_hide() {
    div.style.visibility = 'hidden';
}

Hover box is a hidden div element. You can place this div element anywhere on the page because hidden div will not interfere with other elements. I usually write div right after body tag.

/* hover box */
#hoverbox{
	position: absolute;
	width: 185px;
	height: 90px;
	visibility: hidden;
}

And here is HTML code for use JavaScript functions mentioned above.

<!-- DIV version -->
<span onmouseover="hoverbox_show(this, 'spider1_big.png', 'div')" onmouseout="hoverbox_hide()">Spider1</span>
<span onmouseover="hoverbox_show(this, 'spider2_big.png', 'div')" onmouseout="hoverbox_hide()">Spider2</span>

<!-- IFRAME version -->
<span onmouseover="hoverbox_show(this, 'spider1_big.png', 'iframe')" onmouseout="hoverbox_hide()">Spider1</span>
<span onmouseover="hoverbox_show(this, 'spider2_big.png', 'iframe')" onmouseout="hoverbox_hide()">Spider2</span>

AJAX isn't needed to fetch and display images on mouseover event. Just use div and set innerHTML if you haven't form elements on the page or you don't care about IE6. Otherwise you will have to apply this iframe trick.

On the other hand, if you want to display HTML in hover box (like tooltip), you will have to set correct URL in iframe src attribute - nothing more. Solution with div will be a little complicated because you will have to request HTML content with AJAX. I wrote a post AJAX progress bar where you can see how to use AJAX.

This entry was posted on June 8, 2009 and is filed under JavaScript

Related posts

9 Responses to Image hover using Javascript

  1. jaro says:

    Hi!
    lol, Your pages with scripts are REALLY cool :)

    But mostly like image hover using JavaScript with div :)

    Thanks dude

  2. jess says:

    might i be able to use this if i were trying to get an image to hover over an iframe without needing the pointer to be over it?

  3. dbunic says:

    jess - Yes, you can create DIV element with inner iframe and absoulte position DIV element where ever on page. It's only needed to properly set top and left styles of DIV element.

  4. Stephen says:

    You need an example of how to call this script from the html. I had to do a view source to see your html.

  5. dbunic says:

    @Stephen - HTML example is included on the page now. Thank you for suggestion.

  6. Greg says:

    How would I get the hoverbox to fit the size of the image to the maximum size of 90px when I use the following code:

    div.innerHTML = '<iframe marginwidth="0" marginheight="0" frameborder="no" ' +
                              'width="90" height="90"
    

    At the moment, any images I select in my HTML page only show a proportion of the image!

    Greg

  7. dbunic says:

    @Greg - I'm not sure that fixed box can be created simply with iframe because iframe loads image from the server. But it is possible with DIV. Here is example of how you can create box of 50px width and height using inline CSS:

    div.innerHTML = '<div style="width:50px;height:50px">' +
    '<img style="display:block;width:100%" src="http://www.redips.net/my/img/' + src + '"/>' +
    '</div>';
    

    I don't prefer inline CSS (because of mixing with HTML) but I hope it will give you a hint ...

  8. Zark says:

    Hello dbunic.
    I now have a problem with images and a slideshow.
    In a nutshell, what the code needs to do is. Change to a next picture (7 in total) each time the mouse is over the pic. Then when the last one is reached my alert will pop and the pic will go back to the original. I have the code however I can not for the life of me get it to work. Can mail me directly if you like.

    I see I need to convert it, that will take some time. Any other methods of me showing you the code?

  9. dbunic says:

    @Zark - JS code to show each time the other image hover doesn't sound complicated. Here are few tips that might help you:
    - image location can be stored in Array
    - set counter to 0 -> var i = 0;
    - on mouse over, display image with index definition like -> idx = i % 7;
    - on mouse out increase counter -> i++;
    The idea is to increase counter on each mouseout and to use modulo operation for preparing Array index. 7 is total number of images to rotate on each mouseover. Hope this tips will help you.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

In case of posting HTML tags or JavaScript code please convert special characters to HTML entities.
Especially pay attention to convert "<" character to "&lt;" entity!