JS realizes special effects of web page navigation bar

JS realizes special effects of web page navigation bar

This article shares with you a practical web navigation bar effect implemented with native JS. When the page scrolls, the navigation bar will change. The effect is as follows:

The following is the code implementation, you are welcome to copy, paste and collect it.

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Native JS to achieve web navigation bar special effects</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            word-wrap: break-word;
            font-family: 'Microsoft YaHei', sans-serif;
        }
 
        body {
            background: #000;
            min-height: 200vh;
        }
 
        header {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: space-between;
            transition: 0.6s;
            padding: 40px 100px;
            z-index: 2;
        }
 
        header.sticky {
            padding: 5px 100px;
            background: #fff;
        }
 
        header .logo {
            position: relative;
            font-weight: 700;
            color: #fff;
            text-decoration: none;
            font-size: 2em;
            text-transform:uppercase;
            letter-spacing: 2px;
            transition: 0;
        }
 
        header ul {
            position: relative;
            display: flex;
            justify-content: center;
            align-items: center;
        }
 
        header ul li {
            position: relative;
            list-style: none;
        }
 
        header ul li a {
            position: relative;
            margin: 0 15px;
            text-decoration: none;
            color: #fff;
            letter-spacing: 2px;
            font-weight: 500px;
            transition: 0.5s;
        }
 
 
        .banner {
            position: relative;
            width: 100%;
            height: 100vh;
            background: url(bg.jpg);
            background-size: cover;
        }
 
        header.sticky .logo,
        header.sticky ul li a {
            color: #000;
        }
    </style>
</head>
 
<body>
    <header>
        <a href="#" class="logo">Logo</a>
        <ul>
            <li><a href="#" >Home</a></li>
            <li><a href="#" >About</a></li>
            <li><a href="#" >Services</a></li>
            <li><a href="#" >Works</a></li>
            <li><a href="#" >Contact</a></li>
        </ul>
    </header>
    <section class="banner"></section>
    <script>
        window.addEventListener('scroll', () => {
            let header = document.querySelector('header')
            // Important properties header.classList.toggle('sticky', window.scrollY > 0)
        })
    </script>
</body>
 
</html>

The following is the picture bg.jpg referenced in the code

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • JS realizes the effect of highlighting the navigation bar after selecting the current menu
  • CSS3+Js to implement responsive navigation bar
  • Home page navigation bar DIV+CSS+JS [code example]
  • Implementing a pop-up floating menu in the navigation bar based on JS code
  • Vuejs toggle navigation bar highlight (route menu highlight) method example
  • JS+CSS realizes the effect of dynamic scrolling navigation bar when the mouse slides over
  • Detailed explanation of how to use the Bootstrap navigation bar JS component
  • JavaScript-implemented navigation bar that dynamically scrolls when the mouse hovers
  • js to achieve horizontal drag navigation bar function
  • Pure JS to achieve elastic navigation bar effect

<<:  Markup Language - Image Replacement

>>:  Docker connects to a container through a port

Recommend

Detailed explanation of the usage of MySQL memory tables and temporary tables

Usage of MySQL memory tables and temporary tables...

A Deep Dive into the MySQL InnoDB Storage Engine

Preface In MySQL, InnoDB belongs to the storage e...

Basic operations of mysql learning notes table

Create Table create table table name create table...

NestJs uses Mongoose to operate MongoDB

I recently started learning the NestJs framework....

A Preliminary Study on Vue Unit Testing

Table of contents Preface Why introduce unit test...

How to implement hot deployment and hot start in Eclipse/tomcat

1. Hot deployment: It means redeploying the entir...

Design theory: people-oriented green design

Reflections on the two viewpoints of “people-orie...

Implementation of React configuration sub-routing

1. The component First.js has subcomponents: impo...

How to point the target link of a tag to iframe

Copy code The code is as follows: <iframe id=&...

Native JS realizes uniform motion of various sports

This article shares with you a uniform motion imp...

Sharing experience on the priority of CSS style loading

During the project development yesterday, I encoun...

Detailed example of using if statement in mysql stored procedure

This article uses an example to illustrate the us...

How to solve mysql error 10061

This article shares with you the solution to the ...