当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 页面刷新时记住滚动条的位置jquery代码实现方法

页面刷新时记住滚动条的位置jquery代码实现方法

2019年03月25日  | 移动技术网IT编程  | 我要评论

@*点击按钮页面刷新的时候 记住滚动条的位置*@

. 代码如下:


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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网