title: track mouse position to move images
body: <p>I have a pretty simple page.</p>
  
  <pre><code>&lt;div id="index"&gt;
      &lt;img /&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>the styling is pretty simple too.</p>
  
  <pre><code>#index {position:relative;}
  #index img {position:absolute; bottom:10%; right:10%; width:100%;}
  </code></pre>
  
  <p>I use % so the image can be resized proportionally if the browser window resizes. Never mind that.</p>
  
  <p>The problem is, I'm trying to emulate the effect on this flash site : <a href="http://www.tatogomez.com/" rel="nofollow">http://www.tatogomez.com/</a> So the image is in the bottom right of the screen. When I move my mouse to top left, the image will slightly move a bit more to the right bottom. When I move my mouse to center, the image will revert back to its original position. So it's kinda like I'm giving a shadow/lighting effect where the mouse is the lighting and the image is the object, except I only need the moving animation.</p>
  
  <p>my code is like this</p>
  
  <pre><code>$(document).ready(function($){
      $('#index').mousemove(
          function(e){
              $(this).children('img').each(
                  function(){
                      var totalWidth = $(window).width();
                      var totalHeight = $(window).height();
                      var centerX = $(window).width() / 2;
                      var centerY = $(window).height() / 2;
  
                      var mouseX = e.pageX;
                      var mouseY = e.pageY;
  
                      var current_top = $(this).offset().top;
                      var current_left = $(this).offset().left;
  
                      var myX =  (centerX-mouseX)/centerX;
                      var myY =  (centerY-mouseY)/centerY;
                      var cssObj = {
                          'left': current_left + myX + 'px'
                          'top': current_top + myY + 'px'
                      }
                      $(this).css(cssObj);
                  }
              );
  
          }
      );
  });
  </code></pre>
  
  <p>so if I move the mouse in relation to the center of the screen. So basically it's like this:</p>
  
  <pre><code>centerX = 700 (i use resolution 1400);
  currentLeft = ~200 (the offset left of my image)
  
  So if my mouse position is at 700-1400, then the myX will be 700(centerX) - 900(mouse position) = -200 / 700 = ~ -0.3. Add it into the css calculation, the left will be current_left(200) + myX(-0.3) px = 197.7px.
  </code></pre>
  
  <p>then i realizes if i move my mouse from the center to the right (700-1400 range), the image will slightly move to the left, but when I move my mouse from the right to the center, the image still moves to the left! It should move to the right to its original position, but it doesn't because the web doesn't know whether the mouse moves from right to center or from center to right.</p>
  
  <p>Any help?</p>