HTML+CSS+JS realizes the scrolling gradient effect of the navigation bar

HTML+CSS+JS realizes the scrolling gradient effect of the navigation bar

First look at the effect:

insert image description here

accomplish:

1. Define the text label of the navigation bar:

<div class="tou">
        <sapn class="logo"> Northern Lights. </sapn>
        <ul class="biao">
        <li><a href="#"><a href="#">Home</a></li>
        <li><a href="#">Personal Profile</a></li>
        <li><a href="#">Article</a></li>
        <li><a href="#">Message Board</a></li>
        <li><a href="#">Friends Links</a></li>
        </ul>
    </div>

2. The overall style of the navigation bar:

.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. The style of the Northern Lights logo:

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

letter-spacing: text (letter) spacing

4. Position an image to the left of the text for the Northern Lights logo:

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

5. Some styles of the navigation labels on the right will not be described in detail, after all, everyone's styles are different~:

.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. When the page is scrolled, the navigation bar style, padding becomes smaller, the font color changes, and a blue background color appears:

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

7. Simple js, implementation part:
The first one:

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

The second method: directly like this:

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

        })

explain:
scrollY property:
The read-only scrollY property of the Window interface returns the number of pixels that the document is currently scrolled vertically.

classList attribute:
add(class1, class2, …) adds one or more class names to an element. If the specified class name already exists, it will not be added ;
remove(class1, class2, …) Removes one or more class names from an element.
toggle(class, true|false) The first parameter is the class name to be removed if it already exists , and returns false. If the class name does not exist, it will be added to the element and true will be returned. The second is an optional parameter, a Boolean value used to set whether the element is forced to add or remove the class , regardless of whether the class name exists.

so:
The first js writing method is to add the class .biao to achieve a gradient effect when the scroll is > 0, and remove the .biao class to return to the original state when the scroll is <= 0;
The second is a Boolean value judgment. When the scroll is > 0, the .biao class is added forcibly, and when the scroll is <= 0, the .biao class is removed;

Full code:

<!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);
        }
       /* Background image style */
        .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>
    <!-- Background image -->
    <div class="bjimg"></div>

   <!-- Navigation Bar -->
    <div class="tou">
        <sapn class="logo"> Northern Lights. </sapn>
        <ul class="biao">
        <li><a href="#"><a href="#">Home</a></li>
        <li><a href="#">Personal Profile</a></li>
        <li><a href="#">Article</a></li>
        <li><a href="#">Message Board</a></li>
        <li><a href="#">Friends Links</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>

Summarize:

This is the end of this article about how to achieve the scrolling gradient effect of the navigation bar with html+css+js. For more related html+css+js navigation bar scrolling gradient content, please search 123WORDPRESS.COM's previous articles or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future!

<<:  CSS implements the function of hiding the search box (animation forward and reverse sequence)

>>:  N ways to achieve two-column layout with CSS

Recommend

Basic tutorial on using explain statement in MySQL

Table of contents 1. Overview 1. Explain statemen...

A detailed analysis of the murder caused by a misplaced double quote in MySQL

1. Introduction Recently, I often encounter devel...

How to run MySQL using docker-compose

Directory Structure . │ .env │ docker-compose.yml...

Getting Started: A brief introduction to HTML's basic tags and attributes

HTML is made up of tags and attributes, which are...

Detailed explanation of the new features of ES9: Async iteration

Table of contents Asynchronous traversal Asynchro...

CocosCreator implements skill cooling effect

CocosCreator realizes skill CD effect There are s...

Summary of event handling in Vue.js front-end framework

1. v-on event monitoring To listen to DOM events,...

Steps for packaging and configuring SVG components in Vue projects

I just joined a new company recently. After getti...

Collapsed table row element bug

Let's take an example: The code is very simple...

Understanding MySQL index pushdown in five minutes

Table of contents What is index pushdown? The pri...

Vue realizes dynamic progress bar effect

This article example shares the specific code of ...

A permanent solution to MYSQL's inability to recognize Chinese

In most cases, MySQL does not support Chinese whe...

mysql8.0.23 linux (centos7) installation complete and detailed tutorial

Table of contents What is a relational database? ...

10 Deadly Semantic Mistakes in Web Typography

<br />This is from the content of Web front-...