最新消息: USBMI致力于为网友们分享Windows、安卓、IOS等主流手机系统相关的资讯以及评测、同时提供相关教程、应用、软件下载等服务。

js记录滚动条位置,页面刷新滚动条位置不变

业界 admin 10浏览 0评论

直接上代码

	<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>
<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>

 

直接上代码

	<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>
<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>

 

发布评论

评论列表 (0)

  1. 暂无评论