We often see on some websites that the mouse moves on a web page, and an image moves with the mouse. Do you know how this effect is made? You may feel it is complicated. In fact, it uses JS to write a small program, and the code is simple and easy to understand. I'll share the implementation code below.
The key code is as follows:
<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>js Move to</title><script type="text/javascript">function Divflying(){var div=document.getElementById('dv1');if (!div) {return;}var intX=window.event.clientX;var intY=window.event.clientY;div.style.left=intX+"px";div.style.top=intY+"px";}document.onmousemove=Divflying;</script></head><body><div id="dv1" style="position:absolute;"><img src="fly.jpg"><br>Farewell to the mouse in a low key~~</div></body></html>The above is the JavaScript-based JavaScript-based image moving image. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support to Wulin.com website!