平滑滚动翻页

使用w和s键进行平滑滚动翻页。

目前为 2024-06-22 提交的版本。查看 最新版本

// ==UserScript==
// @name         平滑滚动翻页
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  使用w和s键进行平滑滚动翻页。
// @author       coccvo
// @match        https://www.qidian.com/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    // 定义一个函数来绑定按键事件监听器
    function bindKeyListeners() {
        // 获取视窗高度
        let viewportHeight = window.innerHeight;

        // 定义每次滚动的距离为视窗高度的0.9倍
        let scrollDistance = viewportHeight * 0.9;

        // 监听按键事件
        document.addEventListener('keydown', function(event) {
            if (event.key === 'w') {
                // 按下w键向上滚动
                window.scrollBy({
                    top: -scrollDistance,
                    left: 0,
                    behavior: 'smooth'
                });
            } else if (event.key === 's') {
                // 按下s键向下滚动
                window.scrollBy({
                    top: scrollDistance,
                    left: 0,
                    behavior: 'smooth'
                });
            }
        });
    }

    // 页面加载完毕后执行
    window.addEventListener('DOMContentLoaded', function() {
        bindKeyListeners();
    });

    // 窗口获取焦点时重新绑定按键事件监听器
    window.addEventListener('focus', function() {
        bindKeyListeners();
    });

})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址