欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

  發(fā)布時(shí)間:2021-01-28 16:28:57   作者:北極光之夜。   我要評論
這篇文章主要介紹了html+css+js實(shí)現(xiàn)導(dǎo)航欄滾動漸變效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

先看效果:

在這里插入圖片描述

實(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è)人簡介</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ǎ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.簡單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)前垂直滾動的像素?cái)?shù)。

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

所以:
第一種js寫法就是有滾動>0時(shí)就添加類.biao而實(shí)現(xiàn)漸變效果,當(dāng)滾動<=0時(shí)就移除.biao類回到原來;
第二種就是布爾值判斷,當(dāng)滾動>0就強(qiáng)制添加.biao類,當(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è)人簡介</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)航欄滾動漸變效果的文章就介紹到這了,更多相關(guān)html css js 導(dǎo)航欄滾動漸變內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持腳本之家!

相關(guān)文章

  • HTML+CSS 實(shí)現(xiàn)頂部導(dǎo)航欄菜單制作

    導(dǎo)航對于任何網(wǎng)站都很重要,本文主要介紹了HTML+CSS 實(shí)現(xiàn)頂部導(dǎo)航欄菜單制作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-03
  • CSS 帶搜索導(dǎo)航欄的示例代碼

    這篇文章主要介紹了CSS 帶搜索導(dǎo)航欄的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)
    2021-02-22
  • html+css 實(shí)現(xiàn)簡易導(dǎo)航欄功能

    這篇文章主要介紹了基于html+css 實(shí)現(xiàn)簡易導(dǎo)航欄功能,主要就是css(級聯(lián)樣式表)對html的內(nèi)容做格式化。具體內(nèi)容詳情大家跟隨小編一起通過本文學(xué)習(xí)吧
    2021-04-07
  • 純CSS實(shí)現(xiàn)導(dǎo)航欄下劃線跟隨的示例代碼

    這篇文章主要介紹了純CSS實(shí)現(xiàn)導(dǎo)航欄下劃線跟隨的示例代碼的相關(guān)資料,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-04-11
  • CSS+HTML 實(shí)現(xiàn)頂部導(dǎo)航欄功能

    這篇文章主要介紹了CSS+HTML 實(shí)現(xiàn)頂部導(dǎo)航欄功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-08-24

最新評論