佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > html+css+js實(shí)現(xiàn)導(dǎo)航欄滾動(dòng)漸變效果

html+css+js實(shí)現(xiàn)導(dǎo)航欄滾動(dòng)漸變效果

熱門標(biāo)簽:硅谷的囚徒呼叫中心 阿里云 百度競(jìng)價(jià)點(diǎn)擊價(jià)格的計(jì)算公式 網(wǎng)站建設(shè) 檢查注冊(cè)表項(xiàng) 使用U盤裝系統(tǒng) 智能手機(jī) 美圖手機(jī)

先看效果:

實(shí)現(xiàn):

1.定義導(dǎo)航欄的文字標(biāo)簽:

<div class="tou">
        <sapn class="logo"> 北極光。</sapn>
        <ul class="biao">
        <li><a href="#"><a href="#">主頁</a></li>
        <li><a href="#">個(gè)人簡(jiǎn)介</a></li>
        <li><a href="#">文章</a></li>
        <li><a href="#">留言版</a></li>
        <li><a href="#">友鏈</a></li>
        </ul>
    </div>

2.導(dǎo)航欄整體的樣式:

.tou{
             position: fixed;
             top: 0;
             left: 0;
             padding: 25px 100px;
             width: 100%;
             display: flex;
             justify-content: space-between;
             align-items: center;
            transition:  0.5s;
         }

transition 過渡效果
3.北極光這個(gè)logo的樣式:

 .logo{
             position: relative;
             font-size: 22px;
             font-weight: 900;
             letter-spacing: 1px;
             color: rgb(28, 36, 148);
         }

letter-spacing:文字(字母)間距

4.給北極光logo定位一個(gè)圖片在文字左邊:

 

.logo::before{
            content: '';
            position: absolute;
            left: -50px;
            top: -15px;
            width: 50px;
            height: 50px;
            background-image: url(logo.png);
            background-size: 100%;
         }

5.右邊導(dǎo)航標(biāo)簽的一些樣式,樣式等都不做詳細(xì)說明了,畢竟每個(gè)人的都不一樣~:

 

.biao{
             position: relative;
             display: flex;
             justify-content: center;
             align-content: center;
            list-style: none;
            
         }
        .biao li{
             position: relative;
         }
        .biao a{
             position: relative;
             margin: 0 10px;
             font-size: 18px;
             font-family: 'fangsong';
             font-weight: bold;
             color: rgb(28, 36, 148);
             text-decoration: none;

         }

6.當(dāng)頁面有滾動(dòng)后導(dǎo)航欄的樣式,padding上下變小,字體顏色變,有了藍(lán)背景色:

 .bian{
            padding: 15px 100px;
            background-color: rgb(71, 105, 219);
        }
        .bian .logo,.tou.bian a{
            color: rgb(252, 247, 247);
        }

7.簡(jiǎn)單js,實(shí)現(xiàn)部分:
第一種:

window.addEventListener('scroll',function(){
            let tou = document.querySelector('.tou');
           if(window.scrollY>0)
            {
                tou.classList.add("bian");
            }else{
                tou.classList.remove("bian");
            }
        })

第二種:直接這樣:

window.addEventListener('scroll',function(){
            let tou = document.querySelector('.tou');    
            tou.classList.toggle("bian",window.scrollY>0);

        })

解釋:
scrollY屬性:
Window接口的只讀scrollY屬性返回文檔當(dāng)前垂直滾動(dòng)的像素?cái)?shù)。

classList屬性:
add(class1, class2, …) 在元素中添加一個(gè)或多個(gè)類名。如果指定的類名已存在,則不會(huì)添加;
remove(class1, class2, …) 移除元素中一個(gè)或多個(gè)類名。
toggle(class, true|false) 第一個(gè)參數(shù)為如果已存在類名則中移除的類名,并返回 false。如果該類名不存在則會(huì)在元素中添加類名,并返回 true。第二個(gè)是可選參數(shù),是個(gè)布爾值用于設(shè)置元素是否強(qiáng)制添加或移除類,不管該類名是否存在。

所以:
第一種js寫法就是有滾動(dòng)>0時(shí)就添加類.biao而實(shí)現(xiàn)漸變效果,當(dāng)滾動(dòng)<=0時(shí)就移除.biao類回到原來;
第二種就是布爾值判斷,當(dāng)滾動(dòng)>0就強(qiáng)制添加.biao類,當(dāng)滾動(dòng)<=0就移除.biao類;

完整代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            
        }
        body{
            height: 200vh;
            
        }
        .tou{
             position: fixed;
             top: 0;
             left: 0;
             padding: 25px 100px;
             width: 100%;
             display: flex;
             justify-content: space-between;
             align-items: center;
            transition:  0.5s;
         }
        .logo{
             position: relative;
             font-size: 22px;
             font-weight: 900;
             letter-spacing: 1px;
             color: rgb(28, 36, 148);
         }
         .logo::before{
            content: '';
            position: absolute;
            left: -50px;
            top: -15px;
            width: 50px;
            height: 50px;
            background-image: url(logo.png);
            background-size: 100%;
         }
         .biao{
             position: relative;
             display: flex;
             justify-content: center;
             align-content: center;
            list-style: none;
            
         }
        .biao li{
             position: relative;
         }
        .biao a{
             position: relative;
             margin: 0 10px;
             font-size: 18px;
             font-family: 'fangsong';
             font-weight: bold;
             color: rgb(28, 36, 148);
             text-decoration: none;

         }
          
        .bian{
            padding: 15px 100px;
            background-color: rgb(71, 105, 219);
        }
        .bian .logo,.tou.bian a{
            color: rgb(252, 247, 247);
        }
       /*  背景圖樣式 */
        .bjimg {
      position: fixed;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      min-width: 1000px;
      z-index: -10;
      zoom: 1;
      background-color: #fff;
      background-image: url(11.jpg) ;
      background-repeat: no-repeat;
      background-size: cover;
      -webkit-background-size: cover;
      -o-background-size: cover;
      background-position: center 0;
    }

    </style>
</head>
<body>
    <!-- 背景圖 -->
    <div class="bjimg"></div>

   <!--  導(dǎo)航欄 -->
    <div class="tou">
        <sapn class="logo"> 北極光。</sapn>
        <ul class="biao">
        <li><a href="#"><a href="#">主頁</a></li>
        <li><a href="#">個(gè)人簡(jiǎn)介</a></li>
        <li><a href="#">文章</a></li>
        <li><a href="#">留言版</a></li>
        <li><a href="#">友鏈</a></li>
        </ul>
    </div>
    <script>
        window.addEventListener('scroll',function(){
            let tou = document.querySelector('.tou');
            
           
           /*  tou.classList.toggle("bian",window.scrollY>0); */
           if(window.scrollY>0)
            {
                tou.classList.add("bian");
            }else{
                tou.classList.remove("bian");
            }
        })
    </script>
</body>
</html>

總結(jié):

到此這篇關(guān)于html+css+js實(shí)現(xiàn)導(dǎo)航欄滾動(dòng)漸變效果的文章就介紹到這了,更多相關(guān)html css js 導(dǎo)航欄滾動(dòng)漸變內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

標(biāo)簽:湘潭 黃山 湖北 賀州 通遼 懷化 煙臺(tái) 山南

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《html+css+js實(shí)現(xiàn)導(dǎo)航欄滾動(dòng)漸變效果》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    永年县| 稻城县| 连平县| 黔西县| 东城区| 乌拉特中旗| 车致| 石景山区| 五莲县| 鞍山市| 曲周县| 长子县| 大渡口区| 星子县| 瑞昌市| 交口县| 耒阳市| 逊克县| 苗栗市| 三门县| 盐源县| 肇源县| 日喀则市| 林口县| 永安市| 开阳县| 南郑县| 开远市| 噶尔县| 达日县| 北川| 云浮市| 灌南县| 漯河市| 阳东县| 霞浦县| 扶余县| 德格县| 怀安县| 渝北区| 昌平区|