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:
|
<<: Markup Language - Image Replacement
>>: Docker connects to a container through a port
Usage of MySQL memory tables and temporary tables...
Preface In MySQL, InnoDB belongs to the storage e...
1. Problem The project developed using Eclipse on...
Create Table create table table name create table...
When starting MongoDB, the prompt is: error while...
I recently started learning the NestJs framework....
Table of contents Preface Why introduce unit test...
1. Hot deployment: It means redeploying the entir...
Reflections on the two viewpoints of “people-orie...
1. The component First.js has subcomponents: impo...
Copy code The code is as follows: <iframe id=&...
This article shares with you a uniform motion imp...
During the project development yesterday, I encoun...
This article uses an example to illustrate the us...
This article shares with you the solution to the ...