纯js实现滚动底部加载和返回顶部动画

先看效果

在这里插入图片描述

滚动加载代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			html,body{
				margin: 0;
				padding: 0;
				height: 100%;
				overflow: auto;
			}
			div{
				height: 300px;
			}
			.box1{
				background-color: #5F9EA0;
			}
			.box2{
				background-color: #87CEEB;
			}
			.box3{
				background-color: #8A2BE2;
			}
			.box4{
				background-color: #EBCB69;
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
		<div class="box3"></div>
		<div class="box4"></div>
	</body>
	<script type="text/javascript">
		const body = document.querySelector('body')
		body.addEventListener('scroll',e=>{
			const target = e.target //获取滚动对象
			const scrollTop = target.scrollTop //距离顶部距离
			const scrollHeight = target.scrollHeight //整个页面滚动高度
			const clientHeight = target.clientHeight //可视化页面高度
			//scrollTop+clientHeight = 页面已经滚动的高度
			if(scrollTop+clientHeight+20>=scrollHeight){ //距离底部20px时
			//创建div加入body中
				const div = document.createElement('div')
				const R = Math.floor(Math.random()*255+1)
				const G = Math.floor(Math.random()*255+1)
				const B = Math.floor(Math.random()*255+1)
				div.style.backgroundColor = `rgb(${R},${G},${B})`
				body.appendChild(div)
			}
		},false)
	</script>
</html>

滚动动画代码

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			html,body{
				margin: 0;
				padding: 0;
				height: 100%;
				overflow: auto;
			}
			.top{
				position: fixed;
				width: 50px;
				height: 50px;
				border-radius: 50%;
				bottom: 10px;
				right: 20px;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 20px;
				color:  #87CEEB;
				background-color: #3b6aeb;
				cursor: pointer;
			}
		</style>
	</head>
	<body>
		<div class="top"></div>
	</body>
	<script type="text/javascript">
		const body = document.querySelector('body')
		const topTo = document.querySelector('.top')
		topTo.addEventListener('click',()=>{
			let scrollTop = body.scrollTop //已经滚动的距离
			const time = setInterval(()=>{ //循环定时器
				body.scrollTo(0,scrollTop) //滚动方法
				if(scrollTop>30){ //在大于30px时滚动速度较快
					scrollTop-=20
				}else{
					scrollTop--
				}
				if(scrollTop<=0) clearInterval(time) //在到顶部清楚定时器
			},1)
		},false)
	</script>
</html>

完整代码
<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<style type="text/css">
			html,body{
				margin: 0;
				padding: 0;
				height: 100%;
				overflow: auto;
			}
			div{
				height: 300px;
			}
			.box1{
				background-color: #5F9EA0;
			}
			.box2{
				background-color: #87CEEB;
			}
			.box3{
				background-color: #8A2BE2;
			}
			.box4{
				background-color: #EBCB69;
			}
			.top{
				position: fixed;
				width: 50px;
				height: 50px;
				border-radius: 50%;
				bottom: 10px;
				right: 20px;
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 20px;
				color:  #87CEEB;
				background-color: #3b6aeb;
				cursor: pointer;
			}
		</style>
	</head>
	<body>
		<div class="box1"></div>
		<div class="box2"></div>
		<div class="box3"></div>
		<div class="box4"></div>
		<div class="top"></div>
	</body>
	<script type="text/javascript">
		const body = document.querySelector('body')
		body.addEventListener('scroll',e=>{
			const target = e.target //获取滚动对象
			const scrollTop = target.scrollTop //距离顶部距离
			const scrollHeight = target.scrollHeight //整个页面滚动高度
			const clientHeight = target.clientHeight //可视化页面高度
			//scrollTop+clientHeight = 页面已经滚动的高度
			if(scrollTop+clientHeight+20>=scrollHeight){ //距离底部20px时
			//创建div加入body中
				const div = document.createElement('div')
				const R = Math.floor(Math.random()*255+1)
				const G = Math.floor(Math.random()*255+1)
				const B = Math.floor(Math.random()*255+1)
				div.style.backgroundColor = `rgb(${R},${G},${B})`
				body.appendChild(div)
			}
		},false)
		
		const topTo = document.querySelector('.top')
		topTo.addEventListener('click',()=>{
			let scrollTop = body.scrollTop //已经滚动的距离
			const time = setInterval(()=>{ //循环定时器
				body.scrollTo(0,scrollTop) //滚动方法
				if(scrollTop>30){ //在大于30px时滚动速度较快
					scrollTop-=20
				}else{
					scrollTop--
				}
				if(scrollTop<=0) clearInterval(time) //在到顶部清楚定时器
			},1)
		},false)
	</script>
</html>

写这么多是不是挺麻烦,自己写滚动逻辑是为了可以自定义滚动的速度,其实scrollTo这个方法自带滚动过渡的只要加上一个属性就行。
topTo.addEventListener('click',()=>{
			body.scrollTo({behavior: "smooth",top:0})
			// let scrollTop = body.scrollTop //已经滚动的距离
			// const time = setInterval(()=>{ //循环定时器
			// 	body.scrollTo(0,scrollTop) //滚动方法
			// 	if(scrollTop>30){ //在大于30px时滚动速度较快
			// 		scrollTop-=20
			// 	}else{
			// 		scrollTop--
			// 	}
			// 	if(scrollTop<=0) clearInterval(time) //在到顶部清楚定时器
			// },1)
		},false)
效果相同
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值