本經驗講述原生JavaScript實作網頁元素拖曳的方法,以及拖曳的進階內容。
1.HTML和CSS程式碼自己加一下。
JavaScript程式碼如下:
<script> 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; document.onmousemove=function (ev) { var oEvent=ev||event; oDiv.style.left=oEvent.clientX-disX+'px'; oDiv.style.top=oEvent.clientY-disY+'px'; }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; }; }; }; </script>
拖曳程式碼封裝與呼叫
<script> window.onload=function () { drag('div1'); drag('div2'); drag('div3'); };function drag(id){ var oDiv=document.getElementById(id); oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; document.onmousemove=function (ev) { var oEvent=ev||event; oDiv.style.left=oEvent.clientX-disX+'px'; oDiv.style.top=oEvent.clientY-disY+'px'; }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; }; };} </script>
拖曳拽,效果如圖所示。
1.拖曳元素右下角可以改變元素的大小,右下角是一個很小的圖片。
JavaScript代碼:
<script> 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; var oNewDiv=document.createElement('div'); oNewDiv.className='box'; 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; document.body.removeChild(oNewDiv); }; };}; </script>
拖拽進階之碰撞檢測
塊紅色碰撞檢測檢測到
碰之後,黃色塊變色了。JavaScript密碼吸附,JavaScript程式碼:
<script> window.onload=function (){ var oDiv=document.getElementById('div1'); var oDiv2=document.getElementById('div2'); oDiv.onmousedown=function (ev){ var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; document.onmousemove=function (ev){ var oEvent=ev||event; oDiv2.style.width=oEvent.clientX-disX+oDiv.offsetWidth+'px'; oDiv2.style.height=oEvent.clientY-disY+oDiv.offsetHeight+'px'; }; document.onmouseup=function (){ document.onmousemove=null; document.onmouseup=null; }; };}; </script>
拖曳進階之限制範圍
1.實作區塊只能在固定區域拖曳,無法拖曳文件。 JavaScript程式碼:
<script> window.onload=function (){ var oDiv=document.getElementById('div1'); var oDiv2=document.getElementById('div2'); oDiv.onmousedown=function (ev) { var oEvent=ev||event; var disX=oEvent.clientX-oDiv.offsetLeft; var disY=oEvent.clientY-oDiv.offsetTop; document.onmousemove=function (ev) { var oEvent=ev||event; oDiv.style.left=oEvent.clientX-disX+'px'; oDiv.style.top=oEvent.clientY-disY+'px'; var l1=oDiv.offsetLeft; var r1=oDiv.offsetLeft+oDiv.offsetWidth; var t1=oDiv.offsetTop; var b1=oDiv.offsetTop+oDiv.offsetHeight; var l2=oDiv2.offsetLeft; var r2=oDiv2.offsetLeft+oDiv2.offsetWidth; var t2=oDiv2.offsetTop; var b2=oDiv2.offsetTop+oDiv2.offsetHeight; if(r1<l2 || l1>r2 || b1<t2 || t1>b2) { oDiv2.style.background='yellow'; } else { oDiv2.style.background='green'; } }; document.onmouseup=function () { document.onmousemove=null; document.onmouseup=null; }; };}; </script>
以上是JavaScript實作網頁拖曳的方法的詳細內容。更多資訊請關注PHP中文網其他相關文章!