// ==UserScript==
// @name 页面一键开启自动滚动
// @namespace http://tampermonkey.net/
// @version 0.2
// @description 一键开启页面自动滚动功能,简单实用
// @author casee
// @match *://*/*
// @grant none
// @license MIT License
// @downloadURL none
// ==/UserScript==
(function() {
'use strict';
var btn = null
var isOpenAuto = false
var timer = null
if(timer) {
clearInterval(timer)
}
function init() {
var styleObj = `
html, body {
scroll-behavior: smooth;
}
`
var style = document.createElement('style');
style.type = 'text/css';
style.innerHTML = styleObj;
document.getElementsByTagName("head")[0].appendChild(style);
}
function isToBottom() {
let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
let clientHeight = document.documentElement.clientHeight || document.body.clientHeight;
let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
if (clientHeight + scrollTop >= scrollHeight) {
return true
}
return false
}
function openAutoScroll(){
timer = setInterval(function(){
if(!isToBottom()) {
document.documentElement.scrollTop = document.documentElement.scrollTop += 150;
}
}, 500)
}
function createBtn() {
var el = document.createElement("div");
el.innerHTML = "开启
滚动";
el.style.cssText = `
padding: 4px;
background: linear-gradient(45deg,#fea158,#d500f9,#4e72e3);
color: #fff;
border-radius: 4px;
position: fixed;
right: 10px;
top: 50%;
transform: translateY(-50%);
cursor: pointer;
font-size: 12px;
line-height:14px;
z-index: 99999;
transform: scale(0.9);
`
return el
}
init()
btn = createBtn()
btn.addEventListener("click", function(){
isOpenAuto = !isOpenAuto
btn.innerHTML = isOpenAuto?"关闭
滚动":"开启
滚动"
if(isOpenAuto) {
openAutoScroll()
}else {
clearInterval(timer)
timer = null
}
})
document.body.appendChild(btn)
})();