当前位置:Gxlcms > html代码 > 滚动条动态固定顶部的实例代码分享

滚动条动态固定顶部的实例代码分享

时间:2021-07-01 10:21:17 帮助过:28人阅读

滚动条动态固定顶部的实例代码分享,代码如下:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>js滚动到指定位置导航栏固定顶部</title>
  6. <style type="text/css">
  7. body{height: 2500px; margin: 0; padding: 0;}
  8. .banner{height: 250px; width: 100%; background: #e5e5e5;}
  9. .bignav{width: 100%; background: #000;}
  10. .nav{ background:#000; width: 1200px; margin: 0 auto; height: 45px;}
  11. .nav a{display: block; width: 200px;float: left; color: #fff; text-decoration: none; text-align: center; line-height: 45px;}
  12. </style>
  13. </head>
  14. <body>
  15. <div class="banner">
  16. </div>
  17. <div class="bignav" id="bignav">
  18. <div class="nav">
  19. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
  20. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
  21. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
  22. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
  23. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
  24. <a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >首页</a>
  25. </div>
  26. </div>
  27. <p>其他内容</p>
  28. <p>其他内容</p>
  29. <p>其他内容</p>
  30. <p>其他内容</p>
  31. <p>其他内容</p>
  32. <p>其他内容</p>
  33. <p>其他内容</p>
  34. <p>其他内容</p>
  35. <p>其他内容</p>
  36. <p>其他内容</p>
  37. <p>其他内容</p>
  38. <p>其他内容</p>
  39. <p>其他内容</p>
  40. <p>其他内容</p>
  41. <p>其他内容</p>
  42. <p>其他内容</p>
  43. <p>其他内容</p>
  44. <p>其他内容</p>
  45. <p>其他内容</p>
  46. <p>其他内容</p>
  47. <p>其他内容</p>
  48. <p>其他内容</p>
  49. <p>其他内容</p>
  50. <p>其他内容</p>
  51. <p>其他内容</p>
  52. <p>其他内容</p>
  53. <p>其他内容</p>
  54. <p>其他内容</p>
  55. <p>其他内容</p>
  56. <p>其他内容</p>
  57. <p>其他内容</p>
  58. <p>其他内容</p>
  59. <p>其他内容</p>
  60. <p>其他内容</p>
  61. <p>其他内容</p>
  62. <p>其他内容</p>
  63. <p>其他内容</p>
  64. <p>其他内容</p>
  65. <p>其他内容</p>
  66. <p>其他内容</p>
  67. <p>其他内容</p>
  68. <p>其他内容</p>
  69. <p>其他内容</p>
  70. <p>其他内容</p>
  71. <p>其他内容</p>
  72. <p>其他内容</p>
  73. <p>其他内容</p>
  74. <p>其他内容</p>
  75. <script type="text/javascript">
  76. window.onscroll=function(){
  77. var topScroll =document.body.scrollTop;//滚动的距离,距离顶部的距离
  78. var bignav = document.getElementById("bignav");//获取到导航栏id
  79. if(topScroll > 250){ //当滚动距离大于250px时执行下面的东西
  80. bignav.style.position = 'fixed';
  81. bignav.style.top = '0';
  82. bignav.style.zIndex = '9999';
  83. }else{//当滚动距离小于250的时候执行下面的内容,也就是让导航栏恢复原状
  84. bignav.style.position = 'static';
  85. }
  86. }
  87. </script>
  88. </body>
  89. </html>

以上就是滚动条动态固定顶部的实例代码分享的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行