溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用Vue操作DIV的方法

發(fā)布時(shí)間:2020-12-22 11:54:03 來源:億速云 閱讀:897 作者:小新 欄目:移動開發(fā)

小編給大家分享一下使用Vue操作DIV的方法,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

效果圖

使用Vue操作DIV的方法

demo1.gif

分清clientY pageY screenY layerY offsetY的區(qū)別

在我們想要做出拖拽這個(gè)效果的時(shí)候,我們需要分清這幾個(gè)屬性的區(qū)別,這幾個(gè)屬性都是計(jì)算鼠標(biāo)點(diǎn)擊的偏移值,我們需要對其進(jìn)行了解才可以繼續(xù)實(shí)現(xiàn)我們的拖拽效果

clientY 指的是距離可視頁面左上角的距離
pageY 指的是距離可視頁面左上角的距離(不受頁面滾動影響)
screenY 指的是距離屏幕左上角的距離
layerY 指的是找到它或它父級元素中最近具有定位的左上角距離
offsetY 指的是距離它自己左上角的距離
一張圖帶大家簡單了解了解

使用Vue操作DIV的方法

區(qū)別

在我們簡單了解完這些個(gè)屬性以后,有幾個(gè)屬性需要分清。


 相同點(diǎn)
不同點(diǎn)
clientY距離頁面左上角距離受頁面滾動的影響
pageY距離頁面左上角的距離不受頁面滾動影響

相同點(diǎn)不同點(diǎn)
layerY距離元素的左上角距離受元素的定位的影響,會從本元素往上找到第一個(gè)定位的元素的左上角
offsetY距離元素左上角的距離計(jì)算相對于本元素的左上角,不在乎定位問題,計(jì)算的是內(nèi)交點(diǎn)。是IE瀏覽器的特有屬性

使用Vue操作DIV的方法

layerY與offsetY區(qū)別

實(shí)現(xiàn)拖拽功能

我們既然熟悉了這幾個(gè)偏移屬性的意思,那么我們就進(jìn)入我們的重點(diǎn)。話不多說直接上代碼

// darg.html

<style>
  #app{
    position: relative;   /*定位*/
    top: 10px;
    left: 10px;
    width: 200px;
    height: 200px;
    background: #666;    /*設(shè)置一下背景*/
  }
</style>
<body>
  <p id="app" @mousedown="move">    <!--綁定按下事件-->
    {{positionX}}
    {{positionY}}
  </p>
</body>
//main.js
let app = new Vue({
  el:'#app',
  data:{
    positionX:0,
    positionY:0,
  },
  methods:{
    move(e){
      let op = e.target;    //獲取目標(biāo)元素
      
      //算出鼠標(biāo)相對元素的位置
      let disX = e.clientX - op.offsetLeft;
      let disY = e.clientY - op.offsetTop;
      document.onmousemove = (e)=>{    //鼠標(biāo)按下并移動的事件
        //用鼠標(biāo)的位置減去鼠標(biāo)相對元素的位置,得到元素的位置
        let left = e.clientX - disX;  
        let top = e.clientY - disY;
        
        //綁定元素位置到positionX和positionY上面
        this.positionX = top;
        this.positionY = left;
        
        //移動當(dāng)前元素
        op.style.left = left + 'px';
        op.style.top = top + 'px';
      };
      document.onmouseup = (e) => {
        document.onmousemove = null;
        document.onmouseup = null;
      };
    }  
  
  },
  computed:{},
});

當(dāng)然,我們可以將它綁定為一個(gè)自定義指令,這樣的話就可以用調(diào)用指令的形式來實(shí)現(xiàn)拖拽效果,下面是定義自定義指令的代碼

// darg.html

<style>
  #app{
    position: relative;   /*定位*/
    top: 10px;
    left: 10px;
    width: 200px;
    height: 200px;
    background: #666;    /*設(shè)置一下背景*/
  }
</style>
<body>
  <p id="app" v-drag>    <!--實(shí)現(xiàn)用指令形式實(shí)現(xiàn)拖拽效果-->
    
  </p>
</body>
//main.js
let app = new Vue({
  el:'#app',
  data:{},
  methods:{},
  directives: {
    drag: {
      // 指令的定義
      bind: function (el) {
        let op = el;  //獲取當(dāng)前元素
        op.onmousedown = (e) => {
          //算出鼠標(biāo)相對元素的位置
          let disX = e.clientX - op.offsetLeft;
          let disY = e.clientY - op.offsetTop;
          
          document.onmousemove = (e)=>{
            //用鼠標(biāo)的位置減去鼠標(biāo)相對元素的位置,得到元素的位置
            let left = e.clientX - disX;  
            let top = e.clientY - disY;
           
            //綁定元素位置到positionX和positionY上面
            this.positionX = top;
            this.positionY = left;
        
            //移動當(dāng)前元素
            op.style.left = left + 'px';
            op.style.top = top + 'px';
          };
          document.onmouseup = (e) => {
            document.onmousemove = null;
            document.onmouseup = null;
          };
        };
      }
    }
  }
});

以上是“使用Vue操作DIV的方法”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI