vue2 - 实现网页悬浮窗鼠标可拖拽和靠边隐藏功能,可拖拽悬浮球组件示例代码,当靠近屏幕边缘时会磁吸靠近并缩进半隐藏,反之鼠标移入时悬浮球按钮元素会恢复原状态,vue如何实现右侧悬浮并且能拖拽拖动?

效果图

在vue2项目开发中(PC网页/手机移动端H5都适用),详解实现悬浮窗带自动吸附组件功能示例,全页面随意拖动悬浮按钮元素,用户使用鼠标可按住拖拽到页面任意位置(当靠近屏幕边缘时会磁吸靠近),vue实现悬浮窗拖拽和靠边隐藏,拖拽流畅不卡顿,可自定义样式大小及相关事件,悬挂图片图像或按钮图标等,支持全局引入或局部页面使用。

提供详细示例代码,新手小白复制稍微改改就能用了。

在这里插入图片描述

完整源码

具体代码如下,样式改成你想要的即可。

以下是Vue实现靠边拖动悬浮球代码: ```vue <template> <div class="float-ball" :class="{ 'hide': isHide }" ref="floatBall" @mousedown="startDrag"> <!-- 悬浮球内容 --> </div> </template> <script> export default { data() { return { startX: 0, // 鼠标点击的x轴位置 startY: 0, // 鼠标点击的y轴位置 left: 0, // 悬浮球的左侧位置 top: 0, // 悬浮球的顶部位置 isDragging: false, // 是否正在拖拽中 isHide: false, // 是否隐藏 threshold: 50, // 悬浮球边缘的距离阈值 screenWidth: 0, // 屏幕宽度 screenHeight: 0 // 屏幕高度 } }, mounted() { this.screenWidth = window.innerWidth this.screenHeight = window.innerHeight window.addEventListener('scroll', this.handleScroll, true) }, beforeDestroy() { window.removeEventListener('scroll', this.handleScroll, true) }, methods: { startDrag(e) { this.startX = e.clientX this.startY = e.clientY this.isDragging = true document.addEventListener('mousemove', this.doDrag) document.addEventListener('mouseup', this.stopDrag) }, doDrag(e) { if (!this.isDragging) return const newX = e.clientX const newY = e.clientY // 计算悬浮球的新位置 const newLeft = this.left + newX - this.startX const newTop = this.top + newY - this.startY // 防止悬浮球拖出屏幕边界 if (newLeft < 0) { this.left = 0 } else if (newLeft > this.screenWidth - this.$refs.floatBall.offsetWidth) { this.left = this.screenWidth - this.$refs.floatBall.offsetWidth } else { this.left = newLeft } if (newTop < 0) { this.top = 0 } else if (newTop > this.screenHeight - this.$refs.floatBall.offsetHeight) { this.top = this.screenHeight - this.$refs.floatBall.offsetHeight } else { this.top = newTop } // 防止悬浮球随着滚动屏幕抖动 if (document.documentElement.scrollTop > 0) { this.top -= document.documentElement.scrollTop } // 判断是否靠近屏幕边缘 if (this.left < this.threshold || this.left > this.screenWidth - this.$refs.floatBall.offsetWidth + this.threshold) { this.hideBall() } else { this.showBall() } }, stopDrag() { this.isDragging = false document.removeEventListener('mousemove', this.doDrag) document.removeEventListener('mouseup', this.stopDrag) }, handleScroll() { if (!this.isHide) { this.top -= document.documentElement.scrollTop } }, hideBall() { this.isHide = true if (this.left < this.threshold) { this.left = -this.$refs.floatBall.offsetWidth / 2 } else { this.left = this.screenWidth - this.$refs.floatBall.offsetWidth / 2 } }, showBall() { this.isHide = false if (this.left < this.threshold) { this.left = -this.$refs.floatBall.offsetWidth / 2 + this.threshold } else { this.left = this.screenWidth - this.$refs.floatBall.offsetWidth / 2 - this.threshold } } } } </script> <style scoped> .float-ball { position: fixed; z-index: 999; width: 60px; height: 60px; border-radius: 50%; background-color: #f00; cursor: move; /* 悬浮球样式 */ } .hide { transform: translateX(-50%); /* 隐藏悬浮球一半 */ } </style> ``` 需要注意的是,为了防止悬浮球随着滚动屏幕抖动,我们需要在 `doDrag` `handleScroll` 方法中将悬浮球的 `top` 值减去滚动的距离。此外,当悬浮球靠近屏幕边缘,我们需要将其隐藏一半,可以通过设置 `transform: translateX(-50%)` 实现
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

十一猫咪爱养鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值