vue 拖拽移动(类似于iPhone虚拟home )
vue 移动端 PC 兼容 元素 拖拽移动
效果演示
事件知识点
移动端 | PC端 | 注释 |
touchstart | mousedown | 鼠标/手指按下事件 |
touchmove | mousemove | 鼠标/手指移动事件 |
touchend | mouseup | 鼠标松开/手指抬起事件 |

<template lang="pug">
//- 当前页面全局容器
div.SchemeDetail(
ref='pageDiv'
@mousemove="onmousemove($event)" @touchmove="onmousemove($event)"
@mouseup="onmouseup($event)" @touchend="onmouseup($event)"
)
//- 加号拖拽元素
div.action-mgr(:class="{ active :mgrState}"
ref='actionMgr'
@click="mgrState=!mgrState"
@mousedown="onmousedown($event)" @touchstart="onmousedown($event)")
<i class="icon iconfont icon-jia"></i>
.....
</template> <script>
export default {
data() {
return {
mgrState: false,
mousedownState: false, //鼠标默认抬起
iX: 0,//鼠标坐标 与 拖拽按钮 间距 x
iY: 0//鼠标坐标 与 拖拽按钮 间距 y
};
},
methods: {
send() {
console.log("send");
},
edit() {
console.log("edit");
},
/* 鼠标按下事件 */
onmousedown(event) {
/* 此处判断 pc 或 移动端 得到 event 事件 */
var touch;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
// 鼠标点击 面向页面 的 x坐标 y坐标
let { clientX, clientY } = touch;
// 鼠标x坐标 - 拖拽按钮x坐标 得到鼠标 距离 拖拽按钮 的间距
this.iX = clientX - this.$refs.actionMgr.offsetLeft;
// 鼠标y坐标 - 拖拽按钮y坐标 得到鼠标 距离 拖拽按钮 的间距
this.iY = clientY - this.$refs.actionMgr.offsetTop;
// 设置当前 状态为 鼠标按下
this.mousedownState = true;
},
/* 鼠标移动事件 */
onmousemove(event) {
//鼠标按下 切移动中
if (this.mousedownState) {
/* 此处判断 pc 或 移动端 得到 event 事件 */
var touch;
if (event.touches) {
touch = event.touches[0];
} else {
touch = event;
}
// 鼠标移动时 面向页面 的 x坐标 y坐标
let { clientX, clientY } = touch;
//当前页面全局容器 dom 元素 获取容器 宽高
let {
clientHeight: pageDivY,
clientWidth: pageDivX
} = this.$refs.pageDiv;
/* 鼠标坐标 - 鼠标与拖拽按钮的 间距坐标 得到 拖拽按钮的 左上角 x轴y轴坐标 */
let [x, y] = [clientX - this.iX, clientY - this.iY]; //拖拽按钮 dom 元素 获取 宽高 style 对象
let {
clientHeight: actionMgrY,
clientWidth: actionMgrX,
style: actionMgrStyle
} = this.$refs.actionMgr;
/* 此处判断 拖拽按钮 如果超出 屏幕宽高 或者 小于
设置 屏幕最大 x=全局容器x y=全局容器y 否则 设置 为 x=0 y=0
*/
if (x > pageDivX - actionMgrX) x = pageDivX - actionMgrX;
else if (x < 0) x = 0;
if (y > pageDivY - actionMgrY) y = pageDivY - actionMgrY;
else if (y < 0) y = 0;
// 计算后坐标 设置 按钮位置
actionMgrStyle.left = `${x}px`;
actionMgrStyle.top = `${y}px`;
actionMgrStyle.bottom = "auto";
actionMgrStyle.right = "auto";
//当按下键滑动时, 阻止屏幕滑动事件
event.preventDefault();
}
},
/* 鼠标抬起事件 */
onmouseup(event) {
// 设置当前状态为鼠标抬起
this.mousedownState = false;
}
}
};
</script>
vue 拖拽移动(类似于iPhone虚拟home )的更多相关文章
- Vue拖拽组件
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拖拽插件(弹框拖拽)
// =======拖拽 插件 cnpm install vuedraggableimport draggable from 'vuedraggable' <draggable v-model= ...
- Vue拖拽交换数据(非插件)
HelloWorld.vue 文件 <template> <div class="hello"> <h1>{{ msg }}</h1> ...
- 一个全新的Vue拖拽特性实现:“调整尺寸”部分
关于拖拽 CabloyJS提供了完备的拖拽特性,可以实现移动和调整尺寸两大类功能,这里对调整尺寸的开发进行阐述 关于移动的开发,请参见:拖拽:移动 演示 开发步骤 下面以模块test-party为例, ...
- 一个全新的Vue拖拽特性实现:“移动”部分
关于拖拽 CabloyJS提供了完备的拖拽特性,可以实现移动和调整尺寸两大类功能,这里对移动的开发进行阐述 关于调整尺寸的开发,请参见:拖拽:调整尺寸 演示 开发步骤 下面以模块test-party为 ...
随机推荐
- document.getElementById(), getElementsByname(),getElementsByClassName(),getElementsByTagName()方法表示什么以及其意义
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- 谷歌浏览器扩展程序manifest.json参数详解
{ // Required "manifest_version": 2, // manifest编写规范版本,目前主流2 "name": "My Ex ...
- windows蓝屏代码
原始链接 引用自 https://docs.microsoft.com/zh-cn/windows-hardware/drivers/debugger/bug-check-code-referenc ...
- iOS----------使用cocoapods遇到的问题
-bash: /usr/local/bin/pod: /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/ruby: bad ...
- ORACLE中如何查找定位表最后DML操作的时间小结
在Oracle数据库中,如何查找,定位一张表最后一次的DML操作的时间呢? 方式有三种,不过都有一些局限性,下面简单的解析.总结一下. 1:使用ORA_ROWSCN伪列获取表最后的DML时间 ORA_ ...
- Python大数据系列-01-关系数据库基本运算
关系数据库基本运算 .tg {border-collapse:collapse;border-spacing:0;} .tg td{font-family:Arial, sans-serif;font ...
- SpringMVC相关常用注解
@Controller: @Controller 用于标记在一个类上,使用它标记的类就是一个SpringMVC Controller 对象 @RequestMapping: RequestMappin ...
- LeetCode算法题-Trim a Binary Search Tree(Java实现)
这是悦乐书的第284次更新,第301篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第152题(顺位题号是669).给定二叉搜索树以及L和R的最低和最高边界,修剪树以使其所 ...
- Error:Cannot run program "svn" (in directory "E:demo\Hello"): CreateProcess error=2,
file-->settings-->version controller --> subversion
- 软件设计之Deep Module(深模块)
类是不是越小越好?最近在读John Ousterhout的<A Philosophy of Software Design>,感到作者文笔流畅,书中内容具有启发性.这里摘要一部分内容,以供 ...