uniapp 悬浮按钮支持可拖拽。可移动。

悬浮按钮支持可拖拽。可移动。

javascript 复制代码
<floatBtn :disabled="['CM1100','CM2100','CM1400','CM2400','CM1505','CM2505','CM1502','CM2502','CM3140'].indexOf(formData.oper)==-1">
			<view @click="floatBtnCall" class="clof p5_10">采集<br/>参数</view>
		</floatBtn>

代码:

javascript 复制代码
<template>
	<view>
		<view class="topBtn" :class="{ 'disabled': disabled }" :style="{
				left: `${position.x}px`,
				top: `${position.y}px`
			}" @touchstart="onTouchStart" @touchmove="onTouchMove" @touchend="onTouchEnd">
			<slot></slot>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			// 控制按钮功能是否可用(点击/置顶)
			disabled: {
				type: Boolean,
				default: false,
			},
			// 控制是否允许拖拽位置(与 disabled 无关)
			draggable: {
				type: Boolean,
				default: true,
			}
		},
		data() {
			return {
				isFirstClick: true,
				hideTimer: null,

				// 拖拽相关
				startTouch: {
					x: 0,
					y: 0
				},
				startPosition: {
					x: 0,
					y: 0
				},
				position: {
					x: 0,
					y: 0
				},
				isDragging: false,
				dragThreshold: 5, // px,小于该值视为点击
			};
		},
		mounted() {
			this.initPosition();
		},
		beforeDestroy() {
			if (this.hideTimer) clearTimeout(this.hideTimer);
		},
		methods: {
			initPosition() {
				const sys = uni.getSystemInfoSync();
				const rpx2px = (rpx) => (rpx * sys.windowWidth) / 750;
				const btnSize = rpx2px(100); // 按钮宽高 100rpx
				const margin = rpx2px(30); // 右边距 30rpx

				// 初始位置:right: 30rpx, bottom: 30vh
				const x = sys.windowWidth - btnSize - margin;
				const y = sys.windowHeight * 0.7 - btnSize; // 30vh from bottom → top = 70vh - height

				this.position = {
					x: Math.round(x),
					y: Math.round(y)
				};
			},

			onTouchStart(e) {
				if (!this.draggable) return; // 👈 仅由 draggable 控制
				const touch = e.touches[0];
				this.startTouch.x = touch.clientX;
				this.startTouch.y = touch.clientY;
				this.startPosition = {
					...this.position
				};
				this.isDragging = false;
			},

			onTouchMove(e) {
				if (!this.draggable) return;
				const touch = e.touches[0];
				const deltaX = touch.clientX - this.startTouch.x;
				const deltaY = touch.clientY - this.startTouch.y;

				const sys = uni.getSystemInfoSync();
				const btnSize = (100 * sys.windowWidth) / 750;

				let newX = this.startPosition.x + deltaX;
				let newY = this.startPosition.y + deltaY;

				// 限制在屏幕可视区内
				newX = Math.max(0, Math.min(sys.windowWidth - btnSize, newX));
				newY = Math.max(0, Math.min(sys.windowHeight - btnSize, newY));

				this.position = {
					x: newX,
					y: newY
				};
				this.isDragging = true;
			},

			onTouchEnd(e) {
				if (!this.draggable) return;

				const touch = e.changedTouches[0];
				const dx = touch.clientX - this.startTouch.x;
				const dy = touch.clientY - this.startTouch.y;
				const distance = Math.sqrt(dx * dx + dy * dy);

				// 如果移动很小,视为点击
				if (!this.isDragging || distance < this.dragThreshold) {
					this.handleClick();
				}
			},

			handleClick() {
				if (this.disabled) return; // 👈 仅由 disabled 控制功能
				this.$emit('click');

				if (this.hideTimer) clearTimeout(this.hideTimer);

				if (this.isFirstClick) {
					this.isFirstClick = false;
					this.hideTimer = setTimeout(() => {
						this.isFirstClick = true;
					}, 5000);
				} else {
					uni.pageScrollTo({
						scrollTop: 0,
						duration: 300,
						success: () => {
							setTimeout(() => {
								this.isFirstClick = true;
							}, 500);
						}
					});
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	.topBtn {
		position: fixed !important;
		z-index: 9999 !important;
		width: 100rpx !important;
		height: 100rpx !important;
		background: #3c9cff !important;
		border-radius: 50% !important;
		display: flex !important;
		align-items: center !important;
		justify-content: center !important;
		box-shadow: 0 4rpx 12rpx rgba(60, 156, 255, 0.6) !important;
		cursor: pointer !important;
		user-select: none !important;
		touch-action: none !important;
	}

	.topBtn.disabled {
		opacity: 0.5 !important;
		/* 注意:不要加 pointer-events: none,
	   否则 draggable=true 时也无法拖拽! */
	}
</style>
相关推荐
意法半导体STM3217 分钟前
【官方原创】FDCAN数据段波特率增加后发送失败的问题分析 LAT1617
javascript·网络·stm32·单片机·嵌入式硬件·安全
为什么不问问神奇的海螺呢丶18 分钟前
n9e categraf redis监控配置
前端·redis·bootstrap
云飞云共享云桌面19 分钟前
推荐一些适合10个SolidWorks设计共享算力的服务器硬件配置
运维·服务器·前端·数据库·人工智能
咔咔一顿操作42 分钟前
轻量无依赖!autoviwe 页面自适应组件实战:从安装到源码深度解析
javascript·arcgis·npm·css3·html5
刘联其1 小时前
.net也可以用Electron开发跨平台的桌面程序了
前端·javascript·electron
韩曙亮1 小时前
【jQuery】jQuery 选择器 ④ ( jQuery 筛选方法 | 方法分类场景 - 向下找后代、向上找祖先、同级找兄弟、范围限定查找 )
前端·javascript·jquery·jquery筛选方法
前端 贾公子1 小时前
Node.js 如何处理 ES6 模块
前端·node.js·es6
pas1361 小时前
42-mini-vue 实现 transform 功能
前端·javascript·vue.js
你的代码我的心1 小时前
微信开发者工具开发网页,不支持tailwindcss v4怎么办?
开发语言·javascript·ecmascript
esmap1 小时前
OpenClaw与ESMAP AOA定位系统融合技术分析
前端·人工智能·计算机视觉·3d·ai·js