前端 手搓 橡皮筋效果

Uniapp 版

<template>
	<view class="bounce" :animation="animationData">
		<view class="box" @touchstart="touchstart" @touchend="touchend" @touchmove="touchmove">
			<view v-for="i in 20" :key="i">{{ i }}</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				pageY: 0,
				animationData: {},
				touchmoveY: 0,
			};
		},
		methods: {
			getAnimation(s, h) {
				let animation = uni.createAnimation({
					duration: s,
					timingDunction: 'ease',
				})
				animation.translateY(h).step()
				this.animationData = animation.export()
			},
			touchstart(e) {
				this.pageY = e.touches[0].pageY
			},
			touchend(e) {
				this.getAnimation(300, 0)
			},
			touchmove(e) {
				let pageY = Math.ceil(e.touches[0].pageY)
				let h = pageY - this.pageY;
				if (h < 0 && pageY < this.touchmoveY && pageY >= 200) {
					this.getAnimation(300, h / 2 + 'px');
				} else {
					this.getAnimation(300, h / 3 + 'px');
				}
				this.touchmoveY = pageY
			}
		}
	};
</script>

<style>
	.bounce {
		width: 200vh;
		background: skyblue;
		margin-top: 100rpx;
	}
	.box {
		width: 100%;
		transition: all .1s ease-out;
	}
</style>

移动端

<template>
	<div class="bounce" :style="{ transform: 'translateY(' + translateY + ')' }" @touchstart="touchstart" @touchend="touchend" @touchmove="touchmove">
		<div class="box">
			<div v-for="i in 20" :key="i">{{ i }}</div>
		</div>
	</div>
</template>

<script>
	export default {
		data() {
			return {
				pageY: 0,
				translateY: '0px',
				touchmoveY: 0,
			};
		},
		methods: {
			getTranslateY(h) {
				return h + 'px';
			},
			touchstart(e) {
				this.pageY = e.touches[0].pageY;
			},
			touchend(e) {
				this.translateY = this.getTranslateY(0);
			},
			touchmove(e) {
				let pageY = Math.ceil(e.touches[0].pageY);
				let h = pageY - this.pageY;
				if (h < 0 && pageY < this.touchmoveY && pageY >= 200) {
					this.translateY = this.getTranslateY(h / 2);
				} else {
					this.translateY = this.getTranslateY(h / 3);
				}
				this.touchmoveY = pageY;
			}
		}
	};
</script>

<style>
	.bounce {
		width: 50vh;
		background: skyblue;
		margin-top: 100rpx;
		overflow: hidden; /* 防止内容溢出 */
		user-select: none;
	}
	.box {
		width: 100%;
		transition: transform .3s ease-out;
	}
</style>

PC版 与移动端同理

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值