[前端] html+css+js实现导航栏滚动渐变效果

1650 0
王子 2022-10-19 15:35:34 | 显示全部楼层 |阅读模式
目录

    先看效果:实现:总结:


先看效果:




实现:

1.定义导航栏的文字标签:
  1. <div class="tou">
  2.         <sapn class="logo"> 北极光。</sapn>
  3.         <ul class="biao">
  4.         <li><a href="#"><a href="#">主页</a></li>
  5.         <li><a href="#">个人简介</a></li>
  6.         <li><a href="#">文章</a></li>
  7.         <li><a href="#">留言版</a></li>
  8.         <li><a href="#">友链</a></li>
  9.         </ul>
  10.     </div>
复制代码
2.导航栏整体的样式:
  1. .tou{
  2.              position: fixed;
  3.              top: 0;
  4.              left: 0;
  5.              padding: 25px 100px;
  6.              width: 100%;
  7.              display: flex;
  8.              justify-content: space-between;
  9.              align-items: center;
  10.             transition:  0.5s;
  11.          }
复制代码
transition 过渡效果
3.北极光这个logo的样式:
  1. .logo{
  2.              position: relative;
  3.              font-size: 22px;
  4.              font-weight: 900;
  5.              letter-spacing: 1px;
  6.              color: rgb(28, 36, 148);
  7.          }
复制代码
letter-spacing:文字(字母)间距
4.给北极光logo定位一个图片在文字左边:
  1. .logo::before{
  2.             content: '';
  3.             position: absolute;
  4.             left: -50px;
  5.             top: -15px;
  6.             width: 50px;
  7.             height: 50px;
  8.             background-image: url(logo.png);
  9.             background-size: 100%;
  10.          }
复制代码
5.右边导航标签的一些样式,样式等都不做详细说明了,毕竟每个人的都不一样~:
  1. .biao{
  2.              position: relative;
  3.              display: flex;
  4.              justify-content: center;
  5.              align-content: center;
  6.             list-style: none;
  7.          }
  8.         .biao li{
  9.              position: relative;
  10.          }
  11.         .biao a{
  12.              position: relative;
  13.              margin: 0 10px;
  14.              font-size: 18px;
  15.              font-family: 'fangsong';
  16.              font-weight: bold;
  17.              color: rgb(28, 36, 148);
  18.              text-decoration: none;
  19.          }
复制代码
6.当页面有滚动后导航栏的样式,padding上下变小,字体颜色变,有了蓝背景色:
  1. .bian{
  2.             padding: 15px 100px;
  3.             background-color: rgb(71, 105, 219);
  4.         }
  5.         .bian .logo,.tou.bian a{
  6.             color: rgb(252, 247, 247);
  7.         }
复制代码
7.简单js,实现部分:
第一种:
  1. window.addEventListener('scroll',function(){
  2.             let tou = document.querySelector('.tou');
  3.            if(window.scrollY>0)
  4.             {
  5.                 tou.classList.add("bian");
  6.             }else{
  7.                 tou.classList.remove("bian");
  8.             }
  9.         })
复制代码
第二种:直接这样:
  1. window.addEventListener('scroll',function(){
  2.             let tou = document.querySelector('.tou');   
  3.             tou.classList.toggle("bian",window.scrollY>0);
  4.         })
复制代码
解释:
scrollY属性:
Window接口的只读scrollY属性返回文档当前垂直滚动的像素数。
classList属性:
add(class1, class2, …) 在元素中添加一个或多个类名。如果指定的类名已存在,则不会添加
remove(class1, class2, …) 移除元素中一个或多个类名。
toggle(class, true|false) 第一个参数为如果已存在类名则中移除的类名,并返回 false。如果该类名不存在则会在元素中添加类名,并返回 true。第二个是可选参数,是个布尔值用于设置元素是否强制添加或移除类,不管该类名是否存在。
所以:
第一种js写法就是有滚动>0时就添加类.biao而实现渐变效果,当滚动<=0时就移除.biao类回到原来;
第二种就是布尔值判断,当滚动>0就强制添加.biao类,当滚动<=0就移除.biao类;
完整代码:
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Document</title>
  7.     <style>
  8.         *{
  9.             margin: 0;
  10.             padding: 0;
  11.             box-sizing: border-box;
  12.         }
  13.         body{
  14.             height: 200vh;
  15.         }
  16.         .tou{
  17.              position: fixed;
  18.              top: 0;
  19.              left: 0;
  20.              padding: 25px 100px;
  21.              width: 100%;
  22.              display: flex;
  23.              justify-content: space-between;
  24.              align-items: center;
  25.             transition:  0.5s;
  26.          }
  27.         .logo{
  28.              position: relative;
  29.              font-size: 22px;
  30.              font-weight: 900;
  31.              letter-spacing: 1px;
  32.              color: rgb(28, 36, 148);
  33.          }
  34.          .logo::before{
  35.             content: '';
  36.             position: absolute;
  37.             left: -50px;
  38.             top: -15px;
  39.             width: 50px;
  40.             height: 50px;
  41.             background-image: url(logo.png);
  42.             background-size: 100%;
  43.          }
  44.          .biao{
  45.              position: relative;
  46.              display: flex;
  47.              justify-content: center;
  48.              align-content: center;
  49.             list-style: none;
  50.          }
  51.         .biao li{
  52.              position: relative;
  53.          }
  54.         .biao a{
  55.              position: relative;
  56.              margin: 0 10px;
  57.              font-size: 18px;
  58.              font-family: 'fangsong';
  59.              font-weight: bold;
  60.              color: rgb(28, 36, 148);
  61.              text-decoration: none;
  62.          }
  63.         .bian{
  64.             padding: 15px 100px;
  65.             background-color: rgb(71, 105, 219);
  66.         }
  67.         .bian .logo,.tou.bian a{
  68.             color: rgb(252, 247, 247);
  69.         }
  70.        /*  背景图样式 */
  71.         .bjimg {
  72.       position: fixed;
  73.       top: 0;
  74.       left: 0;
  75.       width: 100%;
  76.       height: 100%;
  77.       min-width: 1000px;
  78.       z-index: -10;
  79.       zoom: 1;
  80.       background-color: #fff;
  81.       background-image: url(11.jpg) ;
  82.       background-repeat: no-repeat;
  83.       background-size: cover;
  84.       -webkit-background-size: cover;
  85.       -o-background-size: cover;
  86.       background-position: center 0;
  87.     }
  88.     </style>
  89. </head>
  90. <body>
  91.     <!-- 背景图 -->
  92.     <div class="bjimg"></div>
  93.    <!--  导航栏 -->
  94.     <div class="tou">
  95.         <sapn class="logo"> 北极光。</sapn>
  96.         <ul class="biao">
  97.         <li><a href="#"><a href="#">主页</a></li>
  98.         <li><a href="#">个人简介</a></li>
  99.         <li><a href="#">文章</a></li>
  100.         <li><a href="#">留言版</a></li>
  101.         <li><a href="#">友链</a></li>
  102.         </ul>
  103.     </div>
  104.     <script>
  105.         window.addEventListener('scroll',function(){
  106.             let tou = document.querySelector('.tou');
  107.            /*  tou.classList.toggle("bian",window.scrollY>0); */
  108.            if(window.scrollY>0)
  109.             {
  110.                 tou.classList.add("bian");
  111.             }else{
  112.                 tou.classList.remove("bian");
  113.             }
  114.         })
  115.     </script>
  116. </body>
  117. </html>
复制代码
总结:

到此这篇关于html+css+js实现导航栏滚动渐变效果的文章就介绍到这了,更多相关html css js 导航栏滚动渐变内容请搜索中国红客联盟以前的文章或继续浏览下面的相关文章,希望大家以后多多支持中国红客联盟!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行