置頂和置底按鈕

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

目前為 2024-08-11 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name 置頂和置底按鈕
  3. // @version 1.2.0.33
  4. // @description 在所有頁面生成兩個按鈕,一個用於順滑回到頂部,一個用於持續滾動到底部,再次點擊取消滾動到底部
  5. // @run-at document-end
  6. // @match *://*/*
  7. // @license MIT
  8. // @namespace https://greasyfork.org/zh-CN/users/1169082
  9. // @icon https://github.com/ChinaGodMan/UserScripts/raw/main/docs/icon/Scripts%20Icons/icons8-up-96.png
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict'
  14. let scrollTimeout
  15. var style = document.createElement('style')
  16. style.innerHTML = `
  17. :root {
  18. --button-size: 29.4px; /* 按钮的大小 */
  19. --button-margin: 1px; /* 按钮之间的间距 */
  20. }
  21.  
  22. .GO_TO_TOP_button, .GO_TO_BOTTOM_button {
  23. width: var(--button-size); /* 按钮大小 */
  24. height: var(--button-size); /* 按钮大小 */
  25. border-radius: 5.6px; /* 縮小30% */
  26. box-shadow: 0 3px 6px rgb(0 0 0 / 16%), 0 1px 2px rgb(0 0 0 / 23%);
  27. position: fixed;
  28. right: 14px; /* 缩小30% */
  29. display: flex;
  30. align-items: center;
  31. justify-content: center;
  32. z-index: 99999999;
  33. background-color: white;
  34. opacity: 0.8;
  35. transition: opacity 0.2s ease-in-out;
  36. cursor: pointer;
  37. }
  38.  
  39.  
  40.  
  41. .GO_TO_TOP_button svg, .GO_TO_BOTTOM_button svg {
  42. left: 16.8px; /* 縮小30% */
  43. height: 16.8px; /* 縮小30% */
  44. margin: 0;
  45. }
  46. .GO_TO_TOP_button {
  47. bottom: 49px; /* 縮小30% */
  48. }
  49. .GO_TO_BOTTOM_button {
  50. bottom: 14px; /* 縮小30% */
  51. }
  52. `
  53. document.head.appendChild(style)
  54.  
  55. const goToTop = () => {
  56. window.scrollTo({
  57. top: 0,
  58. behavior: 'smooth'
  59. })
  60. }
  61.  
  62. let bottomInterval
  63. const startScrollingToBottom = () => {
  64. bottomInterval = setInterval(() => {
  65. window.scrollTo({
  66. top: document.body.scrollHeight,
  67. behavior: 'smooth'
  68. })
  69. }, 1000)
  70.  
  71. let checkTimeout = setTimeout(() => {
  72. if (window.innerHeight + window.scrollY >= document.body.scrollHeight) {
  73. if (buttonBottom.style.backgroundColor === 'green') {
  74. buttonBottom.click()
  75. // alert("已处于最底端");
  76. }
  77. }
  78. }, 3000)
  79.  
  80. window.addEventListener('scroll', () => {
  81. if (window.innerHeight + window.scrollY < document.body.scrollHeight) {
  82. clearTimeout(checkTimeout)
  83. checkTimeout = setTimeout(() => {
  84. if (buttonBottom.style.backgroundColor === 'green') {
  85. buttonBottom.click()
  86. // alert("自动滚动到底部已停止。");
  87. }
  88. }, 3000)
  89. }
  90. })
  91. buttonBottom.style.opacity = '0.8'
  92. buttonBottom.style.display = 'flex'
  93. }
  94. const stopScrollingToBottom = () => {
  95. clearInterval(bottomInterval)
  96. }
  97.  
  98. const buttonTop = document.createElement('div')
  99. buttonTop.className = 'GO_TO_TOP_button'
  100. buttonTop.innerHTML = `
  101. <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg">
  102. <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"/>
  103. <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"/>
  104. </svg>`
  105.  
  106. const buttonBottom = document.createElement('div')
  107. buttonBottom.className = 'GO_TO_BOTTOM_button'
  108. buttonBottom.innerHTML = `
  109. <svg viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg">
  110. <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"/>
  111. <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"/>
  112. </svg>`
  113.  
  114. let isScrollingToBottom = false
  115.  
  116. buttonTop.addEventListener('click', goToTop)
  117.  
  118. buttonBottom.addEventListener('click', () => {
  119. if (isScrollingToBottom) {
  120. stopScrollingToBottom()
  121. buttonBottom.style.transform = 'scale(1)'
  122. buttonBottom.style.backgroundColor = 'white' // 改回原来的背景色
  123. } else {
  124. startScrollingToBottom()
  125. buttonBottom.style.transform = 'scale(1.1)'
  126. buttonBottom.style.backgroundColor = 'green' // 改为绿色
  127. }
  128. isScrollingToBottom = !isScrollingToBottom
  129. })
  130.  
  131. buttonTop.style.display = 'none'
  132. buttonBottom.style.display = 'none'
  133. document.body.appendChild(buttonTop)
  134. document.body.appendChild(buttonBottom)
  135.  
  136. let timer
  137. window.addEventListener('scroll', () => {
  138. if (window.pageYOffset > 50) {
  139. buttonTop.style.opacity = '0.8'
  140. buttonTop.style.display = 'flex'
  141. buttonBottom.style.opacity = '0.8'
  142. buttonBottom.style.display = 'flex'
  143. } else if (window.pageYOffset === 0) {
  144. buttonTop.style.opacity = '0'
  145. buttonBottom.style.opacity = '0'
  146. setTimeout(() => {
  147. buttonTop.style.display = 'none'
  148. buttonBottom.style.display = 'none'
  149. }, 200)
  150. }
  151. clearTimeout(timer)
  152. timer = setTimeout(() => {
  153. buttonTop.style.opacity = '0'
  154. buttonBottom.style.opacity = '0'
  155. setTimeout(() => {
  156. buttonTop.style.display = 'none'
  157. buttonBottom.style.display = 'none'
  158. }, 200)
  159. }, 1800)
  160.  
  161. })
  162. })()