js实现网页刷新后滚动条位置不变

来源:互联网 发布:侠盗飞车作弊软件 编辑:程序博客网 时间:2024/05/17 07:41

做开发时 需要某个页面局部刷新 但是因为不可描述的原因又不能写ajax  所以就想用刷新但是滚动条位置不变来实现伪局部刷新  找了一下 发现确实有实现的方法 十分好用


window.onbeforeunload = function () {    var scrollPos;    if (typeof window.pageYOffset != 'undefined') {        scrollPos = window.pageYOffset;    }    else if (typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat') {        scrollPos = document.documentElement.scrollTop;    }    else if (typeof document.body != 'undefined') {        scrollPos = document.body.scrollTop;    }    document.cookie = "scrollTop=" + scrollPos; //存储滚动条位置到cookies中}window.onload = function () {    if (document.cookie.match(/scrollTop=([^;]+)(;|$)/) != null) {        var arr = document.cookie.match(/scrollTop=([^;]+)(;|$)/); //cookies中不为空,则读取滚动条位置        document.documentElement.scrollTop = parseInt(arr[1]);        document.body.scrollTop = parseInt(arr[1]);    }}




0 0
原创粉丝点击