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 ?
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!
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With