悬浮球

22 篇文章 0 订阅
17 篇文章 0 订阅

悬浮球可左右靠边悬浮,可停止保留原来位置,可控制
数值可更改自己想要的

//子组件
<template>
	<view>
		<view id="_drag_button" class="drag"   :style="'left: ' + left + 'px; top:' + top + 'px;'" @touchstart="touchstart"
		 @touchmove.stop.prevent="touchmove" @touchend="touchend" @click.stop.prevent="click" :class="[{transition: isDock && !isMove},Show_hide == true ? 'display':'Through'  ]"><text>{{ text }}</text>
		</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: '', //添加文字
				Show_hide:false,
				
			}
		},

		mounted() {
			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;
			}
			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 - 50;
				if(uni.getStorageSync('top') != ''){
					this.top = uni.getStorageSync('top')
					this.left = uni.getStorageSync('left')
				}
			}).exec();
		},
		methods: {
			click() {
				this.$emit('btnClick');
				
			},
			touchstart(e) {
				
				this.Show_hide = true;
				setTimeout(() => {
					this.Show_hide = true;
				}, 400)
				let btnTouchstart = 'btnTouchstart'
				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.top = this.edge;
				} else if (clientY > edgeBottom) {
					
					this.top = edgeBottom;
				} else {
					this.top = clientY;
				}
			},
			touchend(e) {
				
				setTimeout(() => {
					this.Show_hide = false;
				}, 800)
				
				if (this.isDock) {
					let edgeRigth = this.windowWidth - this.width - this.edge;
					if (this.left < this.windowWidth / 2 - this.offsetWidth) {
						this.left = this.edge;
						if (this.top <= 150) {
							this.top = 150
						}
					} else {
						this.left = edgeRigth;
						if (this.top <= 150) {
							this.top = 150
						}
					}
				}else{
					if(this.top <= 150){
						this.top = 150
					}
					if(this.left <=0){
						this.left = 5;
					}
					if(this.left >=  this.windowWidth - 50){
						this.left = this.windowWidth -70;
						console.log(this.left)
					}
				}
				
				this.isMove = false;
				uni.setStorageSync('top',this.top)
				uni.setStorageSync('left',this.left)
				this.$emit('btnTouchend','1111');
				
			},
		}
	}
</script>

<style lang="scss">
	.drag {
		box-sizing: border-box;
		display: flex;
		justify-content: center;
		align-items: center;
		color: $uni-text-color-inverse;
		width: 117rpx;
		height: 120rpx;
		border-radius: 10rpx;
		font-size: $uni-font-size-sm;
		position: fixed;
		z-index: 99999;
		

		&.transition {
			transition: left .3s ease, top .3s ease;
		}
	}
	.Through{
		background-image: url(http://vshop.jwcn.net:8080/static/images/module-icon/show_image.png);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		background-position:  center center ;
	}
	.display{
		background-image: url(http://vshop.jwcn.net:8080/static/images/module-icon/show_img.png);
		background-size: 100% 100%;
		background-repeat: no-repeat;
		background-position:  center center ;
	}
</style>

//父组件
<view v-if="suspension">
		<drag-button :isDock="Pullover" :existTabBar="true" @btnClick="btnClick" @btnTouchstart="btnTouchstart" @btnTouchend="btnTouchend" />
</view>

return{
  suspension: uni.getStorageSync('suspension') || false,  //是否显示悬浮球
  Pullover: uni.getStorageSync('Pullover') || false,    //是否靠边停留
}

btnTouchstart() {
	// console.log(“s”)
},
btnTouchend() {
	// console.log(“s”)
},
btnClick() {
	// console.log(“s”)
},
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值