vue2 实现可拖拽悬浮球

news/2024/9/30 13:27:01
  • 实现效果

  • 相关代码

点击查看代码
<template><div class="float-box"><divclass="button-box"@mousedown="mousedown"@mousemove="mousemove"@touchmove="mousemove"@mouseup="mouseup"@touchstart="mousedown"@touchend="mouseup"@click="onClick"ref="flbutton"><span class="title">{{ title }}</span></div><div:style="{ left: left + 'px', top: top + 'px' }"v-if="menuFlag":class="menuPosition === 'right' ? 'menu-item-right':'menu-item-left'"><div :class="tabBackImg(index)" class="box-title-box-item" v-for="(item,index) in circleList"@click="circleClick(item,index)"><span :class="'text'+index">{{ item }}</span></div></div></div>
</template><script>
export default {name: "FloatButton",props: {circleList: {type: Array,default: () => []},},data() {return {title: '',left: 0,top: 40,menuFlag: false,//菜单显隐mouseDownflag: false, // 鼠标点击状态position: {x: 0,y: 0,},nx: "",ny: "",dx: "",dy: "",xPum: "",yPum: "",movb: 1,//区分拖拽还是点击menuPosition: 'right',};},mounted() {this.left = this.$refs.flbutton.offsetLeft + 20;this.top = this.$refs.flbutton.offsetTop;this.title = this.circleList[0];},computed: {tabBackImg() {return (index) => {if (index === 0) {return 'tab-item-1'} else if (index === 1) {return 'tab-item-2'} else if (index === 2) {return 'tab-item-3'} else if (index === 3) {return 'tab-item-4'}}}},methods: {circleClick(item, index) {this.title = itemthis.$emit('circleClick', item, index)},//鼠标摁下mousedown() {this.mouseDownflag = true;/*此处判断pc 或移动端得到event 事件*/var touch;if (event.touches) {touch = event.touches[0];} else {touch = event;}//鼠标点击面向页面的x坐标y坐标this.position.x = touch.clientX;this.position.y = touch.clientY;//鼠标x坐标-拖拽按钮x坐标得到鼠标距离拖拽按钮的间距this.dx = this.$refs.flbutton.offsetLeft;//鼠标y坐标-拖拽按钮y坐标得到鼠标距离拖拽按钮的间距this.dy = this.$refs.flbutton.offsetTop;},//鼠标拖拽移动mousemove() {if (this.mouseDownflag) {this.movb = 2;this.menuFlag = false;/*此处判断得到event事件*/var touch;if (event.touches) {touch = event.touches[0];} else {touch = event;}//鼠标坐标-鼠标与拖拽按钮的间距坐标得到拖拽按钮的左上角x轴y轴坐标this.nx = touch.clientX - this.position.x;this.ny = touch.clientY - this.position.y;this.xPum = this.dx + this.nx;this.yPum = this.dy + this.ny;let width = window.innerWidth - this.$refs.flbutton.offsetWidth; //屏幕宽度减去自身控件宽度let height = window.innerHeight - this.$refs.flbutton.offsetHeight; //屏幕高度减去自身控件高度/* 此处判断拖拽按钮 如果超出屏幕宽高或者小于设置屏幕最大 x=全局容器x y=全局容器y否则 设置 为 x=0 y=0*/this.xPum < 0 && (this.xPum = 0);this.yPum < 0 && (this.yPum = 0);this.xPum > width && (this.xPum = width);this.yPum > height && (this.yPum = height);if (this.xPum > 900) {this.menuPosition = 'left'} else {this.menuPosition = 'right'}// 计算后坐标  设置 按钮位置this.$refs.flbutton.style.left = this.xPum + "px";this.$refs.flbutton.style.top = this.yPum + "px";this.left = this.xPum + 20;this.top = this.yPum;//阻止页面的滑动默认事件document.addEventListener("touchmove", function () {event.preventDefault();}, false);}},//鼠标抬起mouseup() {this.mouseDownflag = false;},onClick() {if (this.movb === 2) {this.movb = 1;} else {this.menuFlag = !this.menuFlag;}},},
};
</script>
<style scoped lang="scss">
.float-box {position: relative;.button-box {width: 110px;height: 110px;position: fixed;top: 100px;left: 500px;touch-action: none;text-align: center;color: white;background-image: url("~@/assets/images/screen/float-button/button.png");background-repeat: no-repeat;background-size: 100% 100%;line-height: 100px;font-size: 14px;cursor: pointer;z-index: 99;.title {background: linear-gradient(rgba(255, 255, 255, 1) 25%, rgba(69, 177, 254, 1) 100%);;-webkit-background-clip: text;color: transparent;font-weight: bolder;}}.menu-item-left {position: absolute;z-index: 99;.box-title-box-item {color: #FFFFFF;cursor: pointer;font-size: 13px;cursor: pointer;display: flex;justify-content: center;align-items: center;}.tab-item-1 {height: 53px;width: 43px;position: absolute;top: -8px;left: -42px;background: url("~@/assets/images/screen/float-button/button-l1.png") no-repeat;background-size: 100% 100%;line-height: 43px;padding-top: 10px;}.tab-item-2 {height: 53px;width: 44px;background: url("~@/assets/images/screen/float-button/button-l2.png") no-repeat;background-size: 100% 100%;position: absolute;top: 46px;left: -42px;line-height: 43px;}.tab-item-3 {height: 43px;width: 53px;background: url("~@/assets/images/screen/float-button/button-r3.png") no-repeat;background-size: 100% 100%;position: absolute;top: 80px;left: 37px;line-height: 43px;}.tab-item-4 {height: 43px;width: 53px;background: url("~@/assets/images/screen/float-button/button-r4.png") no-repeat;background-size: 100% 100%;position: absolute;top: 80px;left: -17px;line-height: 40px;}.tab-item-1:hover, .tab-item-2:hover, .tab-item-3:hover, .tab-item-4:hover {transform: scale(1.2);}.text0, .text1, .text2, .text3 {display: inline-block;}.text0 {writing-mode: vertical-rl;letter-spacing: 1px;transform: rotate(17deg);}.text1 {writing-mode: vertical-rl;letter-spacing: 1px;transform: rotate(335deg);}.text2 {transform: rotate(340deg);}.text3 {transform: rotate(20deg);}}.menu-item-right {position: absolute;z-index: 99;.box-title-box-item {color: #FFFFFF;cursor: pointer;font-size: 13px;cursor: pointer;display: flex;justify-content: center;align-items: center;}.tab-item-1 {height: 53px;width: 43px;position: absolute;top: -10px;left: 67px;background: url("~@/assets/images/screen/float-button/button-r1.png") no-repeat;background-size: 100% 100%;line-height: 43px;padding-top: 10px;}.tab-item-2 {height: 53px;width: 43px;background: url("~@/assets/images/screen/float-button/button-r2.png") no-repeat;background-size: 100% 100%;position: absolute;top: 45px;left: 69px;line-height: 43px;}.tab-item-3 {height: 43px;width: 53px;background: url("~@/assets/images/screen/float-button/button-r3.png") no-repeat;background-size: 100% 100%;position: absolute;top: 80px;left: 35px;line-height: 43px;}.tab-item-4 {height: 43px;width: 53px;background: url("~@/assets/images/screen/float-button/button-r4.png") no-repeat;background-size: 100% 100%;position: absolute;top: 80px;left: -20px;line-height: 40px;}.tab-item-1:hover, .tab-item-2:hover, .tab-item-3:hover, .tab-item-4:hover {transform: scale(1.2);}.text0, .text1, .text2, .text3 {display: inline-block;}.text0 {writing-mode: vertical-rl;letter-spacing: 1px;transform: rotate(345deg);}.text1 {writing-mode: vertical-rl;letter-spacing: 1px;transform: rotate(21deg);margin-bottom: 5px;}.text2 {transform: rotate(337deg);margin-right: 10px;}.text3 {transform: rotate(20deg);margin-left: 5px;}}
}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/45203.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Linux 提权-MySQL UDF

本文通过 Google 翻译 MySQL User Defined Functions – Linux Privilege Escalation 这篇文章所产生,本人仅是对机器翻译中部分表达别扭的字词进行了校正及个别注释补充。导航0 前言 1 什么是用户定义函数 (UDF) ? 2 枚举 UDF 漏洞利用条件2.1 手动枚举 UDF 漏洞利用条件2.1…

1.2 陶瓷电容(MLCC)选型----硬件设计指南(持续补充更新)

本系列文章是笔者总结多年工作经验,结合理论与实践进行整理备忘的笔记。希望能直接指导硬件工程师的设计实操,争取每一条设计要点指南都做到有理有据。既能帮助自己温习整理避免遗忘也能帮助其他需要参考的朋友。笔者也会不定期根据遇到的问题和想起的要点进行查漏补缺。如有…

Profibus转Modbus网关帮助PLC实现智能激光设备通讯

通过Profibus转Modbus网关(XD-MDPB100),可以实现PLC与激光设备之间的无缝连接,实现数据的实时传输与指令的可靠执行。本文将深入探讨PLC通过Profibus转Modbus网关(XD-MDPB100)与激光设备进行通讯的应用案例,带您一窥其中的奥秘。它简单易实现,具有良好的兼容性和可靠性…

【Spring】Bean管理

获取Bean 要从IOC容器当中来获取到bean对象,需要先拿到IOC容器对象@Autowiredprivate ApplicationContext applicationContext; //IOC容器对象Spring容器中提供了一些方法,可以主动从IOC容器中获取到bean对象,下面介绍3种常用方式:根据name获取bean Object getBean(String …

记录--createObjectURL这个API真好用,我举几个场景你们就懂了

🧑‍💻 写在开头 点赞 + 收藏 === 学会🤣🤣🤣 前言 随着我用 URL.createObjectURL 这个 API 越来越多次,越发感觉真的是一个很好用的方法,列举一下我在项目中用到它的场景吧~图片预览 以前我们想要预览图片,只能是上传图片到后端后,获取到url然后赋予给img标签,…

linux 部署jar包

1.准备工作 linux如果没有配置java环境变量的话,具体操作见文末推荐。 2.启动jar包 linux操作系统下启动jar包的方式和windows操作系统没有区别。 用法都是遵循java语法规范。 前提:跳转到要启动的jar包所在目录。cd jar包所在绝对路径使用默认jdk启动 查看当前jdk版本 java …

机器学习--有监督学习--分类算法(KNN算法)

使用场景:做分类的,比如银行想做客户分类,看看新的这个客户,他是高风险用户还是低风险用户。 原理使用:可以用贝叶斯分类,决策树算法,还有KNN,本篇主要整理KNN。 KNN原理:有N个样本点,对新纪录r,使用KNN进行分类,看它属于哪个分类。具体如下: 1、先确定k值,不建议…