MarkDown
// ==UserScript==
// @name 🔃Scroll Buttons
// @namespace http://your.namespace.com
// @version 1.0
// @description Adds buttons to scroll to the top and bottom of the page
// @author Your Name
// @match http://*/*
// @match https://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
if (window.self !== window.top) {
return; // 如果当前不是顶级文档,则退出脚本
}
// Create styles for the buttons
let styles = `
.scroll-btn {
position: fixed;
bottom: 20px;
right: 50px;
z-index: 999;
cursor: pointer;
background-color: #007bff;
color: #fff;
//padding: 10px;
padding-top: 5px; /* 设置上边距为 15px */
padding-right: 10px; /* 设置右边距为 10px */
padding-bottom: 5px; /* 设置下边距为 20px */
padding-left: 10px; /* 设置左边距为 5px */
border-radius: 5px;
text-align: center;
}
.scroll-btn:hover {
background-color: #0056b3;
}
`;
// Create style element and append it to the head
let styleElement = document.createElement('style');
styleElement.textContent = styles;
document.head.appendChild(styleElement);
// Create toTopBtn
let toTopBtn = document.createElement('div');
//toTopBtn.textContent = 'Top';
toTopBtn.textContent = '⬆';
toTopBtn.className = 'scroll-btn';
toTopBtn.style.bottom = '60px';
toTopBtn.addEventListener('click', function() {
window.scrollTo({ top: 0, behavior: 'smooth' });
//window.scrollTo({ top: 0 });
});
document.body.appendChild(toTopBtn);
//document.getElementsByTagName("body")[0].appendChild(toTopBtn);
// Create toBottomBtn
let toBottomBtn = document.createElement('div');
//toBottomBtn.textContent = 'Bottom';
toBottomBtn.textContent = '⬇';
toBottomBtn.className = 'scroll-btn';
//toBottomBtn.style.bottom = '200px';
toBottomBtn.addEventListener('click', function() {
//window.scrollTo({ top: document.body.scrollHeight, behavior: 'smooth' });
window.scrollTo({ top: document.body.scrollHeight, behavior: 'instant' });
});
document.body.appendChild(toBottomBtn);
//document.getElementsByTagName("body")[0].appendChild(toBottomBtn);
})();