滚动渐变导航栏

实现导航栏固定顶部,且滚动渐变的效果

实现效果

准备html

vscode可利用快捷输入 header>a+ul>li*3>a+tab

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="style.css">
</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>
    <script type="text/javascript">
        window.addEventListener("scroll",function(){
            let header = document.querySelector("header");
            header.classList.toggle("sticky",window.scrollY>0)
        })
    </script>
</body>
</html>

准备css

css 复制代码
*{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}
body{
    background-color: #000;
    min-height: 200vh;
}
header{
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    transition: .6s;
    padding: 40px 100px;
    z-index: 99;
}
header.sticky{
    padding: 5px 100px;
    background: #fff;
}
header.sticky .logo,
header.sticky ul li a{
    color: #000;
}
header .logo{
    position: relative;
    font-weight: 700;
    color: #fff;
    text-decoration: none;
    font-size: 2em;
    text-transform: uppercase;
    letter-spacing: 2px;
    transition: .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: #fff;
    letter-spacing: 2px;
    font-weight: 500px;
    transition: .6s;
}
.banner{
    position: relative;
    width: 100%;
    height: 100vh;
    background: url("bg.png");
    background-size: cover;
}
相关推荐
东风西巷26 分钟前
PDFgear:免费全能的PDF处理工具
前端·pdf·软件需求
森之鸟1 小时前
Mac电脑上如何打印出字体图标
前端·javascript·macos
mCell1 小时前
GSAP 入门指南
前端·javascript·动效
gnip2 小时前
组件循环引用依赖问题处理
前端·javascript
Aotman_3 小时前
el-input textarea 禁止输入中文字符,@input特殊字符实时替换,光标位置保持不变
前端·javascript·vue.js·前端框架·es6
Nan_Shu_6143 小时前
Web前端面试题(1)
前端·面试·职场和发展
lypzcgf3 小时前
Coze源码分析-资源库-创建知识库-前端源码-核心组件
前端·typescript·react·coze·coze源码分析·ai应用平台·agent开发平台
百思可瑞教育4 小时前
在Vue项目中Axios发起请求时的小知识
前端·javascript·vue.js·北京百思教育
患得患失9494 小时前
【个人项目】【前端实用工具】OpenAPI to TypeScript 转换器
前端·javascript·typescript
大前端helloworld4 小时前
前端梳理体系从常问问题去完善-基础篇(html,css,js,ts)
前端·javascript·面试