经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » HTML/CSS » CSS » 查看文章
html+css+js实现导航栏滚动渐变效果_HTML/Xhtml
来源:jb51  时间:2021/2/1 11:31:23  对本文有异议

先看效果:

在这里插入图片描述

实现:

1.定义导航栏的文字标签:

  1. <div class="tou">
  2. <sapn class="logo"> 北极光。</sapn>
  3. <ul class="biao">
  4. <li><a href="#"><a href="#">主页</a></li>
  5. <li><a href="#">个人简介</a></li>
  6. <li><a href="#">文章</a></li>
  7. <li><a href="#">留言版</a></li>
  8. <li><a href="#">友链</a></li>
  9. </ul>
  10. </div>

2.导航栏整体的样式:

  1. .tou{
  2. position: fixed;
  3. top: 0;
  4. left: 0;
  5. padding: 25px 100px;
  6. width: 100%;
  7. display: flex;
  8. justify-content: space-between;
  9. align-items: center;
  10. transition: 0.5s;
  11. }

transition 过渡效果
3.北极光这个logo的样式:

  1. .logo{
  2. position: relative;
  3. font-size: 22px;
  4. font-weight: 900;
  5. letter-spacing: 1px;
  6. color: rgb(28, 36, 148);
  7. }

letter-spacing:文字(字母)间距

4.给北极光logo定位一个图片在文字左边:

 

  1. .logo::before{
  2. content: '';
  3. position: absolute;
  4. left: -50px;
  5. top: -15px;
  6. width: 50px;
  7. height: 50px;
  8. background-image: url(logo.png);
  9. background-size: 100%;
  10. }

5.右边导航标签的一些样式,样式等都不做详细说明了,毕竟每个人的都不一样~:

 

  1. .biao{
  2. position: relative;
  3. display: flex;
  4. justify-content: center;
  5. align-content: center;
  6. list-style: none;
  7. }
  8. .biao li{
  9. position: relative;
  10. }
  11. .biao a{
  12. position: relative;
  13. margin: 0 10px;
  14. font-size: 18px;
  15. font-family: 'fangsong';
  16. font-weight: bold;
  17. color: rgb(28, 36, 148);
  18. text-decoration: none;
  19.  
  20. }

6.当页面有滚动后导航栏的样式,padding上下变小,字体颜色变,有了蓝背景色:

  1. .bian{
  2. padding: 15px 100px;
  3. background-color: rgb(71, 105, 219);
  4. }
  5. .bian .logo,.tou.bian a{
  6. color: rgb(252, 247, 247);
  7. }

7.简单js,实现部分:
第一种:

  1. window.addEventListener('scroll',function(){
  2. let tou = document.querySelector('.tou');
  3. if(window.scrollY>0)
  4. {
  5. tou.classList.add("bian");
  6. }else{
  7. tou.classList.remove("bian");
  8. }
  9. })

第二种:直接这样:

  1. window.addEventListener('scroll',function(){
  2. let tou = document.querySelector('.tou');
  3. tou.classList.toggle("bian",window.scrollY>0);
  4.  
  5. })

解释:
scrollY属性:
Window接口的只读scrollY属性返回文档当前垂直滚动的像素数。

classList属性:
add(class1, class2, …) 在元素中添加一个或多个类名。如果指定的类名已存在,则不会添加
remove(class1, class2, …) 移除元素中一个或多个类名。
toggle(class, true|false) 第一个参数为如果已存在类名则中移除的类名,并返回 false。如果该类名不存在则会在元素中添加类名,并返回 true。第二个是可选参数,是个布尔值用于设置元素是否强制添加或移除类,不管该类名是否存在。

所以:
第一种js写法就是有滚动>0时就添加类.biao而实现渐变效果,当滚动<=0时就移除.biao类回到原来;
第二种就是布尔值判断,当滚动>0就强制添加.biao类,当滚动<=0就移除.biao类;

完整代码:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. <style>
  8. *{
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body{
  14. height: 200vh;
  15. }
  16. .tou{
  17. position: fixed;
  18. top: 0;
  19. left: 0;
  20. padding: 25px 100px;
  21. width: 100%;
  22. display: flex;
  23. justify-content: space-between;
  24. align-items: center;
  25. transition: 0.5s;
  26. }
  27. .logo{
  28. position: relative;
  29. font-size: 22px;
  30. font-weight: 900;
  31. letter-spacing: 1px;
  32. color: rgb(28, 36, 148);
  33. }
  34. .logo::before{
  35. content: '';
  36. position: absolute;
  37. left: -50px;
  38. top: -15px;
  39. width: 50px;
  40. height: 50px;
  41. background-image: url(logo.png);
  42. background-size: 100%;
  43. }
  44. .biao{
  45. position: relative;
  46. display: flex;
  47. justify-content: center;
  48. align-content: center;
  49. list-style: none;
  50. }
  51. .biao li{
  52. position: relative;
  53. }
  54. .biao a{
  55. position: relative;
  56. margin: 0 10px;
  57. font-size: 18px;
  58. font-family: 'fangsong';
  59. font-weight: bold;
  60. color: rgb(28, 36, 148);
  61. text-decoration: none;
  62.  
  63. }
  64. .bian{
  65. padding: 15px 100px;
  66. background-color: rgb(71, 105, 219);
  67. }
  68. .bian .logo,.tou.bian a{
  69. color: rgb(252, 247, 247);
  70. }
  71. /* 背景图样式 */
  72. .bjimg {
  73. position: fixed;
  74. top: 0;
  75. left: 0;
  76. width: 100%;
  77. height: 100%;
  78. min-width: 1000px;
  79. z-index: -10;
  80. zoom: 1;
  81. background-color: #fff;
  82. background-image: url(11.jpg) ;
  83. background-repeat: no-repeat;
  84. background-size: cover;
  85. -webkit-background-size: cover;
  86. -o-background-size: cover;
  87. background-position: center 0;
  88. }
  89.  
  90. </style>
  91. </head>
  92. <body>
  93. <!-- 背景图 -->
  94. <div class="bjimg"></div>
  95.  
  96. <!-- 导航栏 -->
  97. <div class="tou">
  98. <sapn class="logo"> 北极光。</sapn>
  99. <ul class="biao">
  100. <li><a href="#"><a href="#">主页</a></li>
  101. <li><a href="#">个人简介</a></li>
  102. <li><a href="#">文章</a></li>
  103. <li><a href="#">留言版</a></li>
  104. <li><a href="#">友链</a></li>
  105. </ul>
  106. </div>
  107. <script>
  108. window.addEventListener('scroll',function(){
  109. let tou = document.querySelector('.tou');
  110. /* tou.classList.toggle("bian",window.scrollY>0); */
  111. if(window.scrollY>0)
  112. {
  113. tou.classList.add("bian");
  114. }else{
  115. tou.classList.remove("bian");
  116. }
  117. })
  118. </script>
  119. </body>
  120. </html>

总结:

到此这篇关于html+css+js实现导航栏滚动渐变效果的文章就介绍到这了,更多相关html css js 导航栏滚动渐变内容请搜索w3xue以前的文章或继续浏览下面的相关文章,希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号