<script type="text/javascript"> window.onload = function() { var oDiv = document.getElementById("div1"); var disX = 0; var disY = 0; oDiv.onmousedown = function(ev) { var oEvent = ev || event; disX = oEvent.clientX - oDiv.offsetLeft; disY = oEvent.clientY - oDiv.offsetTop; if (oDiv.setCapture) { oDiv.onmousemove = mouseMove; oDiv.onmouseup = mouseUp; oDiv.setCapture(); // IE 7 下文字就不会被选中 其实就是文字或图片得不到事件 } else { document.onmousemove = mouseMove; document.onmouseup = mouseUp; } function mouseMove(ev) { var oEvent = ev || event; var oDivLeft = oEvent.clientX - disX; var oDivTop = oEvent.clientY - disY; oDiv.style.left = oDivLeft + 'px'; oDiv.style.top = oDivTop + 'px'; } function mouseUp(ev) { this.onmousemove = null; this.onmouseup = null; if (oDiv.releaseCapture) { oDiv.releaseCapture(); // 释放捕获 } } return false; // 阻止默认事件,解决火狐的bug }; }; </script>
好了,都搞定了 O(∩_∩)O哈哈~
您可能感兴趣的文章: