置頂和置底按鈕

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

目前为 2024-08-02 提交的版本。查看 最新版本

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