Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an image follow mouse pointer

I need a rocket to follow the movements of the mouse pointer on my website. This means it should rotate to face the direction of motion, and if possible, accelerate depending on the distance it has to cover. Is this even possible ? jquery perhaps ?

like image 282
supernoob Avatar asked Aug 22 '11 06:08

supernoob


People also ask

How do I get mouse position in CSS?

Handling the custom propertiessquare element's width and height values. Let's start with the width and say that we want the minimum width of the . square to be 100px (i.e. when the mouse cursor is at the left side of the screen), and we want it to grow 20px for each step the mouse cursor moves to the right. That's it!


1 Answers

by using jquery to register .mousemove to document to change the image .css left and top to event.pageX and event.pageY.

example as below http://jsfiddle.net/BfLAh/1/

$(document).mousemove(function(e) {   $("#follow").css({     left: e.pageX,     top: e.pageY   }); });
#follow {   position: absolute;   text-align: center; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <div id="follow"><img src="https://placekitten.com/96/140" /><br>Kitteh</br> </div>

updated to follow slowly

http://jsfiddle.net/BfLAh/3/

for the orientation , you need to get the current css left and css top and compare with event.pageX and event.pageY , then set the image orientation with

-webkit-transform: rotate(-90deg);  -moz-transform: rotate(-90deg);  

for the speed , you can set the jquery .animation duration to certain amount.

like image 90
wizztjh Avatar answered Oct 10 '22 01:10

wizztjh