封装scroll()

function scroll(){
            if(window.pageYOffset !== undefined){
                return {
                    "top": window.pageYOffset,
                    "left": window.pageXOffset
                };
            }else if(document.compatMode === "CSS1Compat"){
                return {
                    "top": document.documentElement.scrollTop,
                    "left": document.documentElement.scrollLeft
                };
            }else{
                return {
                    "top": document.body.scrollTop,
                    "left": document.body.scrollLeft
                };
            }
        }

scroll().top 代表scrollTop

scroll().left 代表scrollLeft

或者:

var scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;

 

posted @ 2017-10-20 15:44  asimpleday  阅读(234)  评论(0编辑  收藏  举报