vue监听页面滑动方向

该代码示例展示了在Vue.js组件中如何监听并处理滑动事件。通过对touchstart、touchmove和touchend事件的绑定,检测用户在屏幕上的滑动方向,包括左至右、右至左、上至下和下至上四种情况。
摘要由CSDN通过智能技术生成
<template>
	<div id="app" @touchstart="touchStart" @touchmove="touchMove" @touchend="touchEnd">
		<keep-alive include="Home">
			<router-view />
		</keep-alive>
	</div>
</template>

<script>
export default {
	name: 'App',
	data() {
		return {
			startMoveX: 0,
			startMoveY: 0,
			moveEndX: 0,
			moveEndY: 0,
		}
	},
	methods: {
		touchStart(e) {
			// e.preventDefault();
			this.startX = e.changedTouches[0].pageX;
			this.startY = e.changedTouches[0].pageY;
		},
		touchMove(e) {
			// e.preventDefault();
			this.moveEndX = e.changedTouches[0].pageX;
			this.moveEndY = e.changedTouches[0].pageY;
			let X = this.moveEndX - this.startX;
			let Y = this.moveEndY - this.startY;
			if (Math.abs(X) > Math.abs(Y) && X > 0) {
				console.log("left 2 right");
			} else if (Math.abs(X) > Math.abs(Y) && X < 0) {
				console.log("right 2 left");
			} else if (Math.abs(Y) > Math.abs(X) && Y > 0) {
				console.log("top 2 bottom");
			} else if (Math.abs(Y) > Math.abs(X) && Y < 0) {
				console.log("bottom 2 top");
			} else {
				console.log("just touch");
			}
		},
		touchEnd(e) {},
	}
}

</script>

<style></style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值