仿淘宝侧边栏
程序员文章站
2024-03-20 21:13:34
...
仿淘宝侧边栏
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.slider-bar {
position: absolute;
left: 50%;
top: 300px;
margin-left: 600px;
width: 45px;
height: 130px;
background-color: pink;
}
.w {
width: 1200px;
margin: 10px auto;
}
.header {
height: 150px;
background-color: purple;
}
.banner {
height: 250px;
background-color: skyblue;
}
.main {
height: 1000px;
background-color: yellowgreen;
}
span {
display: none;
position: absolute;
bottom: 0;
}
</style>
</head>
<body>
<div class="slider-bar">
<span class="goBack">返回顶部</span>
</div>
<div class="header w">头部区域</div>
<div class="banner w">banner区域</div>
<div class="main w">头部区域</div>
<script>
var slideBar = document.querySelector(".slider-bar");
var banner = document.querySelector(".banner");
var main = document.querySelector(".main");
var goBack = document.querySelector(".goBack");
var bannerTop = banner.offsetTop;
var fixedTop = slideBar.offsetTop - bannerTop;
var mainTop = main.offsetTop;
console.log(document.documentElement);
document.addEventListener("scroll", function() {
if (window.pageYOffset > bannerTop) {
slideBar.style.position = "fixed";
slideBar.style.top = fixedTop + 'px';
} else {
slideBar.style.position = "absolute";
slideBar.style.top = '300px';
}
if (window.pageYOffset > mainTop) {
goBack.style.display = "block"
} else {
goBack.style.display = "none"
}
});
</script>
</body>
</html>
上一篇: python前后端分离-Vue框架一