How to implement drag position Preview with Native JS
This article is about how native JS implements drag-and-drop location preview. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
The effect is as follows:
The following is the code implementation, you are welcome to copy and paste and complain.
Native JS implements drag position preview. Box {position: absolute; border: 1px dashed black;} # div1 {width: 100px; height: 100px; background: yellow; position: absolute } _ window.onload = function () {var oDiv = document.getElementById ('div1'); oDiv.onmousedown = function (ev) {var oEvent = ev | | event; var disX = oEvent.clientX-oDiv.offsetLeft; var disY = oEvent.clientY-oDiv.offsetTop / / create a dashed frame div var oNewDiv = document.createElement ('div'); oNewDiv.className =' box'; / / minus the size of the border coincides with the original div size oNewDiv.style.width = oDiv.offsetWidth-2 + 'px'; oNewDiv.style.height = oDiv.offsetHeight-2 +' px' ONewDiv.style.left = oDiv.offsetLeft + 'px'; oNewDiv.style.top = oDiv.offsetTop +' px'; document.body.appendChild (oNewDiv); _ document.onmousemove = function (ev) {var oEvent = ev | | event; oNewDiv.style.left = oEvent.clientX-disX + 'px' ONewDiv.style.top = oEvent.clientY-disY + 'px';}; _ document.onmouseup = function () {_ document.onmousemove = null; _ document.onmouseup = null; oDiv.style.left = oNewDiv.style.left ODiv.style.top = oNewDiv.style.top; / / remove dotted frame document.body.removeChild (oNewDiv);}; Thank you for reading! This is the end of the article on "how to realize the drag location Preview of Native JS". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!