滚动条滚动方向

// 页面滚动监听事件
window.onscroll = function(e){
    scrollFunc();
    if(scrollDirection == 'down'){
        console.log('down')
    }
    else if(scrollDirection == 'up'){
        console.log('up')
    }else if(scrollDirection == 'left'){
        console.log('left')
    }
    else if(scrollDirection == 'up'){
        console.log('up')
    }
}
let scrollAction = {x: 0, y: 0}, scrollDirection = '';
// 判断页面滚动方向
function scrollFunc() {
    if (scrollAction.x == 0 || scrollAction.y == 0) {
    scrollAction.x = window.pageXOffset;
    scrollAction.y = window.pageYOffset;
    }
    let diffX = scrollAction.x - window.pageXOffset;
    let diffY = scrollAction.y - window.pageYOffset;
    if (diffX < 0) {
        // Scroll right
        scrollDirection = 'right';
    } else if (diffX > 0) {
        // Scroll left
        scrollDirection = 'left';
    } else if (diffY < 0) {
        // Scroll down
        scrollDirection = 'down';
    } else if (diffY > 0) {
        // Scroll up
        scrollDirection = 'up';
    } else {
        // First scroll event
    }
    scrollAction.x = window.pageXOffset;
    scrollAction.y = window.pageYOffset;
}
上一篇:CentOS7 的重启network时Failed to start LSB: Bring up/down


下一篇:对Android开发的现状和未来发展的思考,文末领取面试资料