user1938653 user1938653 - 1 year ago 114
jQuery Question

Make a dynamic image follow mouse

There is this version of image following mouse:

And I've tried to make it work with a dynamically loaded image like this:

var a = new Image();
a.src = '';
a.onload = g;

function g(){
$(a).css({left:e.pageX, top:e.pageY});

But it doesn't work. I need it to work this way since I am going have to draw it on canvas too later on.

I guess the problem is that jQuery can't find the image that way, so how do I make it work?

Answer Source

Set id to the element and append it in the DOM

As position : absolute; is being applied through css, applying id over image element is essential.

var a = new Image(); = 'image';
a.src = '';
a.onload = g;

function g() {
  $(document).mousemove(function(e) {
      left: e.pageX,
      top: e.pageY
#image {
  position: absolute;
<script src=""></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download