最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue.js?自定義指令(拖拽、拖動、移動)?指令?v-drag詳解

 更新時間:2023年01月23日 10:31:14   作者:丿Mr_Liu  
這篇文章主要介紹了vue.js?自定義指令(拖拽、拖動、移動)?指令?v-drag,本文結(jié)合示例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1.main.js文件中添加已下代碼

Vue.directive('drag', {
  //1.指令綁定到元素上回立刻執(zhí)行bind函數(shù),只執(zhí)行一次
  //2.每個函數(shù)中第一個參數(shù)永遠(yuǎn)是el,表示綁定指令的元素,el參數(shù)是原生js對象
  //3.通過el.focus()是無法獲取焦點的,因為只有插入DOM后才生效
  bind: function (el) { },
  //inserted表示一個元素,插入到DOM中會執(zhí)行inserted函數(shù),只觸發(fā)一次
  inserted: function (el) {
    let odiv = el; //獲取當(dāng)前元素
    let firstTime = '', lastTime = '';
    el.onmousedown = function (e) {
      var disx = e.pageX - el.offsetLeft;
      var disy = e.pageY - el.offsetTop;
      // 給當(dāng)前元素添加屬性,用于元素狀態(tài)的判斷
      odiv.setAttribute('ele-flag', false)
      odiv.setAttribute('draging-flag', true)
      firstTime = new Date().getTime();
      document.onmousemove = function (e) {
        el.style.left = e.pageX - disx + 'px';
        el.style.top = e.pageY - disy + 'px';
      }
      document.onmouseup = function (event) {
        document.onmousemove = document.onmouseup = null;
        lastTime = new Date().getTime();
        if ((lastTime - firstTime) > 200) {
          odiv.setAttribute('ele-flag', true)
          event.stopPropagation()
        }
        setTimeout(function () {
          odiv.setAttribute('draging-flag', false)
        }, 100)
      }
    }
  }
})

2.組件中的使用

<template>
	<div class="drag" v-drag @click="handleDragClick"> 我是拖拽的div<div>
<template>
<script>

methods:{
  handleDragClick(e){
  // 判斷拖拽組件的狀態(tài)
	let isDrag = false;
	try {
      isDrag = e.target.getAttribute('ele-flag') === 'true';
	}catch (e) {
		}
	if(isDrag){
		return;
     }
	 // 當(dāng)推拽組件未在 拖動狀態(tài) 執(zhí)行點擊事件
	 // todo 下面是執(zhí)行點擊事件的代碼
   }
}
</script>
<style>
// 這里是拖拽 組件的樣式
.drag{
  width:100px;
  height:100px;
  border:1px solid pink;
}
</style>

補充:vue自定義拖拽指令v-drag

<template>
  <div class="drag" v-drag ref="drag"></div>
</template>

<script>
export default {
  name: 'Home',
  data(){
    return{
      positionX:'',
      positionY:''
    }
  },
  mounted () {
    this.$refs.drag.style.top = window.localStorage.getItem('top')+'px'
    this.$refs.drag.style.left = window.localStorage.getItem('left')+'px'
  },
  directives: {
    drag: {
      // 指令的定義
      bind: function (el,binding,vnode) {
        console.log(el);
        console.log(binding);
        console.log(vnode.context);
        let odiv = el;  //獲取當(dāng)前元素
        odiv.onmousedown = (e) => {
          //算出鼠標(biāo)相對元素的位置
          let disX = e.clientX - odiv.offsetLeft;
          let disY = e.clientY - odiv.offsetTop;
           
          document.onmousemove = (e)=>{
            //用鼠標(biāo)的位置減去鼠標(biāo)相對元素的位置,得到元素的位置
            let left = e.clientX - disX;  
            let top = e.clientY - disY;
            
            //綁定元素位置到positionX和positionY上面
            vnode.context.positionX = top;
            vnode.context.positionY = left;

            window.localStorage.setItem('top',top)
            window.localStorage.setItem('left',left)
         
            //移動當(dāng)前元素
            odiv.style.left = left + 'px';
            odiv.style.top = top + 'px';
          };
          document.onmouseup = () => {
            document.onmousemove = null;
            document.onmouseup = null;
          };
        };
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.drag{
  position: relative;   /*定位*/
  // top: 10px;
  // left: 10px;
  width: 200px;
  height: 200px;
  background: #666;    /*設(shè)置一下背景*/
}
</style>

到此這篇關(guān)于vue.js 自定義指令(拖拽、拖動、移動) 指令 v-drag的文章就介紹到這了,更多相關(guān)vue.js 自定義指令內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

元阳县| 泾川县| 迁西县| 九寨沟县| 利津县| 钟山县| 伊川县| 瑞昌市| 屏山县| 牟定县| 安远县| 新田县| 特克斯县| 龙海市| 罗城| 河北省| 阿克| 桂平市| 巴彦县| 上思县| 绥滨县| 荆门市| 吴江市| 仪陇县| 德昌县| 宜章县| 保定市| 广灵县| 搜索| 凌源市| 启东市| 柏乡县| 陵川县| 鸡泽县| 桑日县| 大连市| 当雄县| 西充县| 南平市| 同江市| 宁国市|