vue實(shí)現(xiàn)拖拽的簡單案例 不超出可視區(qū)域
本文實(shí)例為大家分享了vue實(shí)現(xiàn)拖拽x效果的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)拖拽之前,先了解幾個(gè)小常識:
這兩種獲取鼠標(biāo)坐標(biāo)的方法,區(qū)別在于基于的對象不同:
- pageX和pageY獲取的是鼠標(biāo)指針距離文檔(HTML)的左上角距離,不會(huì)隨著滾動(dòng)條滾動(dòng)而改變;
- clientX和clientY獲取的是鼠標(biāo)指針距離可視窗口(不包括上面的地址欄和滑動(dòng)條)的距離,會(huì)隨著滾動(dòng)條滾動(dòng)而改變;
1.clientX : 是用來獲取鼠標(biāo)點(diǎn)擊的位置距離 當(dāng)前窗口 左邊的距離
2.clientY: 是用來獲取鼠標(biāo)點(diǎn)擊的位置距離 當(dāng)前窗口 上邊的距離
3.offsetWidth: 用來獲取當(dāng)前拖拽元素 自身的寬度
4.offsetHeight:用來獲取當(dāng)前拖拽元素 自身的高度
5.document.documentElement.clientHeight :屏幕的可視高度
6.document.documentElement.clientWidth:屏幕的可視高度
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>vue實(shí)現(xiàn)拖拽</title> <script src="./js/vue.min.js"></script> </head> <style> *{margin: 0;padding:0;} #app{ position: relative; /*定位*/ top: 10px; left: 10px; width: 80px; height: 80px; background: #666; /*設(shè)置一下背景*/ } </style> <body> <div id="app" @mousedown="move"> {{positionX}} {{positionY}} </div> </body> <script> var vm = new Vue({ el: "#app", data: { positionX: 0, positionY: 0 }, methods: { move(e){ let odiv = e.target;// 獲取目標(biāo)元素 //計(jì)算出鼠標(biāo)相對點(diǎn)擊元素的位置,e.clientX獲取的是鼠標(biāo)的位置,OffsetLeft是元素相對于外層元素的位置 let x = e.clientX - odiv.offsetLeft; let y = e.clientY - odiv.offsetTop; console.log(odiv.offsetLeft,odiv.offsetTop) document.onmousemove = (e) => { // 獲取拖拽元素的位置 let left = e.clientX - x; let top = e.clientY - y; this.positionX = left; this.positionY = top; //console.log(document.documentElement.clientHeight,odiv.offsetHeight) // 把拖拽元素 放到 當(dāng)前的位置 if (left <= 0) { left = 0; } else if (left >= document.documentElement.clientWidth - odiv.offsetWidth){ //document.documentElement.clientWidth 屏幕的可視寬度 left = document.documentElement.clientWidth - odiv.offsetWidth; } if (top <= 0) { top = 0; } else if (top >= document.documentElement.clientHeight - odiv.offsetHeight){ // document.documentElement.clientHeight 屏幕的可視高度 top = document.documentElement.clientHeight - odiv.offsetHeight } odiv.style.left = left + "px"; odiv.style.top = top + "px" } // 為了防止 火狐瀏覽器 拖拽陰影問題 document.onmouseup = (e) => { document.onmousemove = null; document.onmouseup = null } } } }) </script> </html>
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vue elementui簡易側(cè)拉欄的使用小結(jié)
這篇文章主要介紹了vue elementui簡易側(cè)拉欄的使用,增加了側(cè)拉欄,目的是可以選擇多條數(shù)據(jù)展示數(shù)據(jù),本文通過示例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-06-06Vue.js中使用iView日期選擇器并設(shè)置開始時(shí)間結(jié)束時(shí)間校驗(yàn)功能
本文通過實(shí)例代碼給大家介紹了Vue.js中使用iView日期選擇器并設(shè)置開始時(shí)間結(jié)束時(shí)間校驗(yàn)功能,需要的朋友可以參考下2018-08-08vue-music 使用better-scroll遇到輪播圖不能自動(dòng)輪播問題
根據(jù)vue-music視頻中slider組建的使用,當(dāng)安裝新版本的better-scroll,輪播組件,不能正常輪播。如何解決這個(gè)問題呢,下面小編給大家?guī)砹藇ue-music 使用better-scroll遇到輪播圖不能自動(dòng)輪播問題,感興趣的朋友一起看看吧2018-12-12vue使用video.js實(shí)現(xiàn)播放m3u8格式的視頻
這篇文章主要為大家詳細(xì)介紹了vue如何使用video.js實(shí)現(xiàn)播放m3u8格式的視頻,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12vue 實(shí)現(xiàn)click同時(shí)傳入事件對象和自定義參數(shù)
這篇文章主要介紹了vue 實(shí)現(xiàn)click同時(shí)傳入事件對象和自定義參數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01