Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Object doesn't follow mouse

Good day everyone,

I would like to make an image follow my mouse pointer at a certain speed. Can anyone tell me where my mistake is?

  <body>
    <img id="rocket" src="rocket.jpg">
  </body>
#rocket {
  position: absolute;
}
$(document).ready(function(){
    $(document).mousemove(function(e){
    mouseX = e.pageX;
    mouseY = e.pageY;
  });
  $(document).click(function(){
    $("#rocket").animtate({
      left: mouseX,
      top: mouseY
    }, "slow");
  });
});

Sorry for posting this off-topic, but I'm helping Treehouse investigate a potential issue with their webserver. Please follow this link and leave a comment saying if you have experienced this webserver issue or not.

https://teamtreehouse.com/forum/attention-treehouse-webserver-issues-please-read

Thank you.

4 Answers

You are using click(), so the image will only move once you click, if you want it to move always, then put the animate inside the mousemove like

$(document).ready(function(){
    $(document).mousemove(function(e){
    mouseX = e.pageX;
    mouseY = e.pageY;

 $("#rocket").animtate({
      left: mouseX,
      top: mouseY
    }, "slow");
  }); 
});

Also, you misspelled animate, just caught that lol

I actually initially had it that way. I just added the click functionality for testing. Should have taken it out here though. In any case, this code is not working for me. The object is not following the mouse pointer. Any suggestions are welcome. Thanks.

You misspelled animate

How embarrassing. Sorry for wasting your time. It's working now. :) It's not doing what I thought it would, but at least I can work with this now. Thanks again. Nick

Lol, yeah the animation is a little glitchy