学无先后,达者为师

网站首页 前端文档 正文

js获取当前滚动条位置

作者:丶扁扁的糖是扁的 更新时间: 2022-01-20 前端文档
//除了IE 8 及 更早 IE 版本 的浏览器都有window.pageXOffset和window.pageYOffset属性直接使用即可
function getScrollOffset() {
    //所有主流浏览器
    if (window.pageXOffset !== undefined) {
        return {
            x: window.pageXOffset,
            y: window.pageYOffset
        }
    } else {
    //IE 8 及 更早 IE 版本
        return {
            x: document.body.scrollLeft + document.documentElement.scrollLeft,
            y: document.body.scrollTop + document.documentElement.scrollTop
        }
    }
}

原文链接:https://blog.csdn.net/sugerinaflat/article/details/121207387

栏目分类
最近更新