uniapp 悬浮球

悬浮球组件

在这里插入图片描述

<template>
	<view
		class="hover_ball_cell"
		:style="{ width: `${diameter}px`, height: `${diameter}px`, top: `${top}px`, left: `${left}px` }"
		@touchmove.prevent="touchmove"
		@touchend="touchend"
		@touchcancel="touchcancel"
		@tap="onTap"
		:animation="ballAnimation"
	>
		<view v-if="iconUrl == ''" class="wave" />
		<image v-else :src="iconUrl"  mode=""></image>
	</view>
</template>

<script>
export default {
	props: {
		iconUrl: {
			type: String,
			default: ''
		},
		 onClickHover: {
		      type: Function,
		      require: true,
		      default: null,
		    },
	},
	data() {
		return {
			diameter: 0,
			top: 0,
			left: 0,
			isMove: false,
			ballAnimation: {},
			timeout: null,
			modile: {},
		};
	},
	name: 'hover-ball',
	created() {
		let _this = this;
		_this.modile = uni.getSystemInfoSync();
		_this.top = _this.modile.safeArea.height - 180;
		_this.left = _this.modile.safeArea.width;
		_this.diameter = _this.modile.screenHeight / 15;
		let create = uni.createAnimation({
			duration: 400,
			timingFunction: 'ease-in'
		});
		create.translate('-100%').step();
		setTimeout(() => {
			// _this.left = _this.modile.safeArea.width
			create.translate('-50%').step();
			_this.ballAnimation = create.export();
		}, 0)
	},
	methods: {
		onTap() {
			let _this = this;
			let x = '0';
			if (2 * _this.left >= _this.modile.safeArea.width) {
				x = '-100%';
			}
			let create = uni.createAnimation({
				duration: 0
			});
			create.translate(x).step();
			_this.ballAnimation = create.export();
			_this.overBall();
			_this.onClickHover()
		},
		touchmove(e) {
			let _this = this;
			_this.isMove = true;
			if (_this.timeout != null) {
				clearTimeout(_this.timeout);
				_this.timeout = null;
			}
			var touch = e.touches[0] || e.changedTouches[0];
			_this.left = touch.clientX;
			_this.top = touch.clientY;
		},
		touchend(e) {
			let _this = this;
			if (!_this.isMove) {
				return;
			}
			_this.finish(e);
		},
		touchcancel(e) {
			let _this = this;
			if (!_this.isMove) {
				return;
			}
			_this.finish(e);
		},
		finish(e) {
			let _this = this;
			_this.isMove = false;
			var touch = e.touches[0] || e.changedTouches[0];
			_this.left = touch.clientX;
			_this.top = touch.clientY;
			let x = '0';
			if (2 * _this.left + _this.diameter >= _this.modile.safeArea.width) {
				_this.left = _this.modile.safeArea.width;
				x = '-100%';
			} else {
				_this.left = _this.modile.safeArea.left;
			}
			if (_this.top > _this.modile.safeArea.height + _this.modile.safeAreaInsets.bottom) {
				_this.top = _this.modile.safeArea.height + _this.modile.safeAreaInsets.bottom; 
			} else if (_this.top < - _this.diameter / 2) {
				_this.top = - _this.diameter / 2;
			}
			let create = uni.createAnimation({
				duration: 0
			});
			console.log(x)
			create.translate(x).step();
			_this.ballAnimation = create.export();
			_this.overBall();
		},
		overBall() {
			let _this = this;
			_this.timeout = setTimeout(() => {
				_this.timeout = null;
				let create = uni.createAnimation({
					duration: 400,
					timingFunction: 'ease-in'
				});
				create.translate('-50%').step();
				_this.ballAnimation = create.export();
			}, 1200);
		}
	}
};
</script>

<style lang="less" scoped>
.hover_ball_cell {
	position: fixed;
	overflow: hidden;
	border-radius: 50%;
	border: 4rpx solid #67c23a;
	background: #ffffff;
	transform: translate(-50%, 0);
	padding: 4rpx;
	display: flex;
	justify-content: center;
	align-items: center;
	z-index: 100000;

	.wave {
		position: relative;
		width: 100%;
		height: 100%;
		background-image: linear-gradient(-180deg, #aaff80 13%, #67c23a 91%);
		border-radius: 50%;
	}
	
	image{
		width: 60%;
		height: 60%;
	}
}
</style>

使用组件

在这里插入图片描述

<template>
	<view class="version">
		<hover-ball :onClickHover="onClickHover" :iconUrl="iconUrl"/>
	</view>
</template>

<script>
export default {
	data() {
		return {
			iconUrl: '../../static/image/shopping-cart.png'
		};
	},
	onLoad(option) {
	},
	onShow() {
	},
	methods: {
		onClickHover(){
			uni.$u.route('/pages/secondary/shopping-cart');
		},
	}
};
</script>

<style scoped lang="scss">

</style>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

织_网

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

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

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

打赏作者

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

抵扣说明:

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

余额充值