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
Table of contents 1. Overview 1. Explain statemen...
1. Introduction Recently, I often encounter devel...
Rendering After looking up relevant information o...
Directory Structure . │ .env │ docker-compose.yml...
HTML is made up of tags and attributes, which are...
Table of contents Asynchronous traversal Asynchro...
CocosCreator realizes skill CD effect There are s...
1. v-on event monitoring To listen to DOM events,...
I just joined a new company recently. After getti...
Let's take an example: The code is very simple...
Table of contents What is index pushdown? The pri...
This article example shares the specific code of ...
In most cases, MySQL does not support Chinese whe...
Table of contents What is a relational database? ...
<br />This is from the content of Web front-...