Vue拖拽组件
vue开发公众号项目,***产品需要添加一个新的功能。拖拽功能。一听简单。百度上轮子挺多,直接拉一个过来用着就行。然鹅。。。兴奋之余,却失望至极。东西很多,没有一个能使得。你让我失望,那我就让你绝望。于是,拖拽的故事就开始了。。
vue拖拽功能
必备知识点:
先给不懂的童鞋补充下流程,文章要细读方能拖动元素到你心里~
按下的时候,我们需要获取
元素当前的 具有相对定位元素的左侧距离
元素当前的具有相对定位元素的顶部距离
鼠标按下点的x轴距离(鼠标左侧的距离)
鼠标按下点的y轴距离 (鼠标顶部的距离)
获取到这些点,先存储起来,后面的计算需要用到这些值
start(e){
// 如果touches存在就说明是移动端
// 否则为pc端直接获取事件源对象
let touch = e.touches? e.touches[0] : e;
this.position.x = touch.clientX;
this.position.y = touch.clientY;
this.dx = moveDiv.offsetLeft;
this.dy = moveDiv.offsetTop;
}
Step1.
让元素跟着鼠标的移动不断移动。既然鼠标的x轴和y轴可以获取到,那我们就可以通过计算来让元素实现移动。
移动的时候,我们需要获取并设置
鼠标此时的当前的x轴和y轴的距离
鼠标点击的x轴和y轴的距离(按下的时候定义过)
此时用移动的距离 - 点击的起始位置就是移动的距离。
赋值给点击元素的left和top即可。
补充:计算的方式很多种,这知识其中一种
move(e){
let touch = e.touches? e.touches[0] : e;
this.nx = touch.clientX - this.position.x;
this.ny = touch.clientY - this.position.y;
this.xPum = this.dx+this.nx;
this.yPum = this.dy+this.ny;
moveDiv.style.left = this.xPum + "px";
moveDiv.style.top = this.yPum + "px";
},
Step2.
离开的时候,我们需要抬起和移动事件从栈中清除掉,并且在结束时对边界做一个处理。不让元素拖动到屏幕外面,否则的话,不小心拖出去了,拉都拉不回来。这就很尴尬了。
元素的宽度
父元素的宽度和高度
元素的左侧距离 + 元素的宽度
元素的顶部距离 + 元素的高度
end(e){
let oWidth = moveDiv.offsetWidth; // Element Width
let oWrapWidth = moveDiv.parentNode.offsetWidth; // Parent Element Width
let oWrprapHeight = moveDiv.parentNode.offsetHeight; // Parent Element Height
let sumWidth = moveDiv.offsetLeft + oWidth; // Element Left + Element Width
let sumHeight = moveDiv.offsetTop + moveDiv.offsetHeight; // Element Top + Element Height
// The Limit Deal
if(moveDiv.offsetLeft < 0) {
moveDiv.style.left = 0;
} else if(sumWidth > oWrapWidth){
moveDiv.style.left = oWrapWidth - oWidth + 'px';
} else if(moveDiv.offsetTop < 0) {
moveDiv.style.top = 0;
} else if(sumHeight > oWrprapHeight) {
moveDiv.style.top = oWrprapHeight - moveDiv.offsetHeight + 'px';
}
document.onmousemove = null;
document.onmouseup = null;
}
组件源码
考虑到复用性,pc和移动端。
<template>
<!--S 拖动组件 -->
<div class="drag" id="moveDiv"
@mousedown="start($event)" @touchstart="start($event)"
@mousemove="move($event)" @touchmove="move($event)"
@mouseup="end($event)" @touchend="end($event)">
<slot name="drag-cont"></slot>
</div><!--E 拖动组件 -->
</template>
<script>
export default {
data() {
return {
position: {x: 0,y: 0}, // 鼠标点击的x轴和y轴的距离
nx: '', // 鼠标当前距离元素的左侧距离
ny: '', // 鼠标当前距离元素的顶部距离
dx: '', // 元素距离左侧的距离
dy: '', // 元素距离顶部的距离
xPum: '', // 元素移动的x轴距离
yPum: '', // 元素移动的y轴距离
}
},
methods: {
start(e){
// 如果touches存在就说明是移动端
// 否则为pc端直接获取事件源对象
let touch = e.touches? e.touches[0] : e;
this.position.x = touch.clientX;
this.position.y = touch.clientY;
this.dx = moveDiv.offsetLeft;
this.dy = moveDiv.offsetTop;
},
move(e){
let touch = e.touches? e.touches[0] : e;
this.nx = touch.clientX - this.position.x;
this.ny = touch.clientY - this.position.y;
this.xPum = this.dx+this.nx;
this.yPum = this.dy+this.ny;
moveDiv.style.left = this.xPum + "px";
moveDiv.style.top = this.yPum + "px";
document.addEventListener("touchmove",function(){
event.preventDefault();
},false);
if(e.preventDefault){
e.preventDefault();
}else{
window.event.returnValue == false;
}
},
end(e){
let oWidth = moveDiv.offsetWidth; // Element Width
let oWrapWidth = moveDiv.parentNode.offsetWidth; // Parent Element Width
let oWrprapHeight = moveDiv.parentNode.offsetHeight; // Parent Element Height
let sumWidth = moveDiv.offsetLeft + oWidth; // Element Left + Element Width
let sumHeight = moveDiv.offsetTop + moveDiv.offsetHeight; // Element Top + Element Height
// The Limit Deal
if(moveDiv.offsetLeft < 0) {
moveDiv.style.left = 0;
} else if(sumWidth > oWrapWidth){
moveDiv.style.left = oWrapWidth - oWidth + 'px';
} else if(moveDiv.offsetTop < 0) {
moveDiv.style.top = 0;
} else if(sumHeight > oWrprapHeight) {
moveDiv.style.top = oWrprapHeight - moveDiv.offsetHeight + 'px';
}
document.onmousemove = null;
document.onmouseup = null;
}
}
}
</script>
<style lang="less" scoped>
.drag {
position: absolute;
left: 0;
right: 0;
z-index: 999;
}
</style>
引入Demo
<Drag class="drag">
<div slot="drag-cont">订单记录</div>
</Drag>
<style>
.drag {
width: .6rem;
height: .6rem;
background-color: rgba(0, 0, 0,.55);
text-align: center;
line-height: .6rem;
font-size: .14rem;
color: #ffffff;
}
</style>
使用的时候,给组件添加类名,添加样式即可。
Vue拖拽组件的更多相关文章
- vue拖拽组件开发
vue拖拽组件开发 创建临时vue项目 先查看node和npm版本,怎么安装就不多多bb了 再安装vue-cli npm install vue-cli -g //全局安装 vue-cli 检测是否安 ...
- Vue.Draggable:基于 Sortable.js 的 Vue 拖拽组件使用中遇到的问题
Sortable.js 介绍 https://segmentfault.com/a/1190000008209715 项目中遇到的问题: A - 我需要在项目的拖拽组件中,使用背景 1 - 想到的第一 ...
- Vue 拖拽组件 vuedraggable 和 vue-dragging
一.描述 之前用 vue 写过一个在线的多二维码生成服务,体验地址:https://postbird.gitee.io/vue-online-qrcode/ 后面发现二维码多了之后有时候想要排序,需要 ...
- vue2-dragula vue拖拽组件
https://github.com/kristianmandrup/vue2-dragula git 地址 https://github.com/kristianmandrup/vue2-dragu ...
- Vue拖拽组件列表实现动态页面配置
需求描述 最近在做一个后台系统,有一个功能产品需求是页面分为左右两部分,通过右边的组件列表来动态配置左边的页面视图,并且左边由组件拼装起来的视图,可以实现上下拖拽改变顺序,也可以删除. 根据这个需求我 ...
- Vue 拖拽组件 vuedraggable 和 awe-dnd
vuedraggable:https://www.npmjs.com/package/vuedraggable awe-dnd:https://www.npmjs.com/package/awe-dn ...
- Vue 拖拽组件 vuedraggable 、 vue-dragging 、awe-dnd
参考链接:http://www.ptbird.cn/vue-draggable-dragging.html vue-draggable 学习和使用:https://www.jianshu.com/p/ ...
- Vue 可拖拽组件 Vue Smooth DnD 详解和应用演示
本文发布自 https://www.cnblogs.com/wenruo/p/15061907.html 转载请注明出处. 简介和 Demo 展示 最近需要有个拖拽列表的需求,发现一个简单好用的 Vu ...
- vue-slicksort拖拽组件
vue-slicksort拖拽组件 安装 通过npm安装 $ npm install vue-slicksort --save 通过yarn安装 $ yarn add vue-slicksort 插件 ...
随机推荐
- Phpstorm配置scss不生成缓存
--no-cache 加上这个,就不会生成 .sass-cache 文件夹了.
- Blue_Flke团队项目设计完善&编码测试
任务1:文档<软件设计方案说明书>github地址:https://github.com/13993013291/ruanjianguigexuqiu 任务2:项目集成开发环境:eclip ...
- 关于React性能优化
这几天陆陆续续看了一些关于React性能优化的博客,大部分提到的都是React 15.3新加入的PureComponent ,通过使用这个类来减少React的重复渲染,从而提升页面的性能.使用过Rea ...
- 滑动窗口解决Substring Search Problem
2018-07-18 11:19:19 一.Minimum Window Substring 问题描述: 问题求解: public String minWindow(String s, String ...
- C语言的的free和c++的delete的区别
首先free对应的是malloc:delete对应的是new:free用来释放malloc出来动态内存,delete用来释放new出来的动态内存空间. 应用的区别为: 1. 数组的时候int *p=( ...
- rsync+inotify文件同步
rsync+inotify文件同步 在服务器中,通常结合计划任务.shell脚本来执行本地备份.为了进一步提高备份的可靠性,使用异地备份也是非常重要的,利用rsync工具,可以实现快速.高效的异地备份 ...
- 在WinForm应用程序中嵌入WPF控件
我们知道,在WPF界面上添加WinForm的控件需要使用WindowsFormHost类.而在WinForm界面上添加WPF控件该如何做呢?有没有类似的类呢?明显是有的,ElementHost就是为了 ...
- hdu-1404-博弈+打表
Digital Deletions Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- ORACLE11G内存管理参数
今天,对ORACLE11G的几个内存参数看了一下,记录如下,大家可以参考: 1.首先,在ORACLE11G的INIT.ORA里,有“__”开头的参数,也就是以两个下划线开头的参数,这种参数应该是系统自 ...
- install rabbitvcs in ubuntu16.04
reference: https://github.com/rabbitvcs/rabbitvcs how to install : sudo apt-get install rabbitvcs-cl ...