原生拖放代码
Posted 大厨的笔记
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了原生拖放代码相关的知识,希望对你有一定的参考价值。
这段js代码会让页面所有position:absolute;的元素可以拖动
var chef = (function(){ var drawDiv,ofstX,ofstY; function fun(event){ var event = event || window.event; var target = event.target || event.srcElement; switch(event.type){ case "mousedown": drawDiv = target; ofstX = event.clientX-drawDiv.offsetLeft; ofstY = event.clientY-drawDiv.offsetTop; break; case "mousemove": if(drawDiv != null){ drawDiv.style.left = event.clientX - ofstX + "px"; drawDiv.style.top = event.clientY - ofstY + "px"; } break; case "mouseup": drawDiv = null; break; } }; document.body.onmousedown = function(event){fun(event);}; document.body.onmousemove = function(event){fun(event);}; document.body.onmouseup = function(event){fun(event);}; })();
以上是关于原生拖放代码的主要内容,如果未能解决你的问题,请参考以下文章