js實(shí)現(xiàn)簡易拖拽的示例
簡易拖拽
目錄
代碼實(shí)例 代碼解析 scrollWidth,clientWidth,offsetWidth的區(qū)別 offsetX,clientX,pageX的辨析 下載源碼鏈接代碼實(shí)例
<div style='background: #334;width: 100px;height: 100px;position: absolute;cursor: move;'></div>(function () { var dragging = false var boxX, boxY, mouseX, mouseY, offsetX, offsetY var box = document.getElementById(’box’) // 鼠標(biāo)按下的動作 box.onmousedown = down // 鼠標(biāo)的移動動作 document.onmousemove = move // 釋放鼠標(biāo)的動作 document.onmouseup = up // 鼠標(biāo)按下后的函數(shù),e為事件對象 function down(e) { dragging = true // 獲取元素所在的坐標(biāo) boxX = box.offsetLeft boxY = box.offsetTop // 獲取鼠標(biāo)所在的坐標(biāo) mouseX = parseInt(getMouseXY(e).x) mouseY = parseInt(getMouseXY(e).y) // 鼠標(biāo)相對元素左和上邊緣的坐標(biāo) offsetX = mouseX - boxX offsetY = mouseY - boxY } // 鼠標(biāo)移動調(diào)用的函數(shù) function move(e){ if (dragging) { // 獲取移動后的元素的坐標(biāo) var x = getMouseXY(e).x - offsetX var y = getMouseXY(e).y - offsetY // 計算可移動位置的大小, 保證元素不會超過可移動范圍 var width = document.documentElement.clientWidth - box.offsetWidth var height = document.documentElement.clientHeight - box.offsetHeight // min方法保證不會超過右邊界,max保證不會超過左邊界 x = Math.min(Math.max(0, x), width) y = Math.min(Math.max(0, y), height) // 給元素及時定位 box.style.left = x + ’px’ box.style.top = y + ’px’ } } // 釋放鼠標(biāo)的函數(shù) function up(e){ dragging = false } // 函數(shù)用于獲取鼠標(biāo)的位置 function getMouseXY(e){ var x = 0, y = 0 e = e || window.event if (e.pageX) { x = e.pageX y = e.pageY } else { x = e.clientX + document.body.scrollLeft - document.body.clientLeft y = e.clientY + document.body.scrollTop - document.body.clientTop } return { x: x, y: y } }})()
代碼解析
在 document 對象上綁定 mousemove 和 mouseup 事件,不在拖拽的元素上綁定是因?yàn)楫?dāng)鼠標(biāo)移動太快而超出元素的范圍時會停止拖拽,而綁定在 document 上則可以避免這樣的事情發(fā)生。拖拽再快都不會超出 document 的范圍。
// 鼠標(biāo)按下的動作box.onmousedown = down// 鼠標(biāo)的移動動作document.onmousemove = move// 釋放鼠標(biāo)的動作document.onmouseup = up
HTMLElement.offsetLeft是一個只讀屬性,返回當(dāng)前元素左上角相對于HTMLElement.offsetParent 節(jié)點(diǎn)的左邊界偏移的像素值。
// 獲取元素所在的坐標(biāo)boxX = box.offsetLeftboxY = box.offsetTop
一般鼠標(biāo)的位置使用 pageX / pageY 獲取,但是 IE 不支持這兩個屬性。所以在 IE 中使用 event.clientX + document.body.scrollLeft - document.body.clientLeft; 獲取鼠標(biāo)的位置
if (e.pageX) { x = e.pageX y = e.pageY} else { x = e.clientX + document.body.scrollLeft - document.body.clientLeft y = e.clientY + document.body.scrollTop - document.body.clientTop}
scrollWidth,clientWidth,offsetWidth的區(qū)別
定義
scrollWidth:對象的實(shí)際內(nèi)容的寬度,不包括邊線寬度 clientWidth:對象內(nèi)容的可視區(qū)的寬度,不包括邊線寬度 offsetWidth:對象整體的實(shí)際寬度,包括滾動條等邊線情況一
元素內(nèi)無內(nèi)容或者內(nèi)容不超過可視區(qū),滾動不出現(xiàn)或不可用 scrollWidth = clientWidth offsetWidth為元素的實(shí)際寬度
情況二
元素的內(nèi)容超過可視區(qū),滾動條出現(xiàn)和可用 scrollWidth > clientWidth offsetWidth為元素的實(shí)際寬度
offsetX,clientX,pageX的辨析
offsetX,offsetY
指鼠標(biāo)指針相對于觸發(fā)事件元素的左上角的偏移,在Chrome,Opera,Safari中指外邊緣,即將該元素邊框的寬度計算在內(nèi),firefox則不包含邊框值
pageX,pageY
指相對文檔窗口左上角的距離,不會隨滾動條移動
clientX,clientY
指相對于瀏覽器可視窗口左上角的距離,參照點(diǎn)會隨滾動條滾動而移動
下載源碼鏈接
星輝的Github
以上就是js實(shí)現(xiàn)簡易拖拽的示例的詳細(xì)內(nèi)容,更多關(guān)于js實(shí)現(xiàn)簡易拖拽的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. Vue 實(shí)現(xiàn)對quill-editor組件中的工具欄添加title2. 使用Python webdriver圖書館搶座自動預(yù)約的正確方法3. 在線php代碼縮進(jìn)、代碼美化工具:PHP Formatter4. ASP.NET MVC使用jQuery ui的progressbar實(shí)現(xiàn)進(jìn)度條5. Python 合并拼接字符串的方法6. Linux刪除系統(tǒng)自帶版本Python過程詳解7. Python3 json模塊之編碼解碼方法講解8. Python字符串到字節(jié)的轉(zhuǎn)換。雙反斜杠問題9. Android 簡單的實(shí)現(xiàn)滑塊拼圖驗(yàn)證碼功能10. ASP基礎(chǔ)知識VBScript基本元素講解

網(wǎng)公網(wǎng)安備