First look at the effect: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 .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: 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: classList attribute: so: 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
This article records the installation and configu...
Table of contents Problem Description 1. Basic so...
During this period of time, while working on a pr...
The cascading drop-down menu developed in this ex...
1. Single column index Choosing which columns to ...
When we develop a single-page application, someti...
I designed and customized a navigation bar with a...
I once promised that I would keep writing until pe...
Recently, there is a particularly abnormal busine...
Table of contents Typical Cases Appendix: Common ...
Table of contents cache Cache location classifica...
Table of contents 1. Environmental Preparation 2....
If there is a backup, it is very simple. You only...
Preparation 1. Start the virtual machine 2. git t...
Project scenario: There is a <ul> tag on th...