在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
先看效果:实现:1.定义导航栏的文字标签: <div class="tou"> <sapn class="logo"> 北极光。</sapn> <ul class="biao"> <li><a href="#"><a href="#">主页</a></li> <li><a href="#">个人简介</a></li> <li><a href="#">文章</a></li> <li><a href="#">留言版</a></li> <li><a href="#">友链</a></li> </ul> </div> 2.导航栏整体的样式: .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:文字(字母)间距 4.给北极光logo定位一个图片在文字左边:
.logo::before{ content: ''; position: absolute; left: -50px; top: -15px; width: 50px; height: 50px; background-image: url(logo.png); background-size: 100%; } 5.右边导航标签的一些样式,样式等都不做详细说明了,毕竟每个人的都不一样~:
.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.当页面有滚动后导航栏的样式,padding上下变小,字体颜色变,有了蓝背景色: .bian{ padding: 15px 100px; background-color: rgb(71, 105, 219); } .bian .logo,.tou.bian a{ color: rgb(252, 247, 247); } 7.简单js,实现部分: window.addEventListener('scroll',function(){ let tou = document.querySelector('.tou'); if(window.scrollY>0) { tou.classList.add("bian"); }else{ tou.classList.remove("bian"); } }) 第二种:直接这样: window.addEventListener('scroll',function(){ let tou = document.querySelector('.tou'); tou.classList.toggle("bian",window.scrollY>0); }) 解释: classList属性: 所以: 完整代码: <!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); } /* 背景图样式 */ .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> <!-- 背景图 --> <div class="bjimg"></div> <!-- 导航栏 --> <div class="tou"> <sapn class="logo"> 北极光。</sapn> <ul class="biao"> <li><a href="#"><a href="#">主页</a></li> <li><a href="#">个人简介</a></li> <li><a href="#">文章</a></li> <li><a href="#">留言版</a></li> <li><a href="#">友链</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> 总结:到此这篇关于html+css+js实现导航栏滚动渐变效果的文章就介绍到这了,更多相关html css js 导航栏滚动渐变内容请搜索极客世界以前的文章或继续浏览下面的相关文章,希望大家以后多多支持极客世界! |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论