// ==UserScript== // @name 置頂和置底按鈕 // @version 1.2.0.41 // @description 在所有頁面生成兩個按鈕,一個用於順滑回到頂部,一個用於持續滾動到底部,再次點擊取消滾動到底部 // @run-at document-end // @match *://*/* // @license MIT // @namespace https://greasyfork.org/zh-CN/users/1169082 // @icon https://github.com/ChinaGodMan/UserScripts/raw/main/docs/icon/Scripts%20Icons/icons8-up-96.png // @supportURL https://github.com/ChinaGodMan/UserScripts/issues // @homepageURL https://github.com/ChinaGodMan/UserScripts // @downloadURL none // ==/UserScript== (function () { 'use strict' let scrollTimeout var style = document.createElement('style') style.innerHTML = ` :root { --button-size: 29.4px; /* 按钮的大小 */ --button-margin: 1px; /* 按钮之间的间距 */ } .GO_TO_TOP_button, .GO_TO_BOTTOM_button { width: var(--button-size); /* 按钮大小 */ height: var(--button-size); /* 按钮大小 */ border-radius: 5.6px; /* 縮小30% */ box-shadow: 0 3px 6px rgb(0 0 0 / 16%), 0 1px 2px rgb(0 0 0 / 23%); position: fixed; right: 14px; /* 缩小30% */ display: flex; align-items: center; justify-content: center; z-index: 99999999; background-color: white; opacity: 0.8; transition: opacity 0.2s ease-in-out; cursor: pointer; } .GO_TO_TOP_button svg, .GO_TO_BOTTOM_button svg { left: 16.8px; /* 縮小30% */ height: 16.8px; /* 縮小30% */ margin: 0; } .GO_TO_TOP_button { bottom: 49px; /* 縮小30% */ } .GO_TO_BOTTOM_button { bottom: 14px; /* 縮小30% */ } ` document.head.appendChild(style) const goToTop = () => { window.scrollTo({ top: 0, behavior: 'smooth' }) } let bottomInterval const startScrollingToBottom = () => { bottomInterval = setInterval(() => { window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' }) }, 1000) let checkTimeout = setTimeout(() => { if (window.innerHeight + window.scrollY >= document.body.scrollHeight) { if (buttonBottom.style.backgroundColor === 'green') { buttonBottom.click() // alert("已处于最底端"); } } }, 3000) window.addEventListener('scroll', () => { if (window.innerHeight + window.scrollY < document.body.scrollHeight) { clearTimeout(checkTimeout) checkTimeout = setTimeout(() => { if (buttonBottom.style.backgroundColor === 'green') { buttonBottom.click() // alert("自动滚动到底部已停止。"); } }, 3000) } }) buttonBottom.style.opacity = '0.8' buttonBottom.style.display = 'flex' } const stopScrollingToBottom = () => { clearInterval(bottomInterval) } const buttonTop = document.createElement('div') buttonTop.className = 'GO_TO_TOP_button' buttonTop.innerHTML = ` ` const buttonBottom = document.createElement('div') buttonBottom.className = 'GO_TO_BOTTOM_button' buttonBottom.innerHTML = ` ` let isScrollingToBottom = false buttonTop.addEventListener('click', goToTop) buttonBottom.addEventListener('click', () => { if (isScrollingToBottom) { stopScrollingToBottom() buttonBottom.style.transform = 'scale(1)' buttonBottom.style.backgroundColor = 'white' // 改回原来的背景色 } else { startScrollingToBottom() buttonBottom.style.transform = 'scale(1.1)' buttonBottom.style.backgroundColor = 'green' // 改为绿色 } isScrollingToBottom = !isScrollingToBottom }) buttonTop.style.display = 'none' buttonBottom.style.display = 'none' document.body.appendChild(buttonTop) document.body.appendChild(buttonBottom) let timer window.addEventListener('scroll', () => { if (window.pageYOffset > 50) { buttonTop.style.opacity = '0.8' buttonTop.style.display = 'flex' buttonBottom.style.opacity = '0.8' buttonBottom.style.display = 'flex' } else if (window.pageYOffset === 0) { buttonTop.style.opacity = '0' buttonBottom.style.opacity = '0' setTimeout(() => { buttonTop.style.display = 'none' buttonBottom.style.display = 'none' }, 200) } clearTimeout(timer) timer = setTimeout(() => { buttonTop.style.opacity = '0' buttonBottom.style.opacity = '0' setTimeout(() => { buttonTop.style.display = 'none' buttonBottom.style.display = 'none' }, 200) }, 1800) }) })()