This article shares the specific code of js to achieve a simple drag effect for your reference. The specific content is as follows 1. Basic effects of draggingIdeas: When the mouse is pressed on the box, it is ready to move (the event is added to the object) When the mouse moves, the box follows the mouse (the event is added to the page) When the mouse is lifted, the box stops moving (the event is added to the page) var o = document.querySelector('div'); //Mouse down o.onmousedown = function (e) { //Mouse position relative to the box var offsetX = e.clientX - o.offsetLeft; var offsetY = e.clientY - o.offsetTop; //Mouse movement document.onmousemove = function (e) { o.style.left = e.clientX - offsetX + "px"; o.style.top = e.clientY - offsetY + "px"; } //Mouse up document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; } } 2. Drag and drop issuesIf text appears in the box, or the box itself is an image, due to the default behavior of the browser (text and images can be dragged), we can set return false to prevent its default behavior. However, this interception of default behavior is not applicable in lower versions of IE. You can use global capture to solve the IE problem. Global capture Global capture is only applicable to lower versions of IE. <button>btn1</button> <button>btn2</button> <script> var bts = document.querySelectorAll('button') bts[0].onclick = function () { console.log(1); } bts[1].onclick = function () { console.log(2); } // bts[0].setCapture() //Add global capture // bts[0].releaseCapture() ; //Release global capture</script> Once a global capture is added for a specified node, other elements on the page will not trigger the same type of event. 3. Full version of drag and dropvar o = document.querySelector('div'); //Mouse down o.onmousedown = function (e) { if (o.setCapture) { //IE lower version o.setCapture() } e = e || window.event //Mouse position relative to the box var offsetX = e.clientX - o.offsetLeft; var offsetY = e.clientY - o.offsetTop; //Mouse movement document.onmousemove = function (e) { e = e || window.event o.style.left = e.clientX - offsetX + "px"; o.style.top = e.clientY - offsetY + "px"; } //Mouse up document.onmouseup = function () { document.onmousemove = null; document.onmouseup = null; if (o.releaseCapture) { o.releaseCapture(); //Release global capture} } return false; //Default behavior of standard browsers} 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:
|
<<: js to achieve floor scrolling effect
>>: Example code for converting http to https using nginx
Table of contents 1. Function debounce 1. What is...
Introduction to jQuery The jQuery library can be ...
The company had a well-configured server that was...
Table of contents Message Board Required librarie...
GitHub address: https://github.com/dmhsq/dmhsq-my...
Basically all e-commerce projects have the functi...
Preface In the daily development or maintenance o...
Ubuntu 18.04, other versions of Ubuntu question: ...
1. SHOW PROCESSLIST command SHOW PROCESSLIST show...
1. Download the MySQL installation package First ...
First we need to install some dependencies npm i ...
Key Takeaways: 1. Mastering CSS3 3D animation 2. ...
1: Download from mysql official website https://d...
1. Background 1.1 Problems A recent product testi...
Log rotation is a very common function on Linux s...