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 相关文章推荐
JQuery.Ajax之错误调试帮助信息介绍
Jul 04 Javascript
js对字符的验证方法汇总
Feb 04 Javascript
jquery实现LED广告牌旋转系统图片切换效果代码分享
Aug 26 Javascript
javascript类型系统——undefined和null全面了解
Jul 13 Javascript
利用原生js和jQuery实现单选框的勾选和取消操作的方法
Sep 04 Javascript
最全面的JS倒计时代码
Sep 17 Javascript
微信小程序实现简单input正则表达式验证功能示例
Nov 30 Javascript
使用js实现将后台传入的json数据放在前台显示
Aug 06 Javascript
javascript的this关键字详解
May 20 Javascript
layui关闭弹窗后刷新主页面和当前更改项的例子
Sep 06 Javascript
解决layui数据表格排序图标被超出的表头挤出去的问题
Sep 19 Javascript
浅析vue-router中params和query的区别
Dec 24 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调用mysql数据 dbclass类
2011/05/07 PHP
PHPAnalysis中文分词类详解
2014/06/13 PHP
php判断数组中是否存在指定键(key)的方法
2015/03/17 PHP
PHP实现远程下载文件到本地
2015/05/17 PHP
浅谈php7的重大新特性
2015/10/23 PHP
使用composer安装使用thinkphp6.0框架问题【视频教程】
2019/10/01 PHP
Thinkphp5.0框架视图view的模板布局用法分析
2019/10/12 PHP
论坛特效代码收集(落伍转发-不错)
2006/12/02 Javascript
JavaScript delete操作符应用实例
2009/01/13 Javascript
js Dialog 实践分享
2012/10/22 Javascript
自动刷新网页,自动刷新当前页面,JS调用
2013/06/24 Javascript
jQuery中toggleClass()方法用法实例
2015/01/05 Javascript
jQuery中extend函数详解
2015/02/13 Javascript
浅谈js的url解析函数封装
2016/06/28 Javascript
javascript特效实现——当前时间和倒计时效果的简单实例
2016/07/20 Javascript
jQuery实现删除li节点的方法
2016/12/06 Javascript
JavaScript基于Dom操作实现查找、修改HTML元素的内容及属性的方法
2017/01/20 Javascript
微信公众号菜单配置微信小程序实例详解
2017/03/31 Javascript
微信小程序微信支付接入开发实例详解
2017/04/12 Javascript
在vue-cli项目中使用bootstrap的方法示例
2018/04/21 Javascript
spring+angular实现导出excel的实现代码
2019/02/27 Javascript
微信小程序使用自定义组件导航实现当前页面高亮
2020/01/02 Javascript
Websocket 向指定用户发消息的方法
2020/01/09 Javascript
python数据结构之二叉树的统计与转换实例
2014/04/29 Python
离线安装Pyecharts的步骤以及依赖包流程
2020/04/23 Python
Python3实现抓取javascript动态生成的html网页功能示例
2017/08/22 Python
使用OpenCV实现道路车辆计数的使用方法
2020/07/15 Python
使用bandit对目标python代码进行安全函数扫描的案例分析
2021/01/27 Python
法国时尚品牌乐都特瑞士站:La Redoute瑞士
2016/09/05 全球购物
高中政治教学反思
2014/01/18 职场文书
施工协议书范本
2014/04/22 职场文书
促销活动计划书
2014/05/02 职场文书
义和团口号
2014/06/17 职场文书
中学生逃课检讨书
2015/02/17 职场文书
护士实习自荐信
2015/03/06 职场文书
电脑开机弹出documents文件夹怎么回事?弹出documents文件夹解决方法
2022/04/08 数码科技