HTML5+CSS3+JS小實(shí)例:滾動(dòng)漸變導(dǎo)航欄

實(shí)例:滾動(dòng)漸變導(dǎo)航欄
技術(shù)棧: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>滾動(dòng)漸變導(dǎo)航欄</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="#">車主服務(wù)</a></li>
            <li><a href="#">科技創(chuàng)新</a></li>
            <li><a href="#">奔馳天下</a></li>
            <li><a href="#">預(yù)約品鑒</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】

/* 引入網(wǎng)絡(luò)字體(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{
    /* 默認(rèn)最小高度:2屏 */
    min-height: 200vh;
    background-color: #000;
}
header{
    /* 固定定位 */
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    /* 彈性布局 */
    display: flex;
    /* 將元素靠邊對(duì)齊 */
    justify-content: space-between;
    align-items: center;
    padding: 40px 100px;
    z-index: 1;
    /* 動(dòng)畫過渡 */
    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{
    /* 相對(duì)定位 */
    position: relative;
    width: 100%;
    height: 100vh;
    background: url("../images/car1.jpg") no-repeat;
    /* 保持原有尺寸比例,裁切長(zhǎng)邊 */
    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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容