HTML5+CSS3+JS小实例:滚动渐变导航栏

实例:滚动渐变导航栏
技术栈:HTML+CSS+JS
效果:

源码:
【html】【js】

<!DOCTYPE html>
<html>
 
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no">
 
    <title>滚动渐变导航栏</title>
    <link rel="stylesheet" href="../css/86.css">
</head>
 
<body>
    <header>
        <a href="#" class="logo">Logo</a>
        <ul>
            <li><a href="#">探索车型</a></li>
            <li><a href="#">官方商城</a></li>
            <li><a href="#">车主服务</a></li>
            <li><a href="#">科技创新</a></li>
            <li><a href="#">奔驰天下</a></li>
            <li><a href="#">预约品鉴</a></li>
        </ul>
    </header>
    <section class="banner1"></section>
    <section class="banner2"></section>
    <script>
        window.addEventListener("scroll",()=>{
            let header=document.querySelector("header");
            header.classList.toggle("sticky",window.scrollY>0);
        })
    </script>
</body>
 
</html>

【css】

/* 引入网络字体(Poppins) */
@import url("http://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700,800,900&display=swap");
 
*{
    /* 初始化 */
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: 'Poppins';
}
body{
    /* 默认最小高度:2屏 */
    min-height: 200vh;
    background-color: #000;
}
header{
    /* 固定定位 */
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    /* 弹性布局 */
    display: flex;
    /* 将元素靠边对齐 */
    justify-content: space-between;
    align-items: center;
    padding: 40px 100px;
    z-index: 1;
    /* 动画过渡 */
    transition: 0.6s;
}
header .logo{
    font-size: 32px;
    font-weight: 700;
    color: #fff;
    text-decoration: none;
    text-transform: uppercase;
    letter-spacing: 2px;
    transition: 0.6s;
}
header ul{
    display: flex;
    justify-content: center;
    align-items: center;
}
header ul li{
    list-style: none;
}
header ul li a{
    margin: 0 15px;
    text-decoration: none;
    color: #fff;
    letter-spacing: 2px;
    font-weight: 500;
    transition: 0.6s;
}
.banner1{
    /* 相对定位 */
    position: relative;
    width: 100%;
    height: 100vh;
    background: url("../images/car1.jpg") no-repeat;
    /* 保持原有尺寸比例,裁切长边 */
    background-size: cover;
    /* 图片定位正中间 */
    background-position: center center;
}
.banner2{
    position: relative;
    width: 100%;
    height: 100vh;
    background: url("../images/car2.jpg") no-repeat;
    background-size: cover;
    background-position: center center;
}
header.sticky{
    padding: 6px 100px;
    background-color: #fff;
}
header.sticky .logo,
header.sticky ul li a{
    color: #000;
}

【图片素材】
https://www.aliyundrive.com/s/542SW1jVZHy
https://www.aliyundrive.com/s/gfQSJKg1xUH

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容