CSS & JavaScript - Sticky Navigation Bar 粘性导航栏


备注:待重新编辑!

实现步骤

基本框架

<!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>

在这里插入图片描述

背景图片

注意:需要修改 headerz-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>

在这里插入图片描述

参考

滚动渐变导航栏

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值