margin布局拖拽

Vue.directive('drag', {
bind(el, binding, vnode, oldVnode) { const dialogHeaderEl = el.querySelector('#top');
// const dragDom = el.querySelector('.alert_child');
dialogHeaderEl.style.cursor = 'move'; let dragBox = el; //获取当前元素
dialogHeaderEl.onmousedown = e => {
//算出鼠标相对元素的位置
let disX = e.clientX - dragBox.offsetLeft;
let disY = e.clientY - dragBox.offsetTop;
document.onmousemove = e => {
//用鼠标的位置减去鼠标相对元素的位置,得到元素的位置
let left = e.clientX - disX;
let top = e.clientY - disY;
//移动当前元素
dragBox.style.marginLeft = left + "px";
dragBox.style.marginTop = top + "px";
};
document.onmouseup = e => {
//鼠标弹起来的时候不再移动
document.onmousemove = null;
//预防鼠标弹起来后还会循环(即预防鼠标放上去的时候还会移动)
document.onmouseup = null;
};
}; }
})

定位拖拽

Vue.directive('show_drag', {
bind(el, binding, vnode, oldVnode) {
const dialogHeaderEl = el.querySelector('.bt') || el.querySelector('.top') || el.querySelector('.header')||el.querySelector(".head");
const dragDom = el;
dialogHeaderEl.style.cursor = 'move';
// 获取原有属性 ie dom元素.currentStyle 火狐谷歌 window.getComputedStyle(dom元素, null);
const sty = dragDom.currentStyle || window.getComputedStyle(dragDom, null); dialogHeaderEl.onmousedown = (e) => {
// 鼠标按下,计算当前元素距离可视区的距离
const disX = e.clientX - dialogHeaderEl.offsetLeft;
const disY = e.clientY - dialogHeaderEl.offsetTop; // 获取到的值带px 正则匹配替换
let styL, styT; // 注意在ie中 第一次获取到的值为组件自带50% 移动之后赋值为px
if (sty.left.includes('%')) {
styL = +document.body.clientWidth * (+sty.left.replace(/\%/g, '') / );
styT = +document.body.clientHeight * (+sty.top.replace(/\%/g, '') / );
} else {
styL = +sty.left.replace(/\px/g, '');
styT = +sty.top.replace(/\px/g, '');
}; document.onmousemove = function (e) {
// 通过事件委托,计算移动的距离
var l = e.clientX < || e.clientX > (document.body.clientWidth - ) ? : e.clientX - disX;
var t = e.clientY < || e.clientY > (document.body.clientHeight - ) ? : e.clientY - disY; // 移动当前元素
if (l) {
dragDom.style.left = `${l + styL}px`;
}
if (t) {
dragDom.style.top = `${t + styT}px`;
} //将此时的位置传出去
//binding.value({x:e.pageX,y:e.pageY})
}; document.onmouseup = function (e) {
document.onmousemove = null;
document.onmouseup = null;
};
}
}
})

vue自定义事件---拖拽的更多相关文章

  1. 原生js拖拽、jQuery拖拽、vue自定义指令拖拽

    原生js拖拽: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  2. Vue自定义事件

    前面的话 父组件使用props传递数据给子组件,子组件怎么跟父组件通信呢?这时,Vue的自定义事件就派上用场了.本文将详细介绍Vue自定义事件 事件绑定 每个 Vue 实例都实现了事件接口 (Even ...

  3. Vue.Draggable实现拖拽效果(采坑小记)

    之前有写过Vue.Draggable实现拖拽效果(快速使用)(http://www.cnblogs.com/songdongdong/p/6928945.html)最近项目中要用到这个拖拽的效果,当产 ...

  4. (尚031)Vue_案例_自定义事件(组件间通信第2种方式:vue自定义事件)

    自定义事件: 我们知道,父组件使用prop传递数据的子组件,但子组件怎么跟父组件通信呢? 这个时候Vue的自定义事件系统就派得上用场了. 自定义事件知道两件事: (1).绑定 (2).触发 注意:$o ...

  5. vue视频: 自定义指令 && 拖拽 && 自定义键盘信息

    v-textv-forv-html 指令: 扩展html语法 自定义指令:1. 自定义属性指令: Vue.directive(指令名称,function(参数){ this.el -> 原生DO ...

  6. 基于Vue实现可以拖拽的树形表格(原创)

    因业务需求,需要一个树形表格,并且支持拖拽排序,任意未知插入,github搜了下,真不到合适的,大部分树形表格都没有拖拽功能,所以决定自己实现一个.这里分享一下实现过程,项目源代码请看github,插 ...

  7. QT笔记之自定义窗口拖拽移动

    1.QT自定义标题栏,拖拽标题栏移动窗口(只能拖拽标题,其他位置无法拖拽) 方法一: 转载:http://blog.sina.com.cn/s/blog_4ba5b45e0102e83h.html . ...

  8. Android 自定义可拖拽View,界面渲染刷新后不会自动回到起始位置

    以自定义ImageView为例: /** * 可拖拽ImageView * Created by admin on 2017/2/21. */ public class FloatingImageVi ...

  9. iPhone手机解锁效果&&自定义滚动条&&拖拽--Clone&&窗口拖拽(改变大小/最小化/最大化/还原/关闭)

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

随机推荐

  1. 使用csv模块读写csv格式文件

    import csv class HandleCsv: ''' csv文件处理类 ''' def __init__(self, filename): ''' 构造器 :param filename: ...

  2. Javascript模块化开发4——Grunt常用模块

    一.copy 用于复制文件与目录. grunt-contrib-copy 二.eslint 检测代码的合理性. grunt-eslint 常见参数: 1.quiet 是否只显示errors.默认值fa ...

  3. .deb文件安装应该怎么做

    https://unix.stackexchange.com/questions/159094/how-to-install-a-deb-file-by-dpkg-i-or-by-apt

  4. 2018-8-10-win10-uwp-进度条-Marquez-

    原文:2018-8-10-win10-uwp-进度条-Marquez- title author date CreateTime categories win10 uwp 进度条 Marquez li ...

  5. 表达式树练习实践:C#判断语句

    目录 表达式树练习实践:C#判断语句 if if...else switch ?? 和 ?: 表达式树练习实践:C#判断语句 判断语句 C# 提供了以下类型的判断语句: 语句 描述 if 一个 if ...

  6. Python超详细的字符串用法大全

    字符串拼接 实际场景:把列表中的数据拼接成一个字符串 解决方案:使用 str.join() 方法 >>> li = ['cxk', 'cxk', 'kk', 'caibi'] > ...

  7. Python规范:提高可读性

    PEP 8 规范 PEP 是 Python Enhancement Proposal 的缩写,翻译过来叫"Python 增强规范". 缩进规范 PEP 8 规范告诉我们,请选择四个 ...

  8. 让你彻底理解volatile,面试不再愁

    本人免费整理了Java高级资料,涵盖了Java.Redis.MongoDB.MySQL.Zookeeper.Spring Cloud.Dubbo高并发分布式等教程,一共30G,需要自己领取.传送门:h ...

  9. Json互相序列化对象

    using System.ServiceModel; using System.ServiceModel.Web; using System.Runtime.Serialization.Json; u ...

  10. Web安全攻防笔记-SQL注入

    information_schema(MySQL5.0版本之后,MySQL数据库默认存放一个information_schema数据库) information_schema的三个表: SCHEMAT ...