Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i create moving element using mouse position, like at http://fieroanimals.com/?

I want to create div element with an image inside, and change it position with coordinates from mouse position. There is a few jquery-pluings for creating 3d effect, but i need only move on X and Y coordinates. Also, do that in limit of div element.

I got next code:

$('div.images').mousemove(function(e){
        var x = e.pageX - this.offsetLeft;
        var y = e.pageY - this.offsetTop;
        $('div.images').css({'top': x,'left': y}); 
    });

In CSS div.images have absolute position. When i move my mouse is not changing position, but when I delete position in CSS it's apply changes to the style of element, but no change position.

Giv me some advise what I need to do.

At http://fieroanimals.com/ that realised on Flash, but I want do that on JQuery.

like image 429
Alexander Shvets Avatar asked Jan 17 '23 06:01

Alexander Shvets


2 Answers

$(document).ready(function(){
      $('div.moveAble-container').mousemove(function(e){
            var y = e.pageY;
            var x = e.pageX;                    
            $('div.moveAble').css({'top': y}); 
            $('div.moveAble').css({'left': x});

      });
    });
like image 194
User99 Avatar answered Jan 25 '23 23:01

User99


$('div.images').mousemove(...) implies that it will only detect mouse movements that are over top of div.images.

$('html') will detect mouse movement over the whole page.

jsFiddle Example

Depending on your needs, you may want apply the X-coordinate to your CSS left property and Y-coordinate to CSS top. (You have it the other way around in your question).

like image 43
Jeff Jenkins Avatar answered Jan 26 '23 00:01

Jeff Jenkins