在jQuery中(這將節省與滾動位置的餅乾):
// When document is ready...
$(document).ready(function() {
// If cookie is set, scroll to the position saved in the cookie.
if ($.cookie("scroll") !== null) {
$(".yourTableContainerDIV").scrollTop($.cookie("scroll"));
}
// On window unload, save cookie
$(window).unload(function() {
$.cookie("scroll", $(".yourTableContainerDIV").scrollTop());
});
});
從this answer採取了一些微小的改動
編輯:
所以不起作用。
第一個問題是,如果您使用的是this table,那麼您需要閱讀的不是容器DIV的scrollTop
,而是您需要查看的tbody。
以及第二個問題是,$(".scrollContent").scrollTop()
值變爲0之前$(window).unload()
被調用。當我修改這樣的代碼:
// When document is ready...
$(document).ready(function() {
// If cookie is set, scroll to the position saved in the cookie.
if ($.cookie("scroll") !== null) {
$(".yourTableContainerDIV").scrollTop($.cookie("scroll"));
}
// Set i to the current scroll position every 100 milliseconds.
var i;
window.setInterval(function(){i=$(".scrollContent").scrollTop()}, 100);
// On window unload, save cookie.
$(window).unload(function() {
$.cookie("scroll", i);
});
});
它很好用!但是,接下來你會調用一個函數,並且每十分之一秒設置一個值,這對性能來說不算太好。另一種方法是使用window.beforeUnload
像這樣:
// When document is ready...
$(document).ready(function() {
// If cookie is set, scroll to the position saved in the cookie.
if ($.cookie("scroll") !== null) {
$(".yourTableContainerDIV").scrollTop($.cookie("scroll"));
}
});
window.onbeforeunload = function(){
$.cookie("scroll", $(".scrollContent").scrollTop());
return;
}
這適用於大多數的瀏覽器大,不涉及間隔,但它不能在Opera工作。
希望這會有所幫助...
這種情況很少。不知道你在說什麼表或滾動條。發佈一些代碼,如果你需要幫助。對不起,這只是非常模糊。鏈接也可以。 – awright18
對不起...問題已更新...我無法提供鏈接,但至少我複製了簡化表的外觀。 –