上拉刷新下拉加载实现

91 阅读1分钟
<template>
	<view class="index-container" :style="{transform:`translateY(${translateY}px)`}">
		<view class="top" :style="{height:height+'px'}">
			下拉刷新
		</view>
		<view class="main" @touchcancel="touchend" @touchstart="touchstart" @touchend="touchend" @touchmove="touchmove">
			btn{{-height}}</view>
		<view class="bottom" :style="{height:-height+'px'}">
			上拉加载{{height}}
		</view>
	</view>
</template>
<script>
	document.body.addEventListener('touchmove', function(e) {
		e.preventDefault(); //阻止默认的处理方式(阻止下拉滑动的效果)
	}, {
		passive: false
	}); //passive 参数不能省略,用来兼容ios和android
	export default {
		data() {
			return {
				startY: 0,
				height: 0,
			}
		},
		computed:{
			translateY(){
				if(this.height>0){
					return 0
				}else{
					return this.height
				}
			},
		},
		methods: {
			touchstart(e) {
				// console.log(e)
				this.startY = e.changedTouches[0].clientY
			},
			touchend(e) {
				// console.log(e)
				this.height = 0
			},
			touchmove(e) {
				this.height = e.changedTouches[0].clientY - this.startY
				console.log(this.height)
			},
		},
	}
</script>
<style lang="scss" scoped>
	.index-container {
		height: 100vh;
	}

	.main {
		height: 100vh;
		border: 1px solid red;
	}

	.top,
	.bottom {
		overflow: hidden;
		display: flex;
		justify-content: center;
		align-items: center;
		border: 1px solid #000;
	}
</style>