监听页面滚动,给页面中的节点添加动态过渡效果

31 篇文章 0 订阅

效果示例图

在这里插入图片描述
在这里插入图片描述

示例代码

<template>
	<div class="animation-wrap">
		<!-- header-start -->
		<div class="animation-header">头部</div>
		<!-- header-end -->
		<div class="animation-subtitle animation-show">标题一</div>
		<div class="animation-content animation-show">内容一</div>
		<div class="animation-subtitle animation-show">标题二</div>
		<div class="animation-content animation-show">内容二</div>
		<div class="animation-subtitle animation-show">标题三</div>
		<div class="animation-content animation-show">内容三</div>
		<div class="animation-subtitle animation-show">标题四</div>
		<div class="animation-content animation-show">内容四</div>
		<div class="animation-subtitle animation-show">标题五</div>
		<div class="animation-content animation-show">内容五</div>
		<!-- footer-start -->
		<div class="animation-footer">底部</div>
		<!-- footer-end -->
	</div>
</template>

<script>
export default {
	data() {
		return {};
	},
	created() {},
	mounted() {
		
		/**
		 * 监听滚动条事件
		 * **/
		this.scrollHandle();
		window.addEventListener('scroll', this.scrollHandle);
	},
	methods: {
		scrollHandle() {
			//可视窗口的高度
			const viewPortHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
			console.log('[可视窗口的高度]', viewPortHeight);
			//滚动条距离顶部的距离
			const scrollTop = this.getScrollTop();
			console.log('[滚动条距离顶部的距离]', scrollTop);
			const elHander = document.querySelectorAll('.animation-show');
			elHander.forEach(el => {
				const offsetTop = el.offsetTop;
				console.log('[每个节点距离顶部的距离]', offsetTop);
				//当节点距离顶部的距离-滚动条距离顶部的距离小于窗口可视高度时,说明节点已经出现在用户的视线里
				console.log('[]', offsetTop - scrollTop <= viewPortHeight);
				if (offsetTop - scrollTop <= viewPortHeight) {
					el.classList.add('current-animation');
				} else {
					el.classList.remove('current-animation');
				}
			});
		},
		/**
		 * 获取滚动条距离顶部的距离
		 * **/
		getScrollTop() {
			let scrollTop = 0;
			//documentElement:文档的文档元素
			if (document.documentElement && document.documentElement.scrollTop) {
				scrollTop = document.documentElement.scrollTop;
			} else if (document.body) {
				scrollTop = document.body.scrollTop;
			}
			return scrollTop;
		}
	}
};
</script>

<style lang="scss" scoped>
.animation-wrap {
	width: 1200px;
	margin: 0px auto;
	.animation-header {
		width: 100%;
		height: 300px;
		background-color: red;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
	}
	.animation-footer {
		width: 100%;
		height: 300px;
		margin-top: 12px;
		background-color: red;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
	}
	.animation-subtitle {
		width: 100%;
		height: 50px;
		display: flex;
		align-items: center;
		margin: 12px auto;
		background-color: powderblue;
		color: #fff;
		padding-left: 12px;
	}
	.animation-content {
		width: 100%;
		height: 400px;
		display: flex;
		justify-content: center;
		align-items: center;
		color: #fff;
		background-color: #dcdcdc;
	}
	.current-animation {
		animation: myfirst 2s ease;
	}
	@keyframes myfirst {
		0% {
			transform: translate(0%, 200%) scale(0);
		}
		100% {
			transform: translate(0px, 0px) scale(1);
		}
	}
}
</style>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值