我有一个图标来切换弹出式div,但我希望div保持隐藏状态,如果用户在页面上关闭它,那么即使用户导航到其他页面,该隐藏状态也应该适用。如何使用本地/会话存储实现此目的?
<section class="floating-cta">
<div class="qq-float-cta">
<a href="#" class="cta-callnow">
<div class="cta-top"><h3>Call now on 012 345 6789</h3></div>
<p>For an accurate, immediate response </p>
</a>
<a href="#" class="cta-getquote">
<div class="cta-bottom"><h3>Get your quote</h3></div>
<p>Guaranteed response in 10 minutes within business hours.</p>
</a>
</div>
</section>
<i class="fa fa-times-circle-o"></i>
<script>
$('.fa-times-circle-o').click(function(){
$('.floating-cta').fadeToggle("slow");
$("i",this).toggleClass("fa fa-times-circle-o fa fa-commenting")
});
</script>
我希望当用户关闭该部分时,当他/她转到其他页面时,它应该保持关闭状态,如果他/她想通过再次单击图标来显示它,请再次打开它。
您应该改用cookies
。
像这样:
<script>
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
$('.fa-times-circle-o').click(function(){
$('.floating-cta').fadeToggle("slow");
$("i",this).toggleClass("fa fa-times-circle-o fa fa-commenting");
setCookie('hide_popup', '1');
});
if(getCookie('hide_popup') == '1'){
$('.floating-cta').hide();
}
</script>