javascript實現div拖放

來源:互聯網
上載者:User

動手練習,真的能發現好多問題,有問題還是好的,趁早解決,哈哈~不過還是有困惑~不知道去哪裡找答案,先記著吧,以後看到相關的知識再回過頭來看~

<!DOCTYPE html><html><head><style>#div1{width:100px;height:100px;background:pink;position:absolute;}</style><script>window.onload=function(){var oDiv1 = document.getElementById('div1');oDiv1.onmousedown = function(ev){var oEvent = ev || event;var disX = oEvent.clientX - oDiv1.offsetLeft;   //對象在滑鼠按下時所處的位置var disY = oEvent.clientY - oDiv1.offsetTop;document.onmousemove = function(ev){var oEvent = ev || event;var l = oEvent.clientX - disX;  //滑鼠移動對象的位移var h = oEvent.clientY - disY;oDiv1.style.left = l + 'px';oDiv1.style.top = h + 'px';};};oDiv1.onmouseup = function(ev){//清除函數,否則div將永遠跟著滑鼠移動document.onmousemove = null;document.onmouseup = null;};};</script></head><body><div id="div1"></div></body></html>

看了教程,然後按自己的理解,再寫了這個拖動,發現有bug。這是給div對象加上滑鼠點擊和鬆開的事件,顯示都正常,但是當我們點擊這個div,然後拖動它超出了瀏覽器的可視地區再鬆開滑鼠時,就能發現它並沒有觸發div的onmouseup事件,所以在沒有點擊的情況下,div一直隨滑鼠移動。

然後回頭再去看教程的代碼,如下:

<!DOCTYPE html><html><head><style>#div1{width:100px;height:100px;background:pink;position:absolute;}</style><script>window.onload=function(){var oDiv1 = document.getElementById('div1');oDiv1.onmousedown = function(ev){var oEvent = ev || event;var disX = oEvent.clientX - oDiv1.offsetLeft;   //對象在滑鼠按下時所處的位置var disY = oEvent.clientY - oDiv1.offsetTop;document.onmousemove = function(ev){var oEvent = ev || event;var l = oEvent.clientX - disX;  //滑鼠移動對象的位移var h = oEvent.clientY - disY;oDiv1.style.left = l + 'px';oDiv1.style.top = h + 'px';};document.onmouseup = function(ev){//清除函數,否則div將永遠跟著滑鼠移動document.onmousemove = null;document.onmouseup = null;};};};</script></head><body><div id="div1"></div></body></html>

教程中將onmouseup事件綁定在document上了,這樣滑鼠移出了可視區,鬆開時還是能觸發該事件,所以一切正常。然後我又開始困惑了,為什麼onmouseup要寫在onmousedown裡面呢,按理說,他們兩個雖然有前後關係,先點擊在有鬆開,但是並沒有內含項目關聯性啊,就像滑鼠移入移出一樣,平時很少將移出寫在移入函數裡面,所以我就把onmouseup給弄出來了,結果錯了,div一直跟著滑鼠移動,鬆開貌似也沒觸發onmouseup函數。。。。

<!DOCTYPE html><html><head><style>#div1{width:100px;height:100px;background:pink;position:absolute;}</style><script>window.onload=function(){var oDiv1 = document.getElementById('div1');oDiv1.onmousedown = function(ev){var oEvent = ev || event;var disX = oEvent.clientX - oDiv1.offsetLeft;   //對象在滑鼠按下時所處的位置var disY = oEvent.clientY - oDiv1.offsetTop;document.onmousemove = function(ev){var oEvent = ev || event;var l = oEvent.clientX - disX;  //滑鼠移動對象的位移var h = oEvent.clientY - disY;oDiv1.style.left = l + 'px';oDiv1.style.top = h + 'px';};};document.onmouseup = function(ev){//清除函數,否則div將永遠跟著滑鼠移動document.onmousemove = null;document.onmouseup = null;};};</script></head><body><div id="div1"></div></body></html>

然後多看了幾遍才知道,其實有觸發該函數,這個onmouseup是綁定在document上的,onmouseup鬆開滑鼠時就被清空了,即這個函數只被執行一次就米有了。所以去掉上面的document.onmouseup = null;就可以了

解惑一、將onmouseup事件綁定在document上時,在div上鬆開滑鼠時還是能觸發document上的事件;

困惑一、這裡框框中的兩句把onmousemove函數和onmouseup函數清空了,為什麼第二次點擊div時還能觸發這兩個函數呢,裡面的內容不是被清空了嗎?

困惑二、儘管多次點擊,下面紅色框框中的函數只執行一次,只彈出了一次視窗,上面的就能多次執行呢???

有大大路過的也清忙我解解惑吖~

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.