This article describes the advanced drag method that JS implements compatible with various browsers. Share it for your reference, as follows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd"><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title> Advanced drag</title> <style> #toBeDraged{ width:100px; height:100px; line-height:100px; border:1px solid red; position:absolute; text-align:center; font-family:Arial, Helvetica, sans-serif; cursor:move; } </style> <script type= "text/javascript"> window.onload = function(){ doDrag(); } function doDrag(){ var div = document.getElementById("toBeDraged"); var posx; var posy; div.onmousedown = function(e){ var e = e || window.event; if (div.setCapture) { div.setCapture(); } posx = e.clientX - parseInt(div.offsetLeft); posy = e.clientY - parseInt(div.offsetTop); document.onmousemove = function(ev){ var ev = ev || window.event;//If it is IE if (ev.setcapture) { } div.style.left = (ev.clientX - posx)+"px"; div.style.top = (ev.clientY - posy)+"px"; } document.onmouseup = function(){ document.onmousemove = null; document.onmouseup = null; if (div.releaseCapture) { div.releaseCapture(); } } } } } </script> </head> <body> <div id = "toBeDraged">You drag me!</div> </body></html>For more information about JavaScript related content, please check out the topics of this site: "Summary of JavaScript switching effects and techniques", "Summary of JavaScript search algorithm skills", "Summary of JavaScript animation effects and techniques", "Summary of JavaScript errors and debugging techniques", "Summary of JavaScript data structures and algorithm skills", "Summary of JavaScript traversal algorithms and techniques", and "Summary of JavaScript mathematical operations usage"
I hope this article will be helpful to everyone's JavaScript programming.