uniapp 悬浮球 可回弹

<template>
	<!-- 悬浮球 -->
	<movable-area class="movableArea">
		<!-- damping:官方默认值20,控制x或y改变时的动画和过界回弹的动画,值越大移动越快
			 direction: 移动方向,属性值有all、vertical、horizontal、none
		     friction: 摩擦系数,用于控制惯性滑动的动画,值越大摩擦力越大,滑动越快停止,越小,滑动会有停留,会有原路返回的情况 -->
			 
		<movable-view class="movableView" :x="x" :y="y" :position="position" direction="all" :damping="30"  :friction="500"
		 @change="onChange" @touchend="onTouchend">
			<!-- <text class="close" @tap="close($event)">X</text> -->
			<slot></slot>
		</movable-view>
	</movable-area>
</template>

<script>
	export default {
		name: 'suspendedBall',
		props: {
			// 悬浮球位置:1.左上角 2.右上角 3.左下角 4.右下角
			position: {
				type: Number,
				default: 4
			}
		},

		data() {
			return {
				timed: null,
				x: 100,
				y: 348,
				x1: 0,
				x2: 0,
				y1: 0,
				y2: 0,
				move: {
					x: 0,
					y: 0
				}
			};
		},

		mounted() {			
			// 获取系统信息
			uni.getSystemInfo({
				success: (res) => {
					this.x1 = 0;
					this.x2 = parseInt(res.windowWidth) - 50;
					this.y1 = 0;
					this.y2 = parseInt(res.windowHeight) - 20;

					if (this.position == 1 || this.position == 2) this.y = parseInt(this.y2 * 0.2);
					if (this.position == 3 || this.position == 4) this.y = parseInt(this.y2 * 0.8);
					if (this.position == 1 || this.position == 3) this.x = parseInt(this.x1);
					if (this.position == 2 || this.position == 4) this.x = parseInt(this.x2);
					this.move.x = this.x;
					this.move.y = this.y;
				}
			})
		},

		methods: {
			// close (e) {
			// 	this.$emit('close-handler', { e:e })
			// },
			
			/**
			 * @param {Object} e拖动触发事件
			 */
			onChange(e) {
				// console.log('拖动:', e)
				if (e.detail.source === "touch") {
					this.move.x = e.detail.x;
					this.move.y = e.detail.y;
				}
			},

			/**
			 * 这个好像是抬起手触发事件
			 * */
			onTouchend() {
				this.x = this.move.x;
				this.y = this.move.y;
				this.timed = setTimeout(() => {
					if (this.move.x < this.x2 / 2) this.x = this.x1;
					else this.x = this.x2;
					// console.log('抬起', this.x, this.y)
				}, 30)
			},
		},

		beforeDestroy() {
			clearTimeout(this.timed)
		}
	}
</script>

<style lang="scss" scoped>
	.movableArea {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		pointer-events: none; //设置area元素不可点击,则事件便会下移至页面下层元素
		z-index: 999;

		.movableView {
			pointer-events: auto; //可以点击
			width: 120rpx;
			height: 120rpx;

			
			.close {
				    position: absolute;
				    top: -54rpx;
				    right: 12rpx;
			}
		}
	}
</style>

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值