基于Vue如何实现拖拽悬浮按钮,代码是什么
Admin 2022-08-06 群英技术资讯 1498 次浏览
这篇文章给大家分享的是基于Vue如何实现拖拽悬浮按钮,代码是什么。小编觉得挺实用的,因此分享给大家做个参考,文中的介绍得很详细,而要易于理解和学习,有需要的朋友可以参考,接下来就跟随小编一起了解看看吧。在移动端开发中,实现悬浮按钮在侧边显示,为不遮挡页面内容,允许手指拖拽换位。
1、按钮在页面侧边悬浮显示;
2、手指长按按钮,按钮改变样式,允许拖拽改变位置;
3、按钮移动结束,手指松开,计算距离左右两侧距离并自动移动至侧边显示;
4、移动至侧边后,按钮根据具体左右两次位置判断改变现实样式;
1、按钮实行position:fixed布局,在页面两侧最上层悬浮显示;
2、手指长按可使用定时器来判断,若手指松开,则关闭定时器,等待下次操作再启用;
3、跟随手指移动计算按钮与页面两侧的距离,判断手指松开时停留的位置;
简单效果展示:



使用定位实现
<!-- 外层ul控制卡片范围 -->
<div>
<div class="floatBtn"
:class="[{moveBtn: longClick}, `${btnType}Btn`]">
<span>悬浮按钮</span>
</div>
</div>
<style lang="scss" scoped>
@mixin notSelect{
-moz-user-select:none; /*火狐*/
-webkit-user-select:none; /*webkit浏览器*/
-ms-user-select:none; /*IE10*/
-khtml-user-select:none; /*早期浏览器*/
user-select:none;
}
@mixin not-touch {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
.floatBtn {
@include notSelect;
@include not-touch();
position: fixed;
z-index: 1;
overflow: hidden;
width: 100px;
left: calc(100% - 100px);
top: calc(100% - 100px);
color: #E0933A;
background: #FCEBD0;
font-size: 14px;
height: 36px;
line-height: 36px;
text-align: center;
box-sizing: border-box;
display: flex;
justify-content: center;
align-items: center;
padding: 10px;
&.rightBtn {
border-radius: 20px 0 0 20px;
}
&.leftBtn {
border-radius: 0 20px 20px 0;
}
&.moveBtn {
border-radius: 20px;
}
}
</style>
应用到touchstart,touchmove,touchend事件,使用定时器实现长按效果:
<div class="floatBtn"
:class="[{moveBtn: longClick}, `${btnType}Btn`]"
@touchstart="touchstart($event)"
@touchmove="touchMove($event)"
@touchend="touchEnd($event)"
>
<span>悬浮按钮</span>
</div>
<script>
export default {
data() {
return {
timeOutEvent: 0,
longClick: 0,
// 手指原始位置
oldMousePos: {},
// 元素原始位置
oldNodePos: {},
btnType: 'right'
};
},
touchstart(ev) {
// 定时器控制长按时间,超过500毫秒开始进行拖拽
this.timeOutEvent = setTimeout(() => {
this.longClick = 1;
}, 500);
const selectDom = ev.currentTarget;
const { pageX, pageY } = ev.touches[0]; // 手指位置
const { offsetLeft, offsetTop } = selectDom; // 元素位置
// 手指原始位置
this.oldMousePos = {
x: pageX,
y: pageY
};
// 元素原始位置
this.oldNodePos = {
x: offsetLeft,
y: offsetTop
};
selectDom.style.left = `${offsetLeft}px`;
selectDom.style.top = `${offsetTop}px`;
},
touchMove(ev) {
// 未达到500毫秒就移动则不触发长按,清空定时器
clearTimeout(this.timeOutEvent);
if (this.longClick === 1) {
const selectDom = ev.currentTarget;
// x轴偏移量
const lefts = this.oldMousePos.x - this.oldNodePos.x;
// y轴偏移量
const tops = this.oldMousePos.y - this.oldNodePos.y;
const { pageX, pageY } = ev.touches[0]; // 手指位置
selectDom.style.left = `${pageX - lefts}px`;
selectDom.style.top = `${pageY - tops}px`;
}
},
touchEnd(ev) {
// 清空定时器
clearTimeout(this.timeOutEvent);
if (this.longClick === 1) {
this.longClick = 0;
const selectDom = ev.currentTarget;
const {clientWidth, clientHeight} = document.body;
const {offsetLeft, offsetTop} = selectDom;
selectDom.style.left =
(offsetLeft + 50) > (clientWidth / 2) ?
'calc(100% - 100px)' : 0;
if (offsetTop < 90) {
selectDom.style.top = '90px';
} else if (offsetTop + 36 > clientHeight) {
selectDom.style.top = `${clientHeight - 36}px`;
}
this.btnType =
(offsetLeft + 50) > (clientWidth / 2) ?
'right' : 'left';
}
},
};
</script>
单个页面引入
<template> <floatBtn/> </template>
<script>
import floatBtn from './floatBtn';
export default {
components: {
floatBtn
},
};
</script>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:mmqy2019@163.com进行举报,并提供相关证据,查实之后,将立刻删除涉嫌侵权内容。
猜你喜欢
中国省市区三级联动Jquery插件经常被使用到,不管是网站还是app,这个都是需要掌握的,我们知道中国省市区三级联动的数据可以存储在mysql数据库中,也可以直接存储在distpicker.data.js这样的js文件中,下面来开始学习如何使用Jquery省市区三级联动插件。 下
Vue 最独特的特性之一,是其非侵入性的响应式系统。下面这篇文章主要给大家介绍了关于vue响应式原理与双向数据的相关资料,需要的朋友可以参考下
本篇文章带大家深入了解Angular中的依赖注入模式,分享依赖注入模式的应用和玩法案例,希望对大家有所帮助!
斐波那契数列来源于兔子繁殖问题,所以也叫兔子序列,下面这篇文章主要给大家介绍了关于JavaScript输出斐波那契数列的实现方法,需要的朋友可以参考下
JS实现简单留言板功能 本文实例为大家分享了JS实现简单留言板的具体代码,供大家参考,具体内容如下 言归正传,之前的案例相信大家都已经完全弄清楚了,还记得我们之前统计字数的那个案例吗?忘记的可以再去翻阅一下,今天就是在这个方法的基础上,把它变成一个留言版,就像我们之前的评论一样,是不是很期待呢?先来看一下效果图 输入昵称,选择头像,输入留言,点击广播就能够在下面显示出来了,是不是很nice呢,具体怎么实现的呢,我们来看代码 <!DOCTYPE html> <html lang="en&quo ...
成为群英会员,开启智能安全云计算之旅
立即注册关注或联系群英网络
7x24小时售前:400-678-4567
7x24小时售后:0668-2555666
24小时QQ客服
群英微信公众号
CNNIC域名投诉举报处理平台
服务电话:010-58813000
服务邮箱:service@cnnic.cn
投诉与建议:0668-2555555
Copyright © QY Network Company Ltd. All Rights Reserved. 2003-2020 群英 版权所有
增值电信经营许可证 : B1.B2-20140078 ICP核准(ICP备案)粤ICP备09006778号 域名注册商资质 粤 D3.1-20240008