js获取,设置滚动条位置

let scrollTop = document.body.scrollTop+document.documentElement.scrollTop;
let scrollTop= document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop;
console.log(scrollTop)

document.documentElement.scrollTop和document.documentElement.scrollTop会有一个为0

//设置滚动条位置
window.scrollTo(0,scrollTop);
//获取document信息
function ScollPostion() {
    var t, l, w, h;
    if (document.documentElement && document.documentElement.scrollTop) {
        t = document.documentElement.scrollTop;
        l = document.documentElement.scrollLeft;
        w = document.documentElement.scrollWidth;
        h = document.documentElement.scrollHeight;
    } else if (document.body) {
        t = document.body.scrollTop;
        l = document.body.scrollLeft;
        w = document.body.scrollWidth;
        h = document.body.scrollHeight;
    }
    return {
        top: t,
        left: l,
        width: w,
        height: h
    };
}

你可能感兴趣的:(js)