vue.js 自定义指令(拖拽、拖动、移动) 指令 v-drag详解
作者:丿Mr_Liu 发布时间:2024-05-28 15:55:24
标签:vue.js,自定义指令,v-drag
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


猜你喜欢
- Golang浮点数比较和运算会出现误差。浮点数储存至内存中时,2的-1、-2……-n次方不能精确的表示小数部分,所以再把这个数从地址中取出来
- 本文实例讲述了python去除文件中空格、Tab及回车的方法。分享给大家供大家参考,具体如下:在最近的开发工作中,为了应付比赛赶进度,服务端
- 本文实例讲述了php中加密解密DES类的简单使用方法。分享给大家供大家参考,具体如下:在平时的开发工作中,我们经常会对关键字符进行加密,可能
- 前言在前后端分离的开发中,通过 Restful API 进行数据交互时,如果没有对 API 进行保护,那么别人就可以很容易地获取并调用这些
- 爬取网页的流程一般如下:选着要爬的网址(url)使用 python 登录上这个网址(urlopen、requests 等)读取网页信息(re
- def report_hook(count, block_size, total_size):... &n
- 本文实例为大家分享了JavaScript实现动态生成表格的具体代码,供大家参考,具体内容如下功能描述在输入框中输入行和列,点击按钮,生成拥有
- 1.如果没有采用响应式布局,指定表格的宽度即可解决比如table{ width:1400px !important;}2.如果采用了响应式布
- 本文实例讲述了js字符串操作方法。分享给大家供大家参考。具体如下:var str="This is my first Script
- 如下所示:函数功能abs(x)返回一个数的绝对值。 参数可以是一个整数或浮点数。 如果参数是一个复数,则返回它的模。all(iterable
- Server对象提供对服务器上访问的方法和属性.大多数方法和属性是作为实用程序的功能提供的。语法:Server.property|metho
- 当创建一个Models, 在同步到数据库里,django默认设置了三个权限 ,就是 add, change, delete权限。但是往往有时
- 问题最近在使用 Vue 做东西,用到钉钉扫描登录的功能,这里需要引入远程的 js 文件,因为 Vue 的方式跟之前的不太一样,又不想把文件下
- 修改/etc/my.cnf或者/etc/mysql/my.cnf文件[client]default-character-set = utf8
- 本文实例讲述了PHP邮件发送类PHPMailer用法,并详细讲述了其具体的操作步骤。分享给大家供大家参考。具体步骤如下:1.在服务器安装 s
- 一.Memory Dumps 1).Global Area ALTER SESSION SET EVENTS ‘immediate trac
- PHP addcslashes() 函数实例在字符 "W" 前添加反斜杠:<?php $str = addcsla
- python读取npy文件时,太大不能完全显示,其解决方法当用python读取npy文件时,会遇到npy文件太大,用print函数打印时不能
- 在经过前面几个部分的操作之后,我们的网页已经图文并茂,具有相当的效果了,但是这对于网页来说还不够,为了网站中的众多网页能够成为一个有机的整体
- Spring事务管理事务(Transaction),一般是指要做的或所做的事情。在计算机术语中是指访问并可能更新数据库中各种数据项的一个程序