html隨意拖動(dòng)內(nèi)容位置的兩種實(shí)現(xiàn)方式

測(cè)試:chrome v80.0.3987.122 正常
兩種方式為:拖拽普通標(biāo)簽位置或拖拽canvas中的文本框位置
1. 實(shí)現(xiàn)鼠標(biāo)拖動(dòng)標(biāo)簽元素到任意位置
css 代碼
#range { position: relative; width: 600px; height: 400px; margin: 10px; background-color: rgb(133, 246, 250); } .icon { position: absolute; height: 100px; width: 100px; cursor: move; background-color: #ff9204; user-select: none; }
html代碼
<div id="range"> <div class="icon">100*100</div> </div>
js代碼
const main = document.getElementById('range'); const icon = document.querySelector('.icon'); let move = false; let deltaLeft = 0, deltaTop = 0; // 拖動(dòng)開(kāi)始事件,要綁定在被移動(dòng)元素上 icon.addEventListener('mousedown', function (e) { /* * @des deltaLeft 即移動(dòng)過(guò)程中不變的值 */ deltaLeft = e.clientX-e.target.offsetLeft; deltaTop = e.clientY-e.target.offsetTop; move = true; }) // 移動(dòng)觸發(fā)事件要放在,區(qū)域控制元素上 main.addEventListener('mousemove', function (e) { if (move) { const cx = e.clientX; const cy = e.clientY; /** 相減即可得到相對(duì)于父元素移動(dòng)的位置 */ let dx = cx - deltaLeft let dy = cy - deltaTop /** 防止超出父元素范圍 */ if (dx < 0) dx = 0 if (dy < 0) dy = 0 if (dx > 500) dx = 500 if (dy > 300) dy = 300 icon.setAttribute('style', `left:${dx}px;top:${dy}px`) } }) // 拖動(dòng)結(jié)束觸發(fā)要放在,區(qū)域控制元素上 main.addEventListener('mouseup', function (e) { move = false; console.log('mouseup'); })
2. canvas繪制文本框,并實(shí)現(xiàn)鼠標(biāo)將其拖拽移動(dòng)到任意位置
css 代碼
.cus-canvas{ background: rgb(50, 204, 243); } .input-ele{ display: none; position: fixed; width: 180px; border: 0; background-color: #fff; }
html 代碼
<div> <canvas id="canvas" class="cus-canvas" width="800" height="600"></canvas> <input id="inputEle" class="input-ele"/> </div>
js代碼
實(shí)現(xiàn)原理為記錄鼠標(biāo)移動(dòng)的位置,不斷的重繪矩形框和文本內(nèi)容
let mouseDown = false; let deltaX = 0; let deltaY = 0; let text = 'hello' const canvas = document.getElementById('canvas'); const ctx = canvas.getContext('2d'); const cw = canvas.width, ch = canvas.height; const rect = { x: 20, y: 20, width: 150, height: 50 } /** 設(shè)置文字和邊框樣式 */ ctx.font="16px Arial"; ctx.fillStyle = "#fff"; /** 設(shè)置為 center 時(shí),文字段的中心會(huì)在 fillText的 x 點(diǎn) */ ctx.textAlign = 'center'; ctx.lineWidth = '2'; ctx.strokeStyle = '#fff'; strokeRect() const inputEle = document.getElementById('inputEle'); inputEle.onkeyup = function(e) { if(e.keyCode === 13) { text = inputEle.value strokeRect() inputEle.setAttribute('style', `display:none`) } } canvas.ondblclick = function(e){ inputEle.setAttribute('style', `left:${e.clientX}px;top:${e.clientY}px;display:block`); inputEle.focus(); } canvas.onmousedown = function(e){ /** 獲取視口左邊界與canvas左邊界的距離 加上 鼠標(biāo)點(diǎn)擊位置與canvas左邊界的長(zhǎng)度,這個(gè)值是相對(duì)移動(dòng)過(guò)程中不變的值 */ deltaX=e.clientX - rect.x; deltaY=e.clientY - rect.y; mouseDown = true }; const judgeW = cw-rect.width, judgeH = ch-rect.height; canvas.onmousemove = function(e){ if(mouseDown) { /** 相減即可獲得矩形左邊界與canvas左邊界之間的長(zhǎng)度 */ let dx = e.clientX-deltaX; let dy = e.clientY-deltaY; if(dx < 0) { dx = 0; } else if (dx > judgeW) { dx = judgeW; } if(dy < 0) { dy = 0; } else if(dy > judgeH) { dy = judgeH; } rect.x = dx; rect.y = dy; strokeRect() } }; canvas.onmouseup = function(e){ mouseDown = false }; /** 清除指定區(qū)域 */ function clearRect() { ctx.clearRect(0, 0, cw, ch) } /** 畫矩形 */ function strokeRect() { clearRect() /** 這里如果不調(diào)用 beginPath 歷史的矩形會(huì)重新被繪制 */ ctx.beginPath() ctx.rect(rect.x, rect.y, rect.width, rect.height) ctx.stroke(); // 設(shè)置字體內(nèi)容,以及在畫布上的位置 ctx.fillText(text, rect.x + 70, rect.y + 30); }
歡迎交流 Github
到此這篇關(guān)于html隨意拖動(dòng)內(nèi)容位置的兩種實(shí)現(xiàn)方式的文章就介紹到這了,更多相關(guān)html隨意拖動(dòng)內(nèi)容內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!
相關(guān)文章
基于Html5實(shí)現(xiàn)的react拖拽排序組件示例
這篇文章主要介紹了基于Html5實(shí)現(xiàn)的react拖拽排序組件示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-08-13- 本文通過(guò)實(shí)例代碼給大家介紹了HTML5拖拽功能實(shí)現(xiàn)的拼圖游戲,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-07-31
- 拖拽API是HTML5的新特性,相對(duì)于其他新特性來(lái)說(shuō),重要程度占到6成。這篇文章通過(guò)經(jīng)典案例給大家介紹了HTML5拖拽API的知識(shí)要點(diǎn),需要的朋友參考下吧2018-04-20
- 這篇文章主要介紹了HTML5 拖拽批量上傳文件的示例代碼的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-28
html5實(shí)現(xiàn)多圖片預(yù)覽上傳及點(diǎn)擊可拖拽控件
這篇文章主要介紹了html5實(shí)現(xiàn)多圖片預(yù)覽上傳及點(diǎn)擊可拖拽控件的相關(guān)資料,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-15- 本篇文章主要介紹了詳解Html5原生拖拽操作,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-12
HTML5 拖放(Drag 和 Drop)詳解與實(shí)例代碼
本篇文章主要介紹了HTML5 拖放(Drag 和 Drop)詳解與實(shí)例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-09-14- 拖放是一種常見(jiàn)的特性,即抓取對(duì)象以后拖到另一個(gè)位置,在 HTML5 中,拖放是標(biāo)準(zhǔn)的組成部分。在HTML5中用戶可以使用鼠標(biāo)選擇一個(gè)可拖動(dòng)元素,將元素拖動(dòng)到一個(gè)可放置元素,2017-08-23
- 這篇文章主要介紹了html5使用Drag事件編輯器拖拽上傳圖片的示例代碼的相關(guān)資料,需要的朋友可以參考下2017-08-22
HTML5拖放功能_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了HTML5拖放功能的相關(guān)資料,如何實(shí)現(xiàn)HTML5拖放效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-13