Do I need to animate the image to move away from the cursor position on each tip?

I am trying to animate a window image by changing its position on the mouse.

I can make it move once, but I need to set it up so that it moves over the image every time. I want users to "chase" a window around the screen.

Is it preferable that the animation loop so that the user can never catch the image?

Here is an example of what I have so far , and below is my jQuery code:

$(document).ready(function() { $('#img').mouseover(function() { $(this).animate({ left: '500px' }); }); }); 

Thank you, million!

+6
source share
1 answer

Here is an example. It covers the basics, I think.

 jQuery(function($) { $('#img').mouseover(function() { var dWidth = $(document).width() - 100, // 100 = image width dHeight = $(document).height() - 100, // 100 = image height nextX = Math.floor(Math.random() * dWidth), nextY = Math.floor(Math.random() * dHeight); $(this).animate({ left: nextX + 'px', top: nextY + 'px' }); }); }); 
+12
source

Source: https://habr.com/ru/post/903414/


All Articles