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 React event binding

1. What is In react applications, event names are...

How to write CSS elegantly with react

Table of contents 1. Inline styles 2. Use import ...

JavaScript to implement search data display

This article shares the data display code for Jav...

Analysis of the reasons why MySQL's index system uses B+ tree

Table of contents 1. What is an index? 2. Why do ...

How to set static IP for Ubuntu 18.04 Server

1. Background Netplan is a new command-line netwo...

Detailed process of NTP server configuration under Linux

Table of contents 1. Environment Configuration 1....

HTML Learning Notes--Detailed Explanation of HTML Syntax (Must Read)

1. What is HTML markup language? HTML is a markup...

Docker starts MySQL configuration implementation process

Table of contents Actual combat process Let's...

How to introduce img images into Vue pages

When we learn HTML, the image tag <img> int...

Excel export always fails in docker environment

Excel export always fails in the docker environme...

5 cool and practical HTML tags and attributes introduction

In fact, this is also a clickbait title, and it c...

Two simple ways to remove text watermarks from web pages

<br /> When we browse certain websites and s...

How to delete special character file names or directories in Linux

Delete a file by its inode number First use ls -i...

...