滚动固定导航栏示例

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>滚动固定导航栏示例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: Arial, sans-serif;
            line-height: 1.6;
            padding-top: 60px; /* 为固定导航栏预留空间 */
        }

        /* 导航栏样式 */
        .navbar {
            background-color: #333;
            color: white;
            padding: 15px 0;
            width: 100%;
            transition: all 0.3s ease;
        }

        .navbar.fixed {
            position: fixed;
            top: 0;
            left: 0;
            z-index: 1000;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 0 20px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }

        .logo {
            font-size: 24px;
            font-weight: bold;
        }

        .nav-links {
            display: flex;
            list-style: none;
        }

        .nav-links li {
            margin-left: 20px;
        }

        .nav-links a {
            color: white;
            text-decoration: none;
            transition: color 0.3s;
        }

        .nav-links a:hover {
            color: #4CAF50;
        }

        /* 内容区域样式 */
        .content {
            padding: 40px 0;
        }

        .section {
            height: 600px;
            padding: 40px;
            margin-bottom: 20px;
            background-color: #f4f4f4;
        }

        .footer {
            text-align: center;
            padding: 20px;
            background-color: #333;
            color: white;
        }

        .footer a {
            color: #4CAF50;
            text-decoration: none;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar">
        <div class="container">
            <div class="logo">网站LOGO</div>
            <ul class="nav-links">
                <li><a href="#home">首页</a></li>
                <li><a href="#about">关于</a></li>
                <li><a href="#services">服务</a></li>
                <li><a href="#contact">联系</a></li>
            </ul>
        </div>
    </nav>

    <!-- 内容区域 -->
    <div class="content">
        <section id="home" class="section">
            <h1>首页区域</h1>
            <p>向下滚动页面,导航栏会固定在顶部。</p>
        </section>

        <section id="about" class="section">
            <h1>关于我们</h1>
            <p>这里是关于我们的内容...</p>
        </section>

        <section id="services" class="section">
            <h1>我们的服务</h1>
            <p>这里是我们提供的服务内容...</p>
        </section>

        <section id="contact" class="section">
            <h1>联系我们</h1>
            <p>这里是联系方式...</p>
        </section>
    </div>

    <!-- 页脚 -->
    <footer class="footer">

    </footer>

    <!-- 引入jQuery -->
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

    <!-- 滚动固定导航栏的jQuery代码 -->
    <script>
        $(document).ready(function() {
            // 获取导航栏的初始位置
            var navbar = $('.navbar');
            var navbarOffsetTop = navbar.offset().top;

            // 滚动事件处理
            $(window).scroll(function() {
                // 检查滚动位置
                if ($(window).scrollTop() >= navbarOffsetTop) {
                    navbar.addClass('fixed');
                } else {
                    navbar.removeClass('fixed');
                }
            });

            // 平滑滚动到锚点
            $('a[href^="#"]').on('click', function(event) {
                event.preventDefault();
                var target = $(this.getAttribute('href'));
                if (target.length) {
                    $('html, body').stop().animate({
                        scrollTop: target.offset().top - navbar.outerHeight()
                    }, 800);
                }
            });
        });
    </script>
</body>
</html>

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值