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
nick88p
5,716 PointsObject 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");
});
});
4 Answers
Colton Ehrman
Courses Plus Student 5,859 PointsYou 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");
});
});
Colton Ehrman
Courses Plus Student 5,859 PointsAlso, you misspelled animate, just caught that lol
nick88p
5,716 PointsI 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.
Colton Ehrman
Courses Plus Student 5,859 PointsYou misspelled animate
nick88p
5,716 PointsHow 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
Colton Ehrman
Courses Plus Student 5,859 PointsLol, yeah the animation is a little glitchy
Christian Andersson
8,712 PointsChristian Andersson
8,712 PointsSorry 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.