0
我有一个菜单,我希望状态打开/关闭状态在页面重新加载以及用户点击关闭或打开时持续存在。在页面重新加载时保存菜单状态
我使用一个Cookie插件,我几乎没有,但我有麻烦设置接近cookie来记住关闭状态,开放的cookie仍然存在。
$(document).ready(function() {
// Open/Close Panel According to Cookie //
if ($.cookie('filtermenu') === 'open'){
$('.filter').show(); // Show on Page Load/Refresh without Animation
}
else {
}
if ($.cookie('filtermenu') === 'close' || $.cookie('filtermenu') === null){
$('.filter').hide(); // Show on Page Load/Refresh without Animation
}
else {
}
// Toggle Panel and Set Cookie //
$('#filter-menu').click(function(){
$('.filter').slideToggle('fast', function(){
if ($('#filter-menu').is(':hidden')) {
$.cookie('filtermenu', 'close', { expires: 30 });
} else {
$.cookie('filtermenu', 'open');
}
});
return false;
});
});
任何人都可以请看看我做错了什么。
谢谢。
更新:对不起,现在我有这个,但它仍然没有关闭,我使用的日期对象不正确吗?
Ok, so now I have this, but the menu still doesn't stay closed.
$(document).ready(function() {
// Open/Close Panel According to Cookie //
if ($.cookie('filtermenu') === 'open'){
$('.filter').show(); // Show on Page Load/Refresh without Animation
}
else {
}
if ($.cookie('filtermenu') === 'close' || $.cookie('filtermenu') === null){
$('.filter').hide(); // Show on Page Load/Refresh without Animation
}
else {
}
// Toggle Panel and Set Cookie //
$('#filter-menu').click(function(){
$('.filter').slideToggle('fast', function(){
var now = new Date();
var time = now.getTime();
time -= 60 * 1000;
now.setTime(time);
$.cookie('filtermenu', 'open', {expires: now});
if ($('#filter-menu').is(':hidden')) {
$.cookie('filtermenu', 'close', { expires: 30 });
} else {
$.cookie('filtermenu', 'open');
}
});
return false;
});
});
谢谢,有道理。你会碰巧知道如何抓住当前时间吗? – leanda
我已经用一个例子更新了我的答案,以获取当前时间并将其设置回足够远以使cookie过期。希望这可以帮助! – binaryatrocity
好吧,现在我有这个,但菜单仍然没有关闭。 – leanda