Detect Document Height Change

后端 未结 8 1105
独厮守ぢ
独厮守ぢ 2020-12-04 10:07

I\'m trying to detect when my document height changes. Once it does, I need to run a few functions to help organize my page layout.

I\'m not looking fo

8条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-04 11:06

    I'm using @vsync's solution, like this. I'm using it for automatic scroll on a page like twitter.

    const scrollInterval = (timeInterval, retry, cb) => {
        let tmpHeight = 0;
        const myInterval = setInterval(() => {
            console.log('interval');
            if (retry++ > 3) {
                clearInterval(this);
            }
            const change = document.body.clientHeight - tmpHeight;
            tmpHeight = document.body.clientHeight;
            if (change > 0) {
                cb(change, (retry * timeInterval));
                scrollBy(0, 10000);
            }
            retry = 0;
        }, timeInterval);
        return myInterval;
    };
    
    const onBodyChange = (change, timeout) => {
        console.log(`document.body.clientHeight, changed: ${change}, after: ${timeout}`);
    }
    
    const createdInterval = scrollInterval(500, 3, onBodyChange);
    
    // stop the scroller on some event
    setTimeout(() => {
        clearInterval(createdInterval);
    }, 10000);
    

    You can also add a minimum change, and a lot of other things... But this is working for me

提交回复
热议问题