When a user closes a
, hide that
on all site pages

前端 未结 12 939
忘了有多久
忘了有多久 2020-12-15 02:49

I\'ve developed a couple of alert boxes that display on all site pages.

The user is able to close each box separately:

12条回答
  •  太阳男子
    2020-12-15 03:38

    You can go with the code you have plus setting a cookie which expires in 24 hours:

    1. Install the MDN JavaScript Cookie Framework

    2. On your js set a tomorrow variable holding the expiration date for your cookie:

      var today = new Date();
      var tomorrow = today.setHours(today.getHours() + 24);
      
    3. Set your cookies as follows:

      docCookies.setItem('hide_news', 'true', tomorrow);
      
    4. Conditionally hide the boxes based on the cookie's value

      if (docCookies.getItem('hide_news') == 'true'){
        $("#alert-box-news").add_class('hidden');
      };
      
    5. Define the hidden class on your CSS file

      .hidden {
        display none;
      }
      

    You can see it all in action in this codepen.

提交回复
热议问题