网站顶部加个浏览器进度条代码

网站顶部加个浏览器进度条代码 - 全民博客

个人博客、个人网站,看那些WordPress主题、Emlog主题、ZBlog主题、Typecho主题模板站,浏览器顶部有个进度条效果,显得非常的好看和与众不同,下面是实现页面顶部进度条效果代码分享。

HTML
  1. <!DOCTYPE>
  2. <html>
  3. <head>
  4. <title>全民博客</title>
  5. </head>
  6. <style type="text/css">
  7. #bar{position:fixed;left:0;top:0;z-index:10000;width:100%;height:2px;-webkit-appearance:none;-moz-appearance:none;appearance:none;border:none;background-color:transparent;color:#f46}
  8. #bar::-webkit-progress-bar{background-color:transparent}
  9. #bar::-webkit-progress-value{background-color:#f46}
  10. #bar::-moz-progress-bar{background-color:#f46}
  11. </style>
  12. <body style="height:1000px;">
  13. <p style="display:flex;align-items:center;justify-content:center;height:98%;cursor:pointer;">鼠标滑轮往下滚动,网站顶部有一个进度条效果。</p>
  14. <script type="text/javascript">
  15. document.addEventListener('DOMContentLoaded', function () {
  16.       var ght = window.innerHeight,
  17.       doc = document.documentElement.scrollHeight,
  18.       bar = document.querySelector('#bar');
  19.       bar.max = doc - ght;
  20.       bar.value = window.scrollY;
  21.       document.addEventListener('scroll', function () {
  22.             bar.max = document.documentElement.scrollHeight - window.innerHeight;
  23.             bar.value = window.scrollY;
  24.       });
  25. });
  26. </script>
  27. <progress id="bar" value="0"></progress>
  28. </body>
  29. </html>

你看,代码是不是超级简单。看到了,就拔下来,做个记录,以后需要用的话,就阔以直接使用啦。

本文结束

评论专区

发表评论

评论列表(无评论)