欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue模塊拖拽實(shí)現(xiàn)示例代碼

 更新時間:2019年03月09日 14:26:01   作者:王_ye  
這篇文章主要介紹了vue模塊拖拽實(shí)現(xiàn)示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

正巧在之前面試中遇到問實(shí)現(xiàn)拖拽效果

當(dāng)時面試的時候簡單回答了實(shí)現(xiàn)的方式與邏輯。

現(xiàn)在閑來無事,把這個東西實(shí)現(xiàn)了一下。

原理很簡單,寫的很方便。

數(shù)據(jù)驅(qū)動,建立一個數(shù)組,數(shù)組初始長度為1

拖動觸發(fā)時,添加一個對象到數(shù)組中,拖動的是下標(biāo)為0的對象,新建的還在原來位置放著,等待下次拖動。

話不多說,上代碼

<template>
  <div class="view">
   <div class="x" @mousedown="move($event,index)" v-for="(x,index) in i">
    <span v-if="index+1 !== i.length">{{index+1}}</span>
    <input v-model="x.input">
   </div>
   {{i}}
  </div>
</template>

<script>
  export default {
    name: "index",
   data(){
     return{
      positionX:0,
      positionY:0,
      i:[
       {input:''}
      ]
     }
   },
   methods:{
     move(e,x){
      let odiv = e.target;    //獲取目標(biāo)元素
      //算出鼠標(biāo)相對元素的位置
      let disX = e.clientX - odiv.offsetLeft;
      let disY = e.clientY - odiv.offsetTop;
      let flag = true;
      document.onmousemove = (e)=>{    //鼠標(biāo)按下并移動的事件
       if(flag && x === this.i.length-1){
        flag = false;
        this.i.push({input:''})
       }
       //用鼠標(biāo)的位置減去鼠標(biāo)相對元素的位置,得到元素的位置
       let left = e.clientX - disX;
       let top = e.clientY - disY;
       //綁定元素位置到positionX和positionY上面
       this.positionX = top;
       this.positionY = left;
       //移動當(dāng)前元素
       odiv.style.left = left + 'px';
       odiv.style.top = top + 'px';
      };
      document.onmouseup = (e) => {
       document.onmousemove = null;
       document.onmouseup = null;
      };
     }
   }
  }
</script>

<style scoped lang="less">
 .view{
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: #f8f8f8;
  .x{
   width: 250px;
   height: 50px;
   top: 50px;
   left: 10px;
   position: absolute;
   background: red;
   color: yellow;
  }
 }
</style>

一個簡單的demo,后續(xù)用的話可以再豐富,比如以拖動長度來觸發(fā)事件。

input可以換成子組件。這里提供分享一個底層的實(shí)現(xiàn)方式

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論