滚轮自动向下滚动到页面底部的 JS 代码

有很多网页是翻页是滑动滚轮的,然后页面又长,手动滚动有点累。于是写了个可以在 F12 控制台 Console 里面执行的 JS 脚本,这样就可以等待把页面全部加载完成之后再来浏览网页,亲测好用,代码如下:

(function() {
    const config = {
        interval: 150,     // 检测间隔(毫秒),建议100~300
        step: 50,          // 每次滚动步长(像素),建议50~100
        scrollThreshold: 500, // 底部触发阈值(像素)
        maxAttempts: 30     // 最大连续检测次数
    };

    let scrollInterval = null;
    let bottomAttemptCount = 0;
    let lastScrollHeight = document.body.scrollHeight;

    function isAtBottom() {
        return (window.innerHeight + window.scrollY >= document.body.scrollHeight - config.scrollThreshold);
    }

    function scrollToBottom() {
        // 动态加载检测
        const currentScrollHeight = document.body.scrollHeight;
        if (currentScrollHeight > lastScrollHeight) {
            bottomAttemptCount = 0; // 高度变化则重置计数
            lastScrollHeight = currentScrollHeight;
        }

        // 滚动策略
        if (isAtBottom()) {
            window.scrollTo(0, currentScrollHeight); // 直接触底加载
            bottomAttemptCount++;
            if (bottomAttemptCount >= config.maxAttempts) {
                stopScroll();
                console.log("✅ 滚动完成:连续检测无新内容");
            }
        } else {
            window.scrollBy(0, config.step); // 按步长滚动
            bottomAttemptCount = 0;
        }
    }

    function startScroll() {
        if (!scrollInterval) {
            scrollInterval = setInterval(scrollToBottom, config.interval);
            console.log("🚀 滚动已启动 | 参数:", config);
        }
    }

    function stopScroll() {
        clearInterval(scrollInterval);
        scrollInterval = null;
        console.log("🛑 滚动已停止");
    }

    startScroll(); // 自动启动
})();
ShellScript
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注