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

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

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

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

Vue.directive('drag', {
  //1.指令綁定到元素上回立刻執(zhí)行bind函數(shù),只執(zhí)行一次
  //2.每個函數(shù)中第一個參數(shù)永遠是el,表示綁定指令的元素,el參數(shù)是原生js對象
  //3.通過el.focus()是無法獲取焦點的,因為只有插入DOM后才生效
  bind: function (el) { },
  //inserted表示一個元素,插入到DOM中會執(zhí)行inserted函數(shù),只觸發(fā)一次
  inserted: function (el) {
    let odiv = el; //獲取當前元素
    let firstTime = '', lastTime = '';
    el.onmousedown = function (e) {
      var disx = e.pageX - el.offsetLeft;
      var disy = e.pageY - el.offsetTop;
      // 給當前元素添加屬性,用于元素狀態(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;
     }
	 // 當推拽組件未在 拖動狀態(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;  //獲取當前元素
        odiv.onmousedown = (e) => {
          //算出鼠標相對元素的位置
          let disX = e.clientX - odiv.offsetLeft;
          let disY = e.clientY - odiv.offsetTop;
           
          document.onmousemove = (e)=>{
            //用鼠標的位置減去鼠標相對元素的位置,得到元素的位置
            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)
         
            //移動當前元素
            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;    /*設置一下背景*/
}
</style>

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

相關文章

  • 解決vuex數(shù)據(jù)頁面刷新后初始化操作

    解決vuex數(shù)據(jù)頁面刷新后初始化操作

    這篇文章主要介紹了解決vuex數(shù)據(jù)頁面刷新后初始化操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-07-07
  • Vue的MVVM實現(xiàn)方法

    Vue的MVVM實現(xiàn)方法

    本篇文章主要主要介紹了Vue的MVVM實現(xiàn)方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-08-08
  • vue項目中使用AvueJs的詳細教程

    vue項目中使用AvueJs的詳細教程

    Avue.js是基于現(xiàn)有的element-ui庫進行的二次封裝,簡化一些繁瑣的操作,核心理念為數(shù)據(jù)驅動視圖,主要的組件庫針對table表格和form表單場景,這篇文章給大家介紹了vue項目中使用AvueJs的相關知識,感興趣的朋友跟隨小編一起看看吧
    2022-10-10
  • vue-cli項目中使用公用的提示彈層tips或加載loading組件實例詳解

    vue-cli項目中使用公用的提示彈層tips或加載loading組件實例詳解

    這篇文章主要介紹了vue-cli項目中使用公用的提示彈層tips或加載loading組件,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2018-05-05
  • vue.js過濾器+ajax實現(xiàn)事件監(jiān)聽及后臺php數(shù)據(jù)交互實例

    vue.js過濾器+ajax實現(xiàn)事件監(jiān)聽及后臺php數(shù)據(jù)交互實例

    這篇文章主要介紹了vue.js過濾器+ajax實現(xiàn)事件監(jiān)聽及后臺php數(shù)據(jù)交互,結合實例形式分析了vue.js前臺過濾器與ajax后臺數(shù)據(jù)交互相關操作技巧,需要的朋友可以參考下
    2018-05-05
  • VUE跨域問題Access to XMLHttpRequest at

    VUE跨域問題Access to XMLHttpRequest at

    今天發(fā)現(xiàn)一個錯誤,VUE發(fā)送請求的時候不能請求到正確數(shù)據(jù),VUE跨域問題Access to XMLHttpRequest at,本文就詳細的來介紹一下解決方法,感興趣的可以了解一下
    2022-05-05
  • vue3.0 項目搭建和使用流程

    vue3.0 項目搭建和使用流程

    這篇文章主要介紹了vue3.0 項目搭建和使用流程,幫助大家更好的理解和學習使用vue框架,感興趣的朋友可以了解下
    2021-03-03
  • 關于dev-tool安裝方法(手動安裝版)

    關于dev-tool安裝方法(手動安裝版)

    這篇文章主要介紹了關于dev-tool安裝方法(手動安裝版),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • Vue中router-view無法顯示的解決辦法

    Vue中router-view無法顯示的解決辦法

    這篇文章主要給大家介紹了關于Vue中router-view無法顯示的解決辦法,router-view組件作為vue最核心的路由管理組件,在項目中作為路由管理經常被使用到,需要的朋友可以參考下
    2023-07-07
  • Vue3中reactive響應式失效的解決方案

    Vue3中reactive響應式失效的解決方案

    這篇文章主要給大家分享Vue3中reactive響應式失效的問題的解決方法,文中有詳細的解決方案供大家參考,如果又遇到一樣問題的同學,可以借鑒閱讀本文
    2023-08-08

最新評論