function ScrollHolder()
{
//add event to window
this.addWindowListener = function(sEventType, fnHandler) {
if (window.addEventListener) {
window.addEventListener(sEventType, fnHandler, false);
} else if (window.attachEvent) {
window.attachEvent("on" + sEventType, fnHandler);
} else {
window["on" + sEventType] = fnHandler;
}
}
//remember scrolling information when window is unloading
this.whenUnload = function()
{
var scrollTop = document.documentElement.scrollTop;
document.cookie = "scrollTop="+scrollTop;
}
//set current scroll bar the last page scroll bar position
this.whenLoad = function()
{
var scrollTop = document.cookie.match(new RegExp("(^| )scrollTop=([^;]*)(;|$)"));
if(scrollTop==null)
scrollTop = 0;
window.scrollTo(0,scrollTop[2]);
}
//run this script
this.run = function()
{
this.addWindowListener("unload", this.whenUnload);
this.addWindowListener("load", this.whenLoad);
}
}
var scrollHolder = new ScrollHolder();
scrollHolder.run();
一个使用方便的用于跳转后保持滚动条位置的JS
最新推荐文章于 2022-08-20 17:40:48 发布