Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery move divs according to mouse position

I got a little Problem with jQuery, maybe you guys can help me. Here's the HTML Markup in jfiddle for better understanding:

jsfiddle

I'd like to have the div called #move_me to move either to the right or to the left side within the #content_wrapper according to the mouse position inside #content_wrapper.

Basically what I want is: if I move my cursor inside the #content_wrapper div to the right then the #move_me div should move to the left to see content2 div, when I move to the left the #move_me div should move to the right to see content1 div..

I tried to get it to work with mousemove and pageXoffset but I didn't get it to work.

something like this:

$('#content_wrapper').mousemove(function(e){
var x = e.pageX - this.parentoffsetLeft;
$('#move_me').css({'left': x}); 
});

HERE IS AN IMAGE EXPLAINING MORE PRECISELY:

Illustration http://www.22labs.com/moveme.jpg

like image 931
seanarcher Avatar asked Dec 05 '25 11:12

seanarcher


1 Answers

Working DEMO

Try this

I guess this is what you need, Add position:relative; to your move_me

code

$('#content_wrapper').mousemove(function (e) {

    $('#move_me').animate({
        'left': -e.pageX + 15 + 'px'
    }, 0);

});

Hope this helps, Thank you

like image 105
SarathSprakash Avatar answered Dec 07 '25 01:12

SarathSprakash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!