github回到顶部

github向下滚动时,出现回到顶部按钮,点击回到顶部

  1. // ==UserScript==
  2. // @name github回到顶部
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description github向下滚动时,出现回到顶部按钮,点击回到顶部
  6. // @author wxb
  7. // @match https://github.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13. let body = document.getElementsByTagName("body")[0];
  14. let wrapper=document.createElement("div");
  15. wrapper.style.width="50px";
  16. wrapper.style.height="50px";
  17. wrapper.style.lineHeight="50px";
  18. wrapper.style.textAlign="center";
  19. wrapper.style.borderRadius="50%";
  20. wrapper.style.position = "fixed";
  21. wrapper.style.fontWeight = "bold";
  22. wrapper.style.cursor="pointer";
  23. wrapper.style.boxShadow = "0 0 2px 0px #716e6e"
  24. wrapper.style.right="20px";
  25. wrapper.style.bottom="80px";
  26. wrapper.innerText="Top";
  27. body.appendChild(wrapper);
  28.  
  29. // 判断是否该显示按钮
  30. const scrollPosition = ()=>{
  31. let top = document.documentElement.scrollTop;
  32. if(top>0){
  33. wrapper.style.display = "block";
  34. }else{
  35. wrapper.style.display = "none";
  36. }
  37. }
  38.  
  39. scrollPosition();
  40.  
  41. // 鼠标移入
  42. wrapper.onmouseenter = ()=>{
  43. wrapper.style.backgroundColor="#f9f9f9";
  44. }
  45.  
  46. // 鼠标移出
  47. wrapper.onmouseleave = ()=>{
  48. wrapper.style.backgroundColor="#fff";
  49. }
  50.  
  51. let timer = null;
  52. // 监听滚动条滚动事件
  53. body.onscroll = () =>{
  54. if(timer) return;
  55. timer = setTimeout(()=>{
  56. scrollPosition();
  57. clearTimeout(timer);
  58. timer = null;
  59. },500)
  60. }
  61.  
  62. // 点击
  63. wrapper.onclick = null;
  64. wrapper.onclick = ()=>{
  65. let scrollH = document.scrollingElement.scrollTop;
  66. let tiemr;
  67. clearInterval(tiemr);
  68. tiemr = setInterval(() => {
  69. if (scrollH >= 1) {
  70. let speed = 0;
  71. if(scrollH>=100){
  72. speed = scrollH / 8;
  73. }else{
  74. speed= scrollH / 4;
  75. }
  76. scrollH -= speed;
  77. document.scrollingElement.scrollTop = scrollH;
  78. } else {
  79. clearInterval(tiemr)
  80. }
  81. }, 30)
  82. }
  83. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址