这是一段可以实现双击网页任意空白处自动返回顶部的油猴脚本:

// ==UserScript==
// @name         Double click to scroll to top
// @namespace    http://tampermonkey/DoubleClickToScrollToTop
// @version      1
// @description  Double click on any blank space of the page to scroll to top automatically
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
  'use strict';

  document.addEventListener('dblclick', function(event) {
    if (event.target === document.documentElement) {
      window.scrollTo({
        top: 0,
        behavior: 'smooth'
      });
    }
  });
})();

这个脚本会在所有网页上生效。当用户双击页面上的任意空白处时,它会检查事件目标是否是 document.documentElement 元素,这个元素表示整个文档。如果是,它会使用 window.scrollTo 方法将页面滚动到顶部,并使用 behavior: 'smooth' 让滚动动画化。

用油猴脚本实现双击网页任意空白处自动返回顶部

原文地址: https://www.cveoy.top/t/topic/J55 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录