2013-08-01 25 views
0

我创建了一个简单的div元素与切换元件jQuery的关闭格窗口+饼干

jQuery('.float_close').click(function() { 
       jQuery('.float_notice').fadeToggle('slow'); 

modal

是多么困难,以保持它通过使用可能关闭通知,关闭按钮饼干?

回答

2

不是很难,但我建议在jQuery之上使用额外的JavaScript插件(http://plugins.jquery.com/cookie/)。

if ($.cookie('noticeVisibility') !== undefined 
     && $.cookie('noticeVisibility') === 'hidden') { 

    // If a cookie that stores the visibility 
    // of the notice was set and it says 'hidden', hide the notice 
    jQuery('.float_notice').hide(); 
} 

jQuery('.float_close').click(function() { 
    jQuery('.float_notice').fadeToggle('slow'); 

    // Saves a cookie for later 
    $.cookie('noticeVisibility', 'hidden'); 
}); 
1

你想要的模式不加载再次曾经(或某些特定的时间框架内),即使用户再次访问该页面?如果这就是你所要做的,那么一个饼干可以被接受,但不是理想的;如果用户禁用了他们的cookies,他们将连续出现模式

如果你想使用cookie,你只需设置它的单击事件的内部,并检查其是否触发模式(或结合到最终触发模式时)之前被设置。设置cookie非常简单(有关教程和某些功能,请参阅http://www.quirksmode.org/js/cookies.html)。以下Cookie将在一小时后过期(直接取自:How to set a cookie to expire in 1 hour in Javascript?)。

var now = new Date(); 
var time = now.getTime(); 
time += 3600 * 1000; 
now.setTime(time); 
document.cookie = 
    'username=' + value + 
    '; expires=' + now.toGMTString() + 
    '; path=/'; 

如果你想在模式进行下一次用户访问该页面(或刷新页面)介绍,你可以存储在JavaScript的信息(或解除绑定触发模式的情况下)。只需设置一个布尔值在JavaScript(注意范围):

var modalDismissed = false; 

,并在点击事件,设置为true:

jQuery('.float_close').click(function() { 
    jQuery('.float_notice').fadeToggle('slow'); 
    modalDismissed = true; 
}); 

然后检查,以确保modalDismissed是假弹出的前modal:

if (!modalDismissed) { 
    // modal 
}