頁面的div中有滾動條,js實現刷新頁面後回到記錄時滾動條的位置

  當div中綁定數據,給它一個屬性overflow-y: scroll,添加長度大小,使其可以出現滾動條;每次刷新的時候滾動條老是會出如今最上方,這使我很頭疼,通過查閱網上資料,返現兩種方法可行。以下:javascript

第一種方案html

  將上一個頁面的div的scrolltop距離長度記錄在cookie中,而後經過js調整刷新頁面時的長度記錄,代碼以下:java

js代碼:cookie

 

複製代碼

<script>        var _h = 0;        function SetH(o) {
            _h = o.scrollTop
            SetCookie("a", _h)

        }
        window.onload = function () {
            document.getElementById("x").scrollTop = GetCookie("a");//頁面加載時設置scrolltop高度
        }        function SetCookie(sName, sValue) {
            document.cookie = sName + "=" + escape(sValue) + "; ";
        }        function GetCookie(sName) {            var aCookie = document.cookie.split("; ");            for (var i = 0; i < aCookie.length; i++) {                var aCrumb = aCookie[i].split("=");                if (sName == aCrumb[0])                    return unescape(aCrumb[1]);
            }            return 0;
        }
    </script>

複製代碼

 

 

 

 

html中代碼以下:ide

 

複製代碼

<div id="x" style="height: 200px; overflow: scroll" onscroll="SetH(this)">
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
            <p>1</p>
        </div>

複製代碼

 

第二種方案this

  1.經過div的onscroll事件記錄滾動條的scrollTop值,設置到document.cookie
  2.頁面加載時再讀取document.cookie的值,設置給div的scrollTopspa

js代碼實現:orm

 

複製代碼

<script type="text/javascript">        function KeepScrollBar() {            var scrollPos;            if (typeof window.pageYOffset != 'undefined') {
                scrollPos = window.pageYOffset;
            }            else if (typeof document.body != 'undefined') {
                scrollPos = document.getElementById('divContent').scrollTop;
            }
            document.cookie = "scrollTop=" + scrollPos; 
        }
 
        window.onload = function (){            if (document.cookie.match(/scrollTop=([^;]+)(;|$)/) != null) {                var arr = document.cookie.match(/scrollTop=([^;]+)(;|$)/); 
                document.getElementById('divContent').scrollTop = parseInt(arr[1]);
                
            }
        }
    </script>

複製代碼

 

html:server

複製代碼

<div class="content" data-role="tab-content" data-id="course-a" runat="server" id="divContent" style="height: 365px; overflow-y: scroll" onscroll= "KeepScrollBar()">
              <p>            1</p>          
             <p>            2</p>          
             <p>            3</p>          
             <p>            4</p>          
             <p>            5</p>          
             <p>            6</p>          
             <p>            7</p>          
             <p>            8</p>          
             <p>            9</p>          
             <p>            10</p>          
             <p>            11</p>          
             <p>            12</p>          
             <p>            13</p>          
             <p>            14</p>          
             <p>            15</p>          
             <p>            16</p> </div>

複製代碼

相關文章
相關標籤/搜索