This article shares the specific code of js to achieve simple image drag effect for your reference. The specific content is as follows //Pictures need to be imported by yourself<!doctype html> <html> <head> <meta charset="UTF-8"> <title>Realize the small square that cannot be clicked in the current display area</title> <style> div{position:fixed;width:100px;height:100px; background-image:url(images/xiaoxin.gif); background-size:100%; } </style> </head> <body> <div id="pop"></div> <script> let pop = document.getElementById("pop") //Define a switch variable to control whether the image follows the mouse movement let canMove = false; //When starting to drag, save the relative position of the mouse from the upper left corner of the div let offsetX,offsetY; //When the mouse is pressed on pop, pop.onmousedown=function(e){ //You can start dragging canMove=true; offsetX=e.offsetX; offsetY=e.offsetY; } //When the mouse moves in the window window.onmousemove=function(e){ //Only when pop can move if(canMove==true){ //Let pop follow the mouse movement //When you start dragging, immediately get the relative position of the mouse to the upper left corner of the image //Find the top and left of pop let left=e.clientX-offsetX; let top=e.clientY-offsetY; //Set the top and left attributes of pop pop.style.left=left+"px"; pop.style.top=top+"px"; } } //When the mouse button is lifted on pop, pop.onmouseup=function(){ //Stop dragging canMove=false } </script> </body> </html> Effect picture: The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: MySQL column to row conversion tips (share)
>>: Detailed explanation of nginx reverse proxy webSocket configuration
Table of contents 1. Bootstrap5 breakpoints 1.1 M...
Table of contents 1. How to use mixin? 2. Notes o...
MySQL itself does not support recursive syntax, b...
Single page application characteristics "Ass...
People who use virtual machines usually set up sh...
Generally speaking, when we view the contents of ...
Table of contents 1. this points to 2. Modify thi...
Before webpack packaging, we must ensure that the...
Recently I saw an article on a public account tha...
MySQL can be set when it is installed, but it see...
Table of contents Demo1 create_fragment SvelteCom...
Without further ado, let’s run the screenshot dir...
In the development environment, the vue project i...
describe: When the Tabs component switches back a...
There are many attributes in CSS. Some attributes...