This example uses jQuery to implement a mouse drag image function. First, set up a wrapper. The coordinates in the wrapper are the coordinates of the image movement. #wrapper{ width: 1000px; height:1000px; position:relative; } Set the image div, which is the div to be dragged #div1{ position: absolute; left:0px; top:0px; width: 300px; height: 200px; background: url("d:/Pictures/Earth.jpg"); background-size:contain; } The above sets the positioning of wrapper to relative and div1 to absolute. Next, design the dragging algorithm: The idea is as follows: 1. Let the div follow the mouse when the mouse is clicked 2. Stop following when the mouse is released First, we need a function that will change the coordinates of the div to the current mouse position: First, you need to define several variables to save the current mouse coordinates and the image coordinates. var timer; var mouseX=0; var mouseY=0; var pic_width = parseInt($("#div1").css("width")); var pic_height = parseInt($("#div1").css("height")); Now we need to add an event listener to the wrapper. When the mouse moves in the wrapper, modify the values of the variables mousex and mousey. $("#wrapper").mousemove(function(e){ mouseX = e.clientX; mouseY = e.clientY; }); Write a follow function and call it with a timer $("#div1").mousedown(function(){ timer=setInterval(follow,10); }); $("#div1").mouseup(function(){ clearInterval(timer); }); var follow = function(){ $("#div1").css("left",mouseX-pic_width/2); $("#div1").css("top",mouseY-pic_height/2); }; The complete code is as follows: <!doctype html> <html> <head> <script type = "text/javascript" src = "jquery.js"></script> <style type = "text/css"> #wrapper{ width: 1000px; height:1000px; position: relative; background: linear-gradient(lightblue,white); font-size: 40px; } #div1{ position: absolute; left:0px; top:0px; width: 300px; height: 200px; background: url("d:/Pictures/Earth.jpg"); background-size:contain; } </style> </head> <body> <div id = "wrapper"> Lorem, ipsum dolor sit amet consectetur adipisicing elit. Impedit numquam accusamus explicabo praesentium laudantium et accusantium, ab ipsum, excepturi necessitatibus quos iste ad qui deleniti sed debitis reiciendis quam nisi. <div id = "div1"> </div> </div> <script> var timer; var mouseX=0; var mouseY=0; var pic_width = parseInt($("#div1").css("width")); var pic_height = parseInt($("#div1").css("height")); $("#wrapper").mousemove(function(e){ mouseX = e.clientX; mouseY = e.clientY; }); $("#div1").mousedown(function(){ timer=setInterval(follow,10); }); $("#div1").mouseup(function(){ clearInterval(timer); }); var follow = function(){ $("#div1").css("left",mouseX-pic_width/2); $("#div1").css("top",mouseY-pic_height/2); }; </script> </body> </html> Final result: This is the end of this article about how to use jQuery to drag images with the mouse. For more information about how to use jQuery to drag images with the mouse, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Several common ways to deploy Tomcat projects [tested]
>>: In-depth understanding of MySQL self-connection and join association
<iframe src=”test.jsp” width=”100″ height=”50″...
Preface HTTP and HTTPS In our daily life, common ...
Beginners who are exposed to HTML learn some HTML...
As usual, today I will talk about a very practica...
I just tried it on IE6, and it does show the toolb...
NProgress is the progress bar that appears at the...
This article shares the specific code of the jQue...
Table of contents mysql filtered replication Impl...
Note that this article does not simply teach you ...
Table of contents question Reproduction Implicit ...
Table of contents question Solution question Ther...
Create a new configuration file (for example, go ...
I heard that there is an interview question: How ...
This article example shares the specific code of ...
After installing MySQL, you will find that the ro...