jQueryの場合(これにより、スクロール位置でCookieが保存されます):
// 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() );
});
});
この回答から、いくつかの小さな変更を加えました。
編集:
したがって、それは完全には機能しません。
最初の問題は、このテーブルを使用している場合、読み取る必要があるのはコンテナーDIVではなく、scrollTop
確認する必要があるのはtbodyであるということです。
そして2番目の問題は、が呼び出される$(".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);
});
});
それは素晴らしい働きをします!ただし、関数が呼び出され、値が10分の1秒ごとに設定されているため、パフォーマンスにはあまり適していません。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では機能しません。
お役に立てれば...