导航栏滚动渐变效果 html+css+js

导航栏滚动渐变效果 html+css+js先看效果:实现:1.定义导航栏的文字标签:<divclass=”tou”><sapnclass=”logo”>北极光。</sapn><ulclass=”biao”><li><ahref=”#”><ahref=”#”>主页</a></li><li><ahref=”#”>个人简介</

大家好,又见面了,我是你们的朋友全栈君。

先看效果:

在这里插入图片描述

实现:

1.定义导航栏的文字标签:

<div class="tou">
        <sapn class="logo"> 北极光。</sapn>
        <ul class="biao">
        <li><a href="#"><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>
    </div>

2.导航栏整体的样式:

 .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.北极光这个logo的样式:

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

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

4.给北极光logo定位一个图片在文字左边:

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

5.右边导航标签的一些样式,样式等都不做详细说明了,毕竟每个人的都不一样~:

.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.当页面有滚动后导航栏的样式,padding上下变小,字体颜色变,有了蓝背景色:

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

7.简单js,实现部分:
第一种:

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属性返回文档当前垂直滚动的像素数。

classList属性:
add(class1, class2, …) 在元素中添加一个或多个类名。如果指定的类名已存在,则不会添加;
remove(class1, class2, …) 移除元素中一个或多个类名。
toggle(class, true|false) 第一个参数为如果已存在类名则中移除的类名,并返回 false。如果该类名不存在则会在元素中添加类名,并返回 true。第二个是可选参数,是个布尔值用于设置元素是否强制添加或移除类,不管该类名是否存在。

所以:
第一种js写法就是有滚动>0时就添加类.biao而实现渐变效果,当滚动<=0时就移除.biao类回到原来;
第二种就是布尔值判断,当滚动>0就强制添加.biao类,当滚动<=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>

   <!-- 导航栏 -->
    <div class="tou">
        <sapn class="logo"> 北极光。</sapn>
        <ul class="biao">
        <li><a href="#"><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>
    </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>

总结:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/140325.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • SPI接口调试

    SPI接口调试以MCP2515芯片的SPI接口为例说明。

    2022年6月18日
  • LC5软件激活成功教程用户口令[通俗易懂]

    LC5软件激活成功教程用户口令[通俗易懂]一、背景知识口令认证口令认证是身份认证的一种手段,计算机通过用户输入的用户名进行身份标识,通过访问·输入的口令对其是否拥有该用户对应的真实身份进行鉴别。口令攻击口令攻击可以通过强力攻击进行激活成功教程,也可以采用字典激活成功教程和字典混合激活成功教程的方法,根据是否掌握口令加密算法和口令数据的情况,采用在线激活成功教程和离线激活成功教程的方式。LC5LC5是一款口令激活成功教程工具,也可以被网络管理员用于检测Windows、Linux系统用户是否使用了不安全的密码,被普遍认为是当前最好、最快的Windows/Linux系统管理员账

    2022年7月24日
  • JavaScript 数组排序——快速排序[通俗易懂]

    JavaScript 数组排序——快速排序[通俗易懂]数组中的快速排序就是取原始数组中的一个元素最为基点,小于基点的放在一个数组中,大于基点的放在一个数组中,无限循环,知道将数组分解到长度(length<1)停止vararr=[12,3,569,78,0,-56,-56,-56,1223,11,16,13,1];functionquickSort(arr){if(arr.length<1)returnarr;分解数组,长度小于1的时候停止执行varmiddle=pa…

    2022年5月1日
  • Hadoop学习之编译eclipse插件

    Hadoop学习之编译eclipse插件

    2022年1月28日
  • exe4j的使用_使用方法

    exe4j的使用_使用方法exe4j的使用在这里选择exe填写你的exe名字和存放路径对软件的设置如果电脑是64位需要勾上防止软件乱码-Dfile.encoding=UTF-8导入jar包jdk版本允许最小和

    2022年8月5日
  • web api 学习 索引

    web api 学习 索引

    2021年9月13日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号