vue.js 自定义指令(拖拽、拖动、移动) 指令 v-drag详解

作者:丿Mr_Liu 时间:2024-05-28 15:55:24 

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

Vue.directive('drag', {
 //1.指令绑定到元素上回立刻执行bind函数,只执行一次
 //2.每个函数中第一个参数永远是el,表示绑定指令的元素,el参数是原生js对象
 //3.通过el.focus()是无法获取焦点的,因为只有插入DOM后才生效
 bind: function (el) { },
 //inserted表示一个元素,插入到DOM中会执行inserted函数,只触发一次
 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;
     // 给当前元素添加属性,用于元素状态的判断
     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){
 // 判断拖拽组件的状态
let isDrag = false;
try {
     isDrag = e.target.getAttribute('ele-flag') === 'true';
}catch (e) {
}
if(isDrag){
return;
    }
// 当推拽组件未在 拖动状态 执行点击事件
// todo 下面是执行点击事件的代码
  }
}
</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>

来源:https://blog.csdn.net/qq_37237495/article/details/104689452

标签:vue.js,自定义指令,v-drag
0
投稿

猜你喜欢

  • vue.js使用v-model实现表单元素(input) 双向数据绑定功能示例

    2023-07-02 16:28:28
  • 如何让利用Python+AI使静态图片动起来

    2022-06-06 08:15:31
  • Python迭代器和生成器定义与用法示例

    2022-02-15 19:42:13
  • ASP生成html的新方法

    2011-04-02 11:04:00
  • Python使用Pandas库实现MySQL数据库的读写

    2024-01-22 18:28:28
  • 实例讲解启动mysql server失败的解决方法

    2008-12-26 17:27:00
  • python图像处理模块Pillow的学习详解

    2021-06-03 19:19:03
  • 简洁的是最好的吗?

    2010-06-24 21:46:00
  • Python实现滑动平均(Moving Average)的例子

    2023-02-10 22:36:58
  • python groupby函数实现分组后选取最值

    2022-06-02 09:27:45
  • Python使用progressbar模块实现的显示进度条功能

    2023-11-20 05:40:07
  • Python调用API接口实现人脸识别

    2022-09-10 22:42:20
  • Python中内建函数的简单用法说明

    2021-09-01 08:19:58
  • Python入门教程之运算符与控制流

    2022-08-04 03:38:40
  • 使用auto.js实现自动化每日打卡功能

    2024-04-16 08:47:38
  • 无闪烁更新网页内容JS实现

    2024-05-09 10:37:18
  • Python import自定义模块方法

    2022-08-23 06:57:27
  • 数字人组件反写[asp组件开发实例2]

    2009-06-09 13:15:00
  • Linux下指定mysql数据库数据配置主主同步的实例

    2024-01-16 09:10:37
  • Python imgaug库安装与使用教程(图片加模糊光雨雪雾等特效)

    2021-06-23 10:07:16
  • asp之家 网络编程 m.aspxhome.com