// ==UserScript==
// @name 回到顶部、前往底部
// @version 0.2
// @description 显示 回到顶部、前往底部 按钮
// @author BIGFA
// @match *://*/*
// @icon data:image/svg+xml,
// @noframes
// @license MIT
// @grant none
// @namespace https://greasyfork.org/users/1445135
// @downloadURL https://update.greasyfork.icu/scripts/529706/%E5%9B%9E%E5%88%B0%E9%A1%B6%E9%83%A8%E3%80%81%E5%89%8D%E5%BE%80%E5%BA%95%E9%83%A8.user.js
// @updateURL https://update.greasyfork.icu/scripts/529706/%E5%9B%9E%E5%88%B0%E9%A1%B6%E9%83%A8%E3%80%81%E5%89%8D%E5%BE%80%E5%BA%95%E9%83%A8.meta.js
// ==/UserScript==
(function() {
'use strict';
// 在页面加载完成后延迟执行 main_ 函数
if (document.readyState === "complete" || (document.readyState !== "loading" && !document.documentElement.doScroll)) {
setTimeout(() => main_(), 1000);
} else {
document.addEventListener("DOMContentLoaded", function () { setTimeout(() => main_(), 1000) });
}
// 主函数,创建按钮并使用 window 作为滚动目标
function main_() {
let scrollElement = window; // 统一使用 window 作为滚动目标
createButtonToTop(scrollElement);
createButtonToBottom(scrollElement);
}
// 滚动到顶部的函数
function scrollToTop(element) {
if (element === window) {
window.scrollTo({ top: 0, behavior: 'smooth' });
} else {
element.scrollTo({ top: 0, behavior: 'smooth' });
}
}
// 滚动到底部的函数
function scrollToBottom(element) {
if (element === window) {
window.scrollTo({ top: document.documentElement.scrollHeight, behavior: 'smooth' });
} else {
element.scrollTo({ top: element.scrollHeight, behavior: 'smooth' });
}
}
// 创建“回到顶部”按钮
function createButtonToTop(scrollElement) {
const btn = document.createElement('div');
btn.innerHTML = '';
btn.style.color = 'rgba(0,0,0,0.2)'; // 默认透明度20%
btn.style.position = 'fixed';
btn.style.right = '50px';
btn.style.bottom = '55px';
btn.style.cursor = 'pointer';
btn.style.background = 'transparent';
btn.style.width = '50px';
btn.style.height = '50px';
btn.style.transition = 'color 0.3s ease, transform 0.3s ease'; // 添加过渡效果
btn.style.zIndex = '9999'; // 设置高 z-index 值,确保在最上层
btn.addEventListener('mouseover', () => {
btn.style.color = 'rgba(0,0,0,0.9)'; // 悬停时透明度90%
btn.style.transform = 'scale(1.1)'; // 放大10%
});
btn.addEventListener('mouseout', () => {
btn.style.color = 'rgba(0,0,0,0.2)'; // 恢复透明度20%
btn.style.transform = 'scale(1)'; // 恢复原始大小
});
btn.onclick = () => scrollToTop(scrollElement);
btn.oncontextmenu = (e) => {
e.preventDefault();
btn.style.display = 'none';
};
document.body.appendChild(btn);
}
// 创建“前往底部”按钮
function createButtonToBottom(scrollElement) {
const btn = document.createElement('div');
btn.innerHTML = '';
btn.style.color = 'rgba(0,0,0,0.2)'; // 默认透明度20%
btn.style.position = 'fixed';
btn.style.right = '50px';
btn.style.bottom = '5px';
btn.style.cursor = 'pointer';
btn.style.background = 'transparent';
btn.style.width = '50px';
btn.style.height = '50px';
btn.style.transition = 'color 0.3s ease, transform 0.3s ease'; // 添加过渡效果
btn.style.zIndex = '9999'; // 设置高 z-index 值,确保在最上层
btn.addEventListener('mouseover', () => {
btn.style.color = 'rgba(0,0,0,0.9)'; // 悬停时透明度90%
btn.style.transform = 'scale(1.1)'; // 放大10%
});
btn.addEventListener('mouseout', () => {
btn.style.color = 'rgba(0,0,0,0.2)'; // 恢复透明度20%
btn.style.transform = 'scale(1)'; // 恢复原始大小
});
btn.onclick = () => scrollToBottom(scrollElement);
btn.oncontextmenu = (e) => {
e.preventDefault();
btn.style.display = 'none';
};
document.body.appendChild(btn);
}
})();