uniapp 可拖拽按钮组件封装

<template>
	<view>
		<view
			id="_drag_button"
			class="drag"
			:style="'left: ' + left + 'px; top:' + top + 'px;'"
			@touchstart="touchstart"
			@touchmove.stop.prevent="touchmove"
			@touchend.stop="touchend"
			@click.stop.prevent="click"
			:class="{ transition: isDock && !isMove }"
		>
			<image style="width: 100%;height: 100%;transform: scale(1.5);" src="https://api.yianda-ennergy.com/storage/20231026/27f497a37a6ad847c60a753d3a934471.gif" mode="aspectFit"></image>
		</view>
	</view>
</template>

<script>
export default {
	name: 'drag-button',
	props: {
		isDock: {
			type: Boolean,
			default: false
		},
		existTabBar: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			top: 0,
			left: 0,
			width: 0,
			height: 0,
			offsetWidth: 0,
			offsetHeight: 0,
			windowWidth: 0,
			windowHeight: 0,
			isMove: true,
			edge: 10,
			text: '领券' ,//此处自定义按钮文字(1-3个汉字)
			topBarHeight:0
		};
	},
	mounted() {
		let that = this
		uni.getSystemInfo({
			success: function(e) {
				console.log(e)
				 let statusBar = 0  //状态栏高度
				 let customBar = 0  // 状态栏高度 + 导航栏高度 
				// #ifdef MP
				      statusBar = e.statusBarHeight
				      customBar = e.statusBarHeight + 45
				      if (e.platform === 'android') {
				        customBar = e.statusBarHeight + 50
				      }
				// #endif
				// #ifdef MP-WEIXIN
				      statusBar = e.statusBarHeight
				      const custom = wx.getMenuButtonBoundingClientRect()
				      customBar = custom.bottom + custom.top - e.statusBarHeight
				 // #endif
				 that.topBarHeight = customBar
		console.log(customBar)
			}
		});
		const sys = uni.getSystemInfoSync();

		this.windowWidth = sys.windowWidth;
		this.windowHeight = sys.windowHeight;

		// #ifdef APP-PLUS
		this.existTabBar && (this.windowHeight -= 50);
		// #endif
		if (sys.windowTop) {
			this.windowHeight += sys.windowTop;
		}
		// console.log(sys);
		const query = uni.createSelectorQuery().in(this);
		query
			.select('#_drag_button')
			.boundingClientRect(data => {
				this.width = data.width;
				this.height = data.height;
				this.offsetWidth = data.width / 2;
				this.offsetHeight = data.height / 2;
				this.left = this.windowWidth - this.width - this.edge;
				this.top = ((this.windowHeight - this.height - this.edge) * 8) / 10;
			})
			.exec();
	},
	methods: {
		click() {
			this.$emit('btnClick');
		},
		touchstart(e) {
			this.$emit('btnTouchstart');
		},
		touchmove(e) {
			// 单指触摸
			if (e.touches.length !== 1) {
				return false;
			}

			this.isMove = true;

			this.left = e.touches[0].clientX - this.offsetWidth;

			let clientY = e.touches[0].clientY - this.offsetHeight;
			// #ifdef H5
			clientY += this.height;
			// #endif
			let edgeBottom = this.windowHeight - this.height - this.edge;

			// 上下触及边界
			if (clientY < this.edge+this.topBarHeight) {
				this.top = this.edge+this.topBarHeight;
			} else if (clientY > edgeBottom) {
				this.top = edgeBottom;
			} else {
				this.top = clientY;
			}
		},
		touchend(e) {
			if (this.isDock) {
				let edgeRigth = this.windowWidth - this.width - this.edge;

				if (this.left < this.windowWidth / 2 - this.offsetWidth) {
					this.left = this.edge;
				} else {
					this.left = edgeRigth;
				}
			}

			this.isMove = false;

			this.$emit('btnTouchend');
		}
	}
};
</script>

<style lang="scss">
.drag {
	display: flex;
	justify-content: center;
	align-items: center;
	// background-color: rgba(0, 0, 0, 0.5);
	// background-image: url('https://wimg.588ku.com/gif620/20/08/24/aa702016e0c2bacbb005a722dcf509cf.gif');
	box-shadow: 0 0 3upx rgba(0, 0, 0, 0.1);
	color: $uni-text-color-inverse;
	width: 180upx;
	height: 180upx;
	border-radius: 50%;
	font-size: $uni-font-size-sm;
	position: fixed;
	z-index: 1000;

	&.transition {
		transition: left 0.3s ease, top 0.3s ease;
	}
}
</style>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值