简体   繁体   English

在页面刷新时保存菜单状态

[英]Saving menu state on page reload

I have a menu where I want the state open/close state to persist if the page reloads and when the user has clicked to be closed or open. 我有一个菜单,如果页面重新加载以及用户单击要关闭或打开,我希望状态打开/关闭状态能够保持不变。

I'm using a cookie plugin and I'm almost there but I'm having trouble setting the close cookie to remember the close state, the open cookie still persists. 我正在使用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;
});
});

Can anyone please see what I'm doing wrong. 任何人都可以看看我在做什么错。

Thanks. 谢谢。

Update: Sorry, so now I have this, but it's still not staying closed, am I using the date object incorrectly? 更新:对不起,所以现在我有了这个,但是它仍然没有保持关闭状态,我是否错误地使用了date对象?

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;
});
});  

You are going to want to re-set the open cookie with an expiration date older than current time right before you set the close cookie. 您将要在设置关闭cookie之前,以过期时间早于当前时间的时间重新设置打开cookie。 That way the open cookie will immediately expire and your closed cookie is what remains. 这样,打开的cookie将立即过期,而剩下的关闭的cookie仍然存在。

Update - Getting the time 更新-节省时间

You've inquired about getting the current time in JS, here is some sample code: 您已经询问有关使用JS获取当前时间的信息,这是一些示例代码:

var now = new Date();
var time = now.getTime();
time -= 60 * 1000;
now.setTime(time);

This creates a new Date object called now and extracts the time from it with getTime() . 这将创建一个名为now的新Date对象,并使用getTime()从中提取时间。 We then subtract one minute of time (time is in milliseconds, so 60 * 1000), and then set the date back to that time. 然后,我们减去一分钟的时间(时间以毫秒为单位,因此为60 * 1000),然后将日期设置回该时间。 You can now use this Date object to set the expiration time of your cookie! 现在,您可以使用此Date对象设置cookie的过期时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM