0

I recently decided that I want an icon to follow the mouse on my web site. So I added the following to a JavaScript file that every page on my site uses

$(document).ready(function() {
    var mouseFollowerHTML = "<img class='mouse-follower' src='/images/space_needle_icon.png' alt='space needle'/>";
    $('.maincontent').append(mouseFollowerHTML);    
});

$(document).mousemove(function(e) {
    $('.mouse-follower').offset({
        left: e.pageX,
        top: e.pageY + 20
    });
});

with the intention that it would inject an image into the maincontent element (which is on every page) and then have the image follow the mouse. But it's not working. I've tried adding an alert to the $(document).ready(...) function, but nothing happened.

Please correct me in the error(s) of my ways.

3
  • Have you included jQuery in your scripts? Commented Nov 9, 2014 at 21:44
  • What exactly does "not working" mean? Have you checked for errors in the console? Commented Nov 9, 2014 at 21:44
  • possible duplicate of Make an image follow mouse pointer Commented Nov 9, 2014 at 21:45

1 Answer 1

1

Check that you connected jQuery to the of the page:

<script src="//code.jquery.com/jquery-1.11.1.min.js"></script>

And check out this jsbin: http://jsbin.com/sipaxaqipo/1/edit?html,js,console,output (it works fully with your code), so your code is correct. Mistake is somewhere in another part of site, hope you just forgot to connect jQuery.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.