0
点赞
收藏
分享

微信扫一扫

【仿淘宝侧边栏案例】

天使魔鬼 2022-04-14 阅读 55
javascript
<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title>仿淘宝滚动侧边栏</title>
		<style type="text/css">
			*{
				margin: 0;
				padding: 0;
			}
			.w{
				width:1600px;
				margin:10px auto 0px auto;
				
			}
			.header{
				height:200px;
				background: pink;
			}
			.banner{
				height:500px;
				background: purple;
			}
			.main{
				height:1500px;
				background:cornflowerblue;
			}
			.slyder-bar{
				position: relative;
				float: right;
				top:600px;
				right:98px;
				width:50px;
				height:200px;
				background:pink;
			}
			.goback{
				display: inline-block;
				text-align: center;
				line-height: 20px;
				width:40px;
				height: 40px;
				bottom:0px;
				position: absolute;
			}
		</style>
	</head>
	<body>
		<div class="slyder-bar">
			<span class="goback">返回顶部</span>
		</div>
		<div class="header w">header</div>
		<div class="banner w">banner</div>
		<div class="main w">main</div>
		
		<script type="text/javascript">
			var slyderbar=document.querySelector('.slyder-bar')
			var banner=document.querySelector('.banner');
			var bannerTop=banner.offsetTop;
			var slyderbarTop=slyderbar.offsetTop-bannerTop;
			var main=document.querySelector('.main');
			var goback=document.querySelector('.goback');
			var mainTop=main.offsetTop;
			document.addEventListener('scroll',function(){
				
				if(window.pageYOffset>=bannerTop){
					slyderbar.style.position='fixed';
					slyderbar.style.top=slyderbarTop+'px';
				}else{
					slyderbar.style.position="absolute";
					slyderbar.style.top='600px';
				}
				
				if(window.pageYOffset>=mainTop){
					goback.style.display="block";
				}else{
					goback.style.display="none";
				}
			})
		</script>
	</body>
</html>


 

举报

相关推荐

0 条评论