jquery - 如何限制每个 session 显示一次预加载器?

标签 jquery html css

在我的网站上,我在每个页面上都有一个预加载器。

<div id="preloader" >
<div id="status"></div>
<div id="statustext">some text</div>
</div>

它是由这个 jquery 触发的:

//<![CDATA[
var sCookie = Cookies.get('cargado');

if (sCookie != null) 
{

    $('#preloader').hide();
}
else 
{
    $(window).on('load', function () 
    { // makes sure the whole site is loaded 
        $('#status').fadeOut(); // will first fade out the loading animation 
        $('#preloader').delay(350).fadeOut('slow'); // will fade out the white DIV that covers the website. 
        $('body').delay(350).css({ 'overflow': 'visible' });
        Cookies.set('cargado', 'si');

    })
}
    //]]>

我的需要是预加载器应该只在 session 中显示一次。换句话说,如果用户访问我网站上的 10 个页面,他应该只看到一次,即第一页,无论他是从主页还是内部页面进入网站。我想出了上面显示的解决方案,但尽管设置正确设置 cookie 它不会隐藏预加载器 div。

我在这里创建了一个测试页面:http://www.mejoresdatos.cl/testpage.html

最佳答案

使用 localStorage/sessionStorage 代替 cookie

 <style>
#preloader 
  {
    display:none;
  }
</style>

$(window).on('load', function () 
        { // makes sure the whole site is loaded 
            if (typeof(Storage) !== "undefined") {
                // Code for localStorage/sessionStorage.                
                if(localStorage.isFirstLoadComplete==="false"){
                    $('#preloader').show(0);
                    $('#status').fadeOut(); // will first fade out the loading animation 
                    $('#preloader').delay(350).fadeOut('slow'); // will fade out the white DIV that covers the website. 
                    $('body').delay(350).css({ 'overflow': 'visible' });
                    localStorage.setItem("isFirstLoadComplete", "true");
                }
            } else {
                // Sorry! No Web Storage support..
            }
        });

关于jquery - 如何限制每个 session 显示一次预加载器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40825099/

相关文章:

javascript - 不在下拉列表中显示所选项目

jquery - 根据 Ruby on Rails 中的当前数据库记录更改元素内容

javascript - 使用 jQuery .css() 将元素的字体设置为 Google Fonts 字体

html - 如何定位 HTML/CSS 的背景图片?

jquery - 如何使用 jQuery 打开模式( Bootstrap )?

jquery - 滚动效果 JQuery

javascript - 使用 jquery 切换显示子项

jquery - 在 IE 中以 IFRAME 模式加载 PDF

php - 将 href 传递并签名到 php 页面帮助

javascript - 隐藏父 block (显示:none) if child element content text