February 28, 2012

Hover image and show it next to the cursor

Question by Johan

I would like to know if there is a way to show the same image as you are hovering, with a different size, next to the cursor? And on mouseout, it should disappear.

Answer by elclanrs

Something like this will work. Adjust to your needs. http://jsfiddle.net/elclanrs/jF27b/

var $img = $('img');
$img.hide();
$('div').mousemove(function(e) {
    $img.stop(1, 1).fadeIn();
    $('img').offset({
        top: e.pageY - $img.outerHeight(),
        left: e.pageX - $img.outerWidth()
    });
}).mouseleave(function() {
    $img.fadeOut();
});

Answer by Starx

Here is a simple way you can do it.

Markup Structure

<li class="thumb">
    <img class="small" src="http://stackoverflow.com/" />
    <img class="large" src="someimage.jpg" />
</li>

jQuery Snippet

$(".thumb").mouseover(function() {    
   $(this).children(".large").show();
}).mouseout(function() {
   $(this).children(".large").hide();
});

P.S: I am not going to test this code to work, because I completely agree with @OptimusCrime

Demo

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!