<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
html {
scroll-behavior: smooth;
}
.content {
overflow: hidden;
width: 1000px;
height: 3000px;
background-color: pink;
margin: 0 auto;
}
.backtop {
display: none;
width: 50px;
left: 50%;
margin: 0 0 0 505px;
position: fixed;
bottom: 60px;
z-index: 100;
}
.backtop a {
height: 50px;
width: 50px;
background: url(./images/bg2.png) 0 -600px no-repeat;
opacity: 0.35;
overflow: hidden;
display: block;
text-indent: -999em;
cursor: pointer;
}
.header {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 80px;
background-color: purple;
text-align: center;
color: #fff;
line-height: 80px;
font-size: 30px;
transition: all .3s;
opacity: 1;
}
.sk {
width: 300px;
height: 300px;
background-color: skyblue;
margin-top: 500px;
}
</style>
</head>
<body>
<div class="header">我是顶部导航栏</div>
<div class="content">
<div class="sk">秒杀模块</div>
</div>
<div class="backtop">
<img src="./images/close2.png" alt="">
<a href="javascript:;"></a>
</div>
<script>
//获取事件源 顶部和秒杀
const header = document.querySelector('.header')
const sk = document.querySelector('.sk')
console.log(sk.offsetTop)
//注册事件 滑轮
window.addEventListener('scroll', function() {
//要获取当前页面的滚动条纵坐标位置,用:document.documentElement.scrollTop;
const n = document.documentElement.scrollTop
// 判断n >= sk.offsetTop(sk.offsetTop其实就是距顶部500px)
//然后用opacity是否隐藏或显示
header.style.opacity = n >= sk.offsetTop ? 1 : 0
})
</script>
</body>
</html>
仿京东头部固定导航栏(滑动滑轮顶部固定)
最新推荐文章于 2024-07-15 23:16:45 发布