Double-click any blank space on the page to return to the top JS code

 Double-click the left button on any blank space on the page to automatically return to the top of the page.

(function() {
    'use strict';
  
    // 定义一个变量用来记录上次点击的时间
    var lastClickTime = 0;
  
    // 监听页面的点击事件
    document.addEventListener('click', function(e) {
        // 如果点击的目标元素不是文本或图片,即为空白处
        if (e.target.nodeName !== 'TEXT' && e.target.nodeName !== 'IMG') {
            // 获取当前点击的时间
            var currentTime = new Date().getTime();
            // 如果当前点击和上次点击的时间间隔小于300毫秒,即为双击
            if (currentTime - lastClickTime < 300) {
                // 滚动到页面顶部
                window.scrollTo(0, 0);
            }
            // 更新上次点击的时间为当前时间
            lastClickTime = currentTime;
        }
    });
})();

Guess you like

Origin blog.csdn.net/mo3408/article/details/131771793