<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<style>
* {
padding: 0;
margin: 0;
}
.nav {
position: fixed;
left: 0;
top: 0;
right: 0;
transition: all .3s;
line-height: 100px;
}
.nav .container {
width: 1100px;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
.nav .container .left {
font-size: 2em;
color: #FFDE00;
}
.nav .container .left:hover {
color: #fec503
}
.nav .container .menu{
display: flex;
color: #fff;
}
.nav .container .menu-item{
margin-left: 50px;
}
.nav.active {
line-height: 50px;
background-color: #fff;
height: 50px;
border-bottom: 2px solid #FFDE00;
}
.nav.active .container .menu{
display: flex;
line-height:50px;
color: #000;
}
</style>
<div class="nav">
<div class="container">
<div class="left">网站名称</div>
<div class="menu">
<div class="menu-item">首页</div>
<div class="menu-item">产品</div>
<div class="menu-item">新闻</div>
<div class="menu-item">关于我们</div>
</div>
</div>
</div>
<div style="background-color: blanchedalmond;height: 3100px">
<img src="http://project-all.oss-cn-beijing.aliyuncs.com/jianzhan/20230422/173046_978776.jpg" style="width: 100%">
</div>
<script>
let cbpAnimatedHeader = function () {
let docElem = document.documentElement;
let header = document.querySelector('.nav');
let didScroll = false;
let changeHeaderOn = 150;
function init() {
window.addEventListener('scroll', function (event) {
if (!didScroll) {
didScroll = true;
setTimeout(scrollPage, 250);
}
}, false);
}
init();
function scrollPage() {
let sy = window.pageYOffset || docElem.scrollTop;
if (sy >= changeHeaderOn) {
header.classList.add('active')
} else {
header.classList.remove('active')
}
didScroll = false;
}
}
cbpAnimatedHeader()
</script>
</body>
</html>
网友评论