页面刷新时记住滚动条的位置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 EasyUI API 中文文档 - DataGrid数据表格
jQuery EasyUI API 中文文档 - DataGrid数据表格使用说明,需要的朋友可以参考下。2011-11-11
Ext.get() 和 Ext.query()组合使用实现最灵活的取元素方式
想要利用ExtJS的库函数对DOM进行各类操作,就要得到Element类型的对象,但是Ext.get()取到的虽然是Element,但是参数只能是id,如果大家对jQuery的selector方式很喜欢和崇拜,那么就一定要学习Ext.get()和Ext.query()的组合方式。2011-09-09
开发插件的两个方法jquery.fn.extend与jquery.extend
jQuery为开发插件提拱了两个方法,分别是jquery.fn.extend与jquery.extend,接下来就为大家介绍下两者的具体使用2013-11-11


最新评论