下拉刷新组件(Vue版)

在这里插入图片描述

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>下拉刷新</title>
		<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<script src="https://lib.baomitu.com/vue/2.6.11/vue.common.dev.js"></script>
	</head>
	<style>
		* {
			margin: 0;
			padding: 0;
		}

		#box {
			height: 100vh;
			overflow: scroll;
		}

		.box-item {
			width: 100%;
			height: 100px;
			box-sizing: border-box;
			border: 1px solid skyblue;
			font-size: 40px;
			text-align: center;
			background-color: #0077AA;
		}

		.box-item::after {
			content: "";
			display: inline-block;
			height: 100%;
			vertical-align: middle;
		}

		.drownFresh {
			width: 100%;
			background-color: #EBEBEB;
			text-align: center;
		}

		.drownFresh-hold,
		.drownFresh-release {
			padding: 20px 0;
		}
	</style>
	<style>
		.css-icon {
			display: inline-block;
			height: 1em;
			width: 1em;
			font-size: 20px;
			box-sizing: border-box;
			text-indent: -9999px;
			vertical-align: middle;
			position: relative;
		}

		.css-icon::before,
		.css-icon::after {
			content: '';
			box-sizing: inherit;
			position: absolute;
			left: 50%;
			top: 50%;
			-ms-transform: translate(-50%, -50%);
			transform: translate(-50%, -50%);
		}

		.icon-upward {
			transition: transform .3s;
		}

		.icon-upward::before {
			height: .65em;
			width: .65em;
			border-style: solid;
			border-width: 2px 0 0 2px;
			-ms-transform: translate(-50%, -50%) rotate(45deg);
			transform: translate(-50%, -50%) rotate(45deg);
		}

		.icon-upward::after {
			height: .8em;
			border-left: 2px solid;
			top: 55%;
		}

		.icon-upward.active {
			transform: rotate(180deg);

		}
	</style>
	<body>
		<div id="box" ref="box">
			<div class="drownFresh" ref="drownFresh">
				<div class="drownFresh-hold" ref="drownFresh_hold">
					<i ref="icon" class="css-icon icon-upward"></i>
					<p>释放刷新</p>
				</div>
				<div class="drownFresh-release" ref="drownFresh_release" v-show="drownFresh_release">
					<p>假装是个loading图标</p>
				</div>
			</div>
			<div class="box-item" v-for="(i,j) in list" :key="j">{{j}}</div>
		</div>
	</body>
	<script>
		new Vue({
			el: "#box",
			data: {
				list: Array(100).fill(0).map((i, j) => j),
				drownFresh_release: false
			},
			mounted() {
				let that = this
				const Box = this.$refs.box // 整个大元素
				const iconEl = this.$refs.icon // 手写icon图标
				const drownFresh = this.$refs.drownFresh
				const drownFresh_hold = this.$refs.drownFresh_hold // 下拉刷新块
				const drownFresh_release = this.$refs.drownFresh_release // 释放加载块
				drownFresh.style.marginTop = `-${drownFresh_hold.offsetHeight}px`


				let offsetY, MoveOffset, timer;

				Box.addEventListener('touchstart', function(e) {
					offsetY = e.touches[0].pageY
				})
				Box.addEventListener('touchmove', function(e) {
					if (this.scrollTop != 0) return //	不到顶部不做操作
					MoveOffset = e.touches[0].pageY - offsetY // 下拉的距离
					if (MoveOffset < 0 || MoveOffset > 180) return // 避免初始化赋值  || 下拉距离超过180 停止拉伸
					drownFresh.style.marginTop = `-${drownFresh_hold.offsetHeight-MoveOffset}px`
					// if(drownFresh_hold.offsetHeight-MoveOffset<0){
					// 	this.style.transform = `translateY(${MoveOffset}px)` //	动态拉伸距离
					// 	this.style.transition = '' // 动画清0
					// }
					
					//  下拉距离超过60 箭头转向
					MoveOffset > 60 ? iconEl.classList.add('active') : iconEl.classList.remove('active')
				})
				Box.addEventListener('touchend', function(e) {
					if (MoveOffset > 60) {
						// this.style.transform = `translateY(0px)`
						// this.style.transition = '0.8s'
						that.drownFresh_release = true
						if (timer) return
						timer = setTimeout(() => {
							that.drownFresh_release = false
							iconEl.classList.remove('active')
							timer = null
							clearTimeout(timer)
							//  释放刷新后接口写在这
							console.log('接口刷新了')
						}, 800)
					} else {
						
					}
					
					drownFresh.style.marginTop = `-${drownFresh_hold.offsetHeight}px`
					offsetY = null
					MoveOffset = null

				})
			}
		})
	</script>
</html>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值