vue实现移动端触屏拖拽功能


Posted in Javascript onAugust 21, 2020

vue实现移动端可拖拽浮球,供大家参考,具体内容如下

1 首先创建一个div

<div class="floatball" id="floatball"
 @mousedown="down" @touchstart.stop="down"
 @mousemove="move" @touchmove.stop="move"
 @mouseup="end" @touchend.stop="end" @click="showRewardDesc"
 :style="{top:position.y+'px', left:position.x+'px'}">
 奖励规则
</div>

2 给 div 附上样式

<style>
 .floatball{
 color:white;
 height:50px;
 width: 50px;
 padding: 5px;
 z-index: 990;
 position: fixed;
 top: 60px;
 right: 320px;
 border-radius: 50%;
 background-color: rgba(29, 157, 237,0.8);
 }

</style>

3 给 div 附上事件

准备四个变量

1)、屏幕长

var screenHeight = window.screen.height

2)、屏幕宽

var screenWidth = window.screen.width

3)、初始触控点 距离 div 左上角的横向距离 dx

4)、初始触控点 距离 div 左上角的竖向距离 dy

vue实现移动端触屏拖拽功能

在开始拖拽时,计算出鼠标点(初始触控点)和 div左上角顶点的距离

down(event){
 this.flags = true;
 var touch ;
 if(event.touches){
 touch = event.touches[0];
 }else {
 touch = event;
 }
 console.log('鼠标点所在位置', touch.clientX,touch.clientY)
 console.log('div左上角位置', event.target.offsetTop,event.target.offsetLeft)
 dx = touch.clientX - event.target.offsetLeft
 dy = touch.clientY - event.target.offsetTop
},

拖拽进行时,将触控点的位置赋值给 div

// 定位滑块的位置
this.position.x = touch.clientX - dx;
this.position.y = touch.clientY - dy;
// 限制滑块超出页面
// console.log('屏幕大小', screenWidth, screenHeight)
if (this.position.x < 0) {
 this.position.x = 0
} else if (this.position.x > screenWidth - touch.target.clientWidth) {
 this.position.x = screenWidth - touch.target.clientWidth
}
if (this.position.y < 0) {
 this.position.y = 0
} else if (this.position.y > screenHeight - touch.target.clientHeight) {
 this.position.y = screenHeight - touch.target.clientHeight
}

拖拽结束

//鼠标释放时候的函数
end(){
 console.log('end')
 this.flags = false;
},

全部代码

<template>
 <div class="floatball" id="floatball"
 @mousedown="down" @touchstart.stop="down"
 @mousemove="move" @touchmove.stop="move"
 @mouseup="end" @touchend.stop="end"
 :style="{top:position.y+'px', left:position.x+'px'}">
 奖励规则
 </div>
</template>

<script>
// 鼠标位置和div的左上角位置 差值
var dx,dy
var screenWidth = window.screen.width
var screenHeight = window.screen.height

export default {
 data() {
 return {
 flags: false,
 position: {
 x: 320,
 y: 60
 },
 }
 },
 

 methods: {
 // 实现移动端拖拽
 down(event){
 this.flags = true;
 var touch ;
 if(event.touches){
 touch = event.touches[0];
 }else {
 touch = event;
 }
 console.log('鼠标点所在位置', touch.clientX,touch.clientY)
 console.log('div左上角位置', event.target.offsetTop,event.target.offsetLeft)
 dx = touch.clientX - event.target.offsetLeft
 dy = touch.clientY - event.target.offsetTop
 },
 move() {
 if (this.flags) {
 var touch ;
 if (event.touches) {
 touch = event.touches[0];
 } else {
 touch = event;
 }
 // 定位滑块的位置
 this.position.x = touch.clientX - dx;
 this.position.y = touch.clientY - dy;
 // 限制滑块超出页面
 // console.log('屏幕大小', screenWidth, screenHeight )
 if (this.position.x < 0) {
 this.position.x = 0
 } else if (this.position.x > screenWidth - touch.target.clientWidth) {
 this.position.x = screenWidth - touch.target.clientWidth
 }
 if (this.position.y < 0) {
 this.position.y = 0
 } else if (this.position.y > screenHeight - touch.target.clientHeight) {
 this.position.y = screenHeight - touch.target.clientHeight
 }
 //阻止页面的滑动默认事件
 document.addEventListener("touchmove",function(){
 event.preventDefault();
 },false);
 }
 },
 //鼠标释放时候的函数
 end(){
 console.log('end')
 this.flags = false;
 },

 }
 
}
</script>

<style>
 .floatball{
 color:white;
 height:50px;
 width: 50px;
 padding: 5px;
 z-index: 990;
 position: fixed;
 top: 60px;
 right: 320px;
 border-radius: 50%;
 background-color: rgba(29, 157, 237,0.8);
 }

</style>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
JavaScript中的几个关键概念的理解-原型链的构建
May 12 Javascript
ie下jquery.getJSON的缓存问题的处理方法
Mar 29 Javascript
利用javascript判断文件是否存在
Dec 31 Javascript
js选项卡的实现方法
Feb 09 Javascript
js限制文本框只能输入中文的方法
Aug 11 Javascript
jquery.cookie.js用法实例详解
Dec 25 Javascript
jQuery改变form表单的action,并进行提交的实现代码
May 25 Javascript
Javascript的动态增加类的实现方法
Oct 20 Javascript
微信小程序 PHP后端form表单提交实例详解
Jan 12 Javascript
vue input输入框关键字筛选检索列表数据展示
Oct 26 Javascript
微信小程序简单的canvas裁剪图片功能详解
Jul 12 Javascript
详解Javascript实践中的命令模式
May 05 Javascript
vue实现移动端拖动排序
Aug 21 #Javascript
微信小程序实现聊天室
Aug 21 #Javascript
vue实现折线图 可按时间查询
Aug 21 #Javascript
Vue按时间段查询数据组件使用详解
Aug 21 #Javascript
js绘制一条直线并旋转45度
Aug 21 #Javascript
AJAX XMLHttpRequest对象创建使用详解
Aug 20 #Javascript
基于vue.js仿淘宝收货地址并设置默认地址的案例分析
Aug 20 #Javascript
You might like
php环境套包 dedeampz 伪静态设置示例
2014/03/26 PHP
ThinkPHP3.2.2实现持久登录(记住我)功能的方法
2016/05/16 PHP
自定义右键属性覆盖浏览器默认右键行为实现代码
2013/02/02 Javascript
sogou地图API用法实例教程
2014/09/11 Javascript
js实现的奥运倒计时时钟效果代码
2015/12/09 Javascript
JS小数运算出现多为小数问题的解决方法
2016/06/02 Javascript
JavaScript简单获取页面图片原始尺寸的方法
2016/06/21 Javascript
详谈$.data()的用法和作用
2017/02/13 Javascript
深入理解Angular4中的依赖注入
2017/06/07 Javascript
vue项目总结之文件夹结构配置详解
2017/12/13 Javascript
在axios中使用params传参的时候传入数组的方法
2018/09/25 Javascript
layer.confirm()右边按钮实现href的例子
2019/09/27 Javascript
[46:43]DOTA2上海特级锦标赛D组小组赛#1 EG VS COL第三局
2016/02/28 DOTA
用Python实现换行符转换的脚本的教程
2015/04/16 Python
PyMongo安装使用笔记
2015/04/27 Python
总结python爬虫抓站的实用技巧
2016/08/09 Python
python 对象和json互相转换方法
2018/03/22 Python
YUV转为jpg图像的实现
2019/12/09 Python
PyTorch的SoftMax交叉熵损失和梯度用法
2020/01/15 Python
python同义词替换的实现(jieba分词)
2020/01/21 Python
Pycharm激活码激活两种快速方式(附最新激活码和插件)
2020/03/12 Python
详解Ubuntu环境下部署Django+uwsgi+nginx总结
2020/04/02 Python
keras 如何保存最佳的训练模型
2020/05/25 Python
Python3爬虫里关于代理的设置总结
2020/07/30 Python
pyqt5 textEdit、lineEdit操作的示例代码
2020/08/12 Python
基于Python爬取股票数据过程详解
2020/10/21 Python
开朗女孩的自我评价
2014/02/10 职场文书
教师党员岗位承诺书
2014/05/29 职场文书
邓小平理论心得体会
2014/09/09 职场文书
公安纪律作风整顿剖析材料
2014/10/10 职场文书
2014年局领导班子自身建设情况汇报
2014/11/21 职场文书
2014年助理政工师工作总结
2014/12/19 职场文书
国庆庆典邀请函
2015/02/02 职场文书
pytorch 一行代码查看网络参数总量的实现
2021/05/12 Python
python中pd.cut()与pd.qcut()的对比及示例
2022/06/16 Python
本地搭建minio文件服务器(使用bat脚本启动)的方法
2022/07/15 Servers