lengziyu / noteBook

This is my noteBook.
0 stars 0 forks source link

获取浏览器滚动方向 #8

Open lengziyu opened 7 years ago

lengziyu commented 7 years ago
//获取浏览器scroll
function scroll() {
    if (window.pageYOffset != null){
    // 支持IE9 +
        return{
            left:window.pageXOffset,
            top:window.pageYOffset
        }
    }else if (document.compatMode == 'CSS1Compat'){
        return{
            left:document.documentElement.scrollLeft,
            top:document.documentElement.scrollTop
        }
    }
    return{
        left:document.body.scrollLeft,
        top:document.body.scrollTop
    }
}

判断滚动条是往上下还是左右方向:

window.onscroll = (function () {
    var sLeft = scroll().left;
    var sTop = scroll().top;

    return function () {
        if (sTop != scroll().top) {
            console.log('上下滚动')
            sTop = scroll().top;
        }
        if (sLeft != scroll().left) {
            console.log('左右滚动')
            sLeft = scroll().left;

        }
    }

})();