1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
| <body> 实例文字实例文字实例文字实例文字 <div id="div1">实例文字</div> 实例文字实例文字实例文字实例文字 <script> window.onload = function() { drag("div1"); } function drag(id) { var oDiv = document.getElementById(id);
oDiv.onmousedown = function(ev) { var oEvent = event || ev; var disX = oEvent.clientX - oDiv.offsetLeft; var disY = oEvent.clientY - oDiv.offsetTop; oDiv.style.cursor = "move";
if (oDiv.setCapture) { oDiv.onmousemove = fnMove; oDiv.onmouseup = fnUp; oDiv.setCapture(); } else { document.onmousemove = fnMove; document.onmouseup = fnUp; }
function fnMove(ev) { var oEvent = event || ev; var l = oEvent.clientX - disX; var t = oEvent.clientY - disY;
if (l < 50) { l = 0; } else if (l > document.documentElement.clientWidth - oDiv.offsetWidth-50) { l = document.documentElement.clientWidth - oDiv.offsetWidth; } if (t < 50) { t = 0; } else if (t > document.documentElement.clientHeight - oDiv.offsetHeight-50) { t = document.documentElement.clientHeight - oDiv.offsetHeight; } oDiv.style.left = l + 'px'; oDiv.style.top = t + 'px';
}; function fnUp() { this.onmousemove = null; this.onmouseup = null; if (this.releaseCapture) { this.releaseCapture(); } } return false; } } </script> </body>
|