2012-10-24 33 views
2

可能有人幫助我得到這個工作的滾動條位置?我想我需要爲此使用JS,但是我對它的經驗很少......所以我認爲一些代碼示例可能會有用。我簡單的表像這樣(爲簡化起見我沒有複製表的內容,因爲它有很多的代碼,而不是相關還記得在HTML/CSS表

<div style="overflow:auto; height:600px; border:2px solid black; border-radius:5px; background:white"> 
<table style="width:100%; border-collapse:collapse"> 
<thead> 
</thead> 
<tbody> 
</tbody> 
</table> 
</div> 
+0

這種情況很少。不知道你在說什麼表或滾動條。發佈一些代碼,如果你需要幫助。對不起,這只是非常模糊。鏈接也可以。 – awright18

+0

對不起...問題已更新...我無法提供鏈接,但至少我複製了簡化表的外觀。 –

回答

0

在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工作。

希望這會有所幫助...

+0

我把我的文檔的開頭在節...下載的jQuery插件的cookie,但它仍然doesn't工作:/我缺少的東西? –

+0

當然,我改名爲「.yourTableContainerDIV」我的DIV的ID,「.tableContainer」(我補充說,後來,它不是在我原來的問題) –

+0

嗯......如果你使用[此表(HTTP: //stackoverflow.com/questions/13043837/scrollable-table-with-fixed-header),我得到了'.yourTableContainerDIV'有點不對,應該是'.scrollContent'(在TBODY)。而且,由於某種原因,在調用'$(window).unload'方法之前將該值設置爲0。如果我添加'變種I; window.setInterval(函數(){I = $( 「scrollContent。」)scrollTop的()},100);''之前$(窗口).unload'和變化'$ .cookie( 「scroll」,$(「。yourTableContainerDIV」)。scrollTop());'to'$ .cookie(「scroll」,i);',它工作的很好,但是你有一個函數被調用每十分之一第二。 –