頁面刷新時記住滾動條的位置jquery代碼
更新時間:2014年06月17日 09:04:36 投稿:whsnow
這篇文章主要介紹了點擊按鈕頁面刷新的時候 記住滾動條的位置,需要的朋友可以參考下
@*點擊按鈕頁面刷新的時候 記住滾動條的位置*@
<script type="text/javascript">
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]);
}
}
</script>
復制代碼 代碼如下:
<script type="text/javascript">
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]);
}
}
</script>
您可能感興趣的文章:
- jQuery彈出層后禁用底部滾動條(移動端關閉回到原位置)
- jQuery實現(xiàn)將div中滾動條滾動到指定位置的方法
- jquery 點擊元素后,滾動條滾動至該元素位置的方法
- jQuery實現(xiàn)定位滾動條位置
- jQuery實現(xiàn)根據(jù)滾動條位置加載相應內容功能
- 使用jQuery判斷瀏覽器滾動條位置的方法
- 通過JQuery將DIV的滾動條滾動到指定的位置方便自動定位
- jQuery判斷div隨滾動條滾動到一定位置后停止
- JS和JQUERY獲取頁面大小,滾動條位置,元素位置(示例代碼)
- JQuery獲取元素文檔大小、偏移和位置和滾動條位置的方法集合
- jQuery實現(xiàn)滾動條滾動到子元素位置(方便定位)
相關文章
jQuery EasyUI API 中文文檔 - DataGrid數(shù)據(jù)表格
jQuery EasyUI API 中文文檔 - DataGrid數(shù)據(jù)表格使用說明,需要的朋友可以參考下。2011-11-11
Ext.get() 和 Ext.query()組合使用實現(xiàn)最靈活的取元素方式
想要利用ExtJS的庫函數(shù)對DOM進行各類操作,就要得到Element類型的對象,但是Ext.get()取到的雖然是Element,但是參數(shù)只能是id,如果大家對jQuery的selector方式很喜歡和崇拜,那么就一定要學習Ext.get()和Ext.query()的組合方式。2011-09-09
開發(fā)插件的兩個方法jquery.fn.extend與jquery.extend
jQuery為開發(fā)插件提拱了兩個方法,分別是jquery.fn.extend與jquery.extend,接下來就為大家介紹下兩者的具體使用2013-11-11

