置頂和置底按鈕

在所有頁面生成兩個按鈕,一個用於順滑回到頂部,一個用於持續滾動到底部,再次點擊取消滾動到底部

  1. // ==UserScript==
  2. // @name 置頂和置底按鈕
  3. // @version 1.4
  4. // @description 在所有頁面生成兩個按鈕,一個用於順滑回到頂部,一個用於持續滾動到底部,再次點擊取消滾動到底部
  5. // @run-at document-end
  6. // @match *://*/*
  7. // @license MIT
  8. // @namespace https://gf.qytechs.cn/zh-CN/users/954189
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. 'use strict';
  13.  
  14. var style = document.createElement('style');
  15. style.innerHTML = `
  16. .GO_TO_TOP_button, .GO_TO_BOTTOM_button {
  17. width: 29.4px; /* 縮小30% */
  18. height: 29.4px; /* 縮小30% */
  19. border-radius: 5.6px; /* 縮小30% */
  20. box-shadow: 0 3px 6px rgb(0 0 0 / 16%), 0 1px 2px rgb(0 0 0 / 23%);
  21. position: fixed;
  22. right: 14px; /* 縮小30% */
  23. display: flex;
  24. align-items: center;
  25. justify-content: center;
  26. z-index: 99999999;
  27. background-color: white;
  28. opacity: 0.8;
  29. transition: opacity 0.2s ease-in-out;
  30. cursor: pointer;
  31. }
  32. .GO_TO_TOP_button svg, .GO_TO_BOTTOM_button svg {
  33. width: 16.8px; /* 縮小30% */
  34. height: 16.8px; /* 縮小30% */
  35. margin: 0;
  36. }
  37. .GO_TO_TOP_button {
  38. bottom: 49px; /* 縮小30% */
  39. }
  40. .GO_TO_BOTTOM_button {
  41. bottom: 14px; /* 縮小30% */
  42. }
  43. `;
  44. document.head.appendChild(style);
  45.  
  46. const goToTop = () => {
  47. window.scrollTo({
  48. top: 0,
  49. behavior: 'smooth'
  50. });
  51. }
  52.  
  53. let bottomInterval;
  54. const startScrollingToBottom = () => {
  55. bottomInterval = setInterval(() => {
  56. window.scrollTo({
  57. top: document.body.scrollHeight,
  58. behavior: 'smooth'
  59. });
  60. }, 1000);
  61. }
  62.  
  63. const stopScrollingToBottom = () => {
  64. clearInterval(bottomInterval);
  65. }
  66.  
  67. const buttonTop = document.createElement('div');
  68. buttonTop.className = 'GO_TO_TOP_button';
  69. buttonTop.innerHTML = `
  70. <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg">
  71. <path d="M825.568 555.328l-287.392-289.28C531.808 259.648 523.488 256.576 515.2 256.64 514.08 256.544 513.12 256 512 256c-4.672 0-9.024 1.088-13.024 2.88-4.032 1.536-7.872 3.872-11.136 7.136l-259.328 258.88c-12.512 12.48-12.544 32.736-0.032 45.248 6.24 6.272 14.432 9.408 22.656 9.408 8.192 0 16.352-3.136 22.624-9.344L480 364.288V928c0 17.696 14.336 32 32 32s32-14.304 32-32V362.72l236.192 237.728c6.24 6.272 14.496 9.44 22.688 9.44s16.32-3.104 22.56-9.312C838.016 588.128 838.048 567.84 825.568 555.328z"/>
  72. <path d="M864 192H160C142.336 192 128 177.664 128 160s14.336-32 32-32h704c17.696 0 32 14.336 32 32S881.696 192 864 192z"/>
  73. </svg>`;
  74.  
  75. const buttonBottom = document.createElement('div');
  76. buttonBottom.className = 'GO_TO_BOTTOM_button';
  77. buttonBottom.innerHTML = `
  78. <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg">
  79. <path d="M198.4 468.352l287.392 289.28c6.368 6.4 14.688 9.472 22.976 9.408 1.12 0.096 2.08 0.64 3.2 0.64 4.672 0 9.024-1.088 13.024-2.88 4.032-1.536 7.872-3.872 11.136-7.136l259.328-258.88c12.512-12.48 12.544-32.736 0.032-45.248-6.24-6.272-14.432-9.408-22.656-9.408-8.192 0-16.352 3.136-22.624 9.344L544 659.712V96c0-17.696-14.336-32-32-32s-32 14.304-32 32v565.28L243.808 423.552c-6.24-6.272-14.496-9.44-22.688-9.44s-16.32 3.104-22.56 9.312c-12.48 12.512-12.512 32.8-0.032 45.312z"/>
  80. <path d="M160 832h704c17.664 0 32 14.336 32 32s-14.336 32-32 32H160c-17.664 0-32-14.336-32-32s14.336-32 32-32z"/>
  81. </svg>`;
  82.  
  83. let isScrollingToBottom = false;
  84.  
  85. buttonTop.addEventListener('click', goToTop);
  86.  
  87. buttonBottom.addEventListener('click', () => {
  88. if (isScrollingToBottom) {
  89. stopScrollingToBottom();
  90. buttonBottom.style.transform = 'scale(1)';
  91. } else {
  92. startScrollingToBottom();
  93. buttonBottom.style.transform = 'scale(1.1)';
  94. }
  95. isScrollingToBottom = !isScrollingToBottom;
  96. });
  97.  
  98. buttonTop.style.display = 'none';
  99. buttonBottom.style.display = 'none';
  100. document.body.appendChild(buttonTop);
  101. document.body.appendChild(buttonBottom);
  102.  
  103. let timer;
  104. window.addEventListener('scroll', () => {
  105. if (window.pageYOffset > 50) {
  106. buttonTop.style.opacity = '0.8';
  107. buttonTop.style.display = 'flex';
  108. buttonBottom.style.opacity = '0.8';
  109. buttonBottom.style.display = 'flex';
  110. } else if (window.pageYOffset === 0) {
  111. buttonTop.style.opacity = '0';
  112. buttonBottom.style.opacity = '0';
  113. setTimeout(() => {
  114. buttonTop.style.display = 'none';
  115. buttonBottom.style.display = 'none';
  116. }, 200);
  117. }
  118. clearTimeout(timer);
  119. timer = setTimeout(() => {
  120. buttonTop.style.opacity = '0';
  121. buttonBottom.style.opacity = '0';
  122. setTimeout(() => {
  123. buttonTop.style.display = 'none';
  124. buttonBottom.style.display = 'none';
  125. }, 200);
  126. }, 1800);
  127. });
  128. })();

QingJ © 2025

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