备注:待重新编辑!
实现步骤
基本框架
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
</body>
</html>
全局样式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
</html>
固定导航栏
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
</html>
Logo 样式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
}
header .logo {
position: relative;
font-weight: 700;
color: #FFFFFF;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
</html>
导航栏右侧菜单样式
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
}
header .logo {
position: relative;
font-weight: 700;
color: #FFFFFF;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #FFFFFF;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.6s;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
</html>
背景图片
注意:需要修改 header
的 z-index
属性(第 28 行)。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header .logo {
position: relative;
font-weight: 700;
color: #FFFFFF;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #FFFFFF;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.6s;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url(img/bg.jpg);
background-size: cover;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
</html>
header - toggle class
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header .logo {
position: relative;
font-weight: 700;
color: #FFFFFF;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #FFFFFF;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.6s;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url(img/bg.jpg);
background-size: cover;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
<script type="text/javascript">
window.addEventListener("scroll", function() {
const header = document.querySelector("header")
header.classList.toggle("sticky", window.scrollY > 0)
})
</script>
</html>
最终效果 - 发生滚动之后,修改导航栏的样式
- 第 30 ~ 33 行
- 第 64 ~ 69 行
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Sticky Navigation Bar</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "courier new";
color: #FFFFFF;
}
body {
background: #000000;
min-height: 200vh;
}
header {
position: fixed;
top: 0;
left: 0;
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
transition: 0.6s;
padding: 40px 100px;
z-index: 100000;
}
header.sticky {
padding: 5px 100px;
background: #FFFFFF;
}
header .logo {
position: relative;
font-weight: 700;
color: #FFFFFF;
text-decoration: none;
font-size: 2em;
text-transform: uppercase;
letter-spacing: 2px;
transition: 0.6s;
}
header ul {
position: relative;
display: flex;
justify-content: center;
align-items: center;
}
header ul li {
position: relative;
list-style: none;
}
header ul li a {
position: relative;
margin: 0 15px;
text-decoration: none;
color: #FFFFFF;
letter-spacing: 2px;
font-weight: 500px;
transition: 0.6s;
}
header.sticky .logo {
color: #000000;
}
header.sticky ul li a {
color: #000000;
}
.banner {
position: relative;
width: 100%;
height: 100vh;
background: url(img/bg.jpg);
background-size: cover;
}
</style>
</head>
<body>
<header>
<a href="#" class="logo">Logo</a>
<ul>
<li>
<a href="#">Home</a>
</li>
<li>
<a href="#">About</a>
</li>
<li>
<a href="#">Contact</a>
</li>
</ul>
</header>
<section class="banner"></section>
</body>
<script type="text/javascript">
window.addEventListener("scroll", function() {
const header = document.querySelector("header")
header.classList.toggle("sticky", window.scrollY > 0)
})
</script>
</html>