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
1. What is In react applications, event names are...
Table of contents 1. Inline styles 2. Use import ...
This article shares the data display code for Jav...
Table of contents 1. What is an index? 2. Why do ...
1. Background Netplan is a new command-line netwo...
Table of contents 1. Environment Configuration 1....
1. What is HTML markup language? HTML is a markup...
Table of contents Actual combat process Let's...
When we learn HTML, the image tag <img> int...
Excel export always fails in the docker environme...
In fact, this is also a clickbait title, and it c...
<br /> When we browse certain websites and s...
Delete a file by its inode number First use ls -i...
Verification environment: [root@~~/]# rpm -qa | g...