页面重新加载后滚动条位置不变

<script type="text/javascript">
    //重新加载后滚动条回到原来的位置
    function window.onload() {
        var arr;
         //cookie中保存的有值,则将滚动条返回到原来的位置
        if (arr = document.cookie.match(/scrollTop=([^;]+)(;|$)/))
        {
            document.documentElement.scrollTop = parseInt(arr[1]);
            document.body.scrollTop = parseInt(arr[1]);
        }
    }
 
    //页面刷新前保存滚动条位置信息到cookie
 
    function window.onbeforeunload() {
        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;
    }
 
 
 
     </script>
阅读更多

没有更多推荐了,返回首页