滚动渐变导航栏

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

实现效果

准备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;
}
相关推荐
轻口味4 分钟前
AI 时代全栈开发破局:TypeScript 生态实战,从入门到部署一站式通关
前端·mongodb·docker·ai·typescript·react·next.js
ZC跨境爬虫13 分钟前
跟着MDN学HTML_day_45:(EventTarget接口)
前端·javascript·ui·html·媒体
漂移的电子26 分钟前
【el-tree】外层多选,某个属性内层单选
前端·javascript·vue.js
BJ-Giser37 分钟前
Cesium 体积光阴影率分析和阴影体渲染效果
前端·可视化·cesium
幽络源小助理41 分钟前
YK一点资讯Zblog主题源码, 游戏攻略新闻资讯模板
前端·php源码
RPGMZ43 分钟前
RPGMZ NPC头顶自动显示一段消息
前端·游戏引擎·rpgmz·rpgmakermz
DFT计算杂谈1 小时前
AMSET 设置多核并行计算
java·前端·css·html·css3
花椒技术1 小时前
AI 协同开发落地复盘:1 小时生成首版后,为什么 Review 和修正又花了 2-3 天
前端·人工智能·架构
万少1 小时前
万少用9个AI工具,帮朋友完成了一个"不可能"的项目
前端
小小小小宇1 小时前
Vue `import` 为什么可以异步加载
前端