The JS web page smoothly scrolls down step pixels every timeout milliseconds until it reaches the bottom of the page

The JS web page smoothly scrolls down by step pixels every timeout milliseconds until it reaches the bottom of the page. Even if there is a new load during scrolling, page height changes are available.

var scrollHeight = window.pageYOffset;
var step = 1000;
var timeout = 300;

function scrollPageDown() {
    
    
    // 重新计算页面底部的位置
    var bottom = document.body.offsetHeight;

    var currentHeight = window.pageYOffset + document.documentElement.clientHeight;
    var nextStep = Math.min(step, bottom - currentHeight);
    window.scroll({
    
    
        left: window.pageXOffset,
        top: currentHeight + nextStep,
        behavior: 'smooth'
    });
    if (currentHeight+step <= bottom) {
    
    
        setTimeout(scrollPageDown, timeout);
    }
}

scrollPageDown();

おすすめ

転載: blog.csdn.net/weixin_50624398/article/details/128421399