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 30 Javascript
JS判断表单输入是否为空(示例代码)
Dec 23 Javascript
JavaScript数组函数unshift、shift、pop、push使用实例
Aug 27 Javascript
js实现点击获取验证码倒计时效果
Jan 28 Javascript
解决JavaScript数字精度丢失问题的方法
Dec 03 Javascript
分享Javascript实用方法二
Dec 13 Javascript
KnockoutJs快速入门教程
May 16 Javascript
Bootstrap中glyphicons-halflings-regular.woff字体报404错notfound的解决方法
Jan 19 Javascript
vue以组件或者插件的形式实现throttle或者debounce
May 22 Javascript
vue项目中实现缓存的最佳方案详解
Jul 11 Javascript
Vue 动态组件components和v-once指令的实现
Aug 30 Javascript
vue基础知识--axios合并请求和slot
Jun 04 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中常用的预定义变量小结
2012/05/09 PHP
PHP中echo和print的区别
2014/08/28 PHP
合格的PHP程序员必备技能
2015/11/13 PHP
yii实现model添加默认值的方法(2种方法)
2016/01/06 PHP
PHP实现大数(浮点数)取余的方法
2017/02/18 PHP
详解PHP实现支付宝小程序用户授权的工具类
2018/12/25 PHP
PHP PDOStatement::fetchObject讲解
2019/02/01 PHP
javascript 常用方法总结
2009/06/03 Javascript
jquery 注意事项与常用语法小结
2010/06/07 Javascript
原生javascript实现图片轮播效果代码
2010/09/03 Javascript
js写一个字符串转成驼峰的实例
2013/06/21 Javascript
Extjs单独定义各组件的实例代码
2013/06/25 Javascript
jQuery focus和blur事件的应用详解
2014/01/26 Javascript
javascript的push使用指南
2014/12/05 Javascript
JavaScript 不支持 indexof 该如何解决
2016/03/30 Javascript
javascript数组去重常用方法实例分析
2017/04/11 Javascript
基于jQuery封装的分页组件
2017/06/26 jQuery
微信小程序联网请求的轮播图
2017/07/07 Javascript
Vue单页应用引用单独的样式文件的两种方式
2018/03/30 Javascript
vue.js 实现评价五角星组件的实例代码
2018/08/13 Javascript
微信小程序图片右边加两行文字的代码
2020/04/23 Javascript
Django csrf 验证问题的实现
2018/10/09 Python
HTML5时代CSS设置漂亮字体取代图片
2014/09/04 HTML / CSS
美国娱乐和流行文化商品店:FYE
2017/09/14 全球购物
Nike荷兰官方网站:Nike.com (NL)
2018/04/19 全球购物
全球性的奢侈品梦工厂:Forzieri(福喜利)
2019/02/20 全球购物
电子商务专员岗位职责
2013/12/11 职场文书
留学推荐信写作指南
2014/01/25 职场文书
教师申诉制度
2014/01/29 职场文书
《爱如茉莉》教后反思
2014/04/12 职场文书
书香校园建设方案
2014/05/02 职场文书
爱护公共设施标语
2014/06/24 职场文书
个人安全生产责任书
2014/07/28 职场文书
活动费用申请报告
2015/05/15 职场文书
详解MongoDB排序时内存大小限制与创建索引的注意事项
2022/05/06 MongoDB
Android中的Launch Mode详情
2022/06/05 Java/Android