简体   繁体   English

运行一次后使用html会话存储隐藏div

[英]Use html session storage to hide a div after it's run once

I have this div with an id of se-pre-con in my website which I only want to display once per website visit.我的网站中有一个带有 se-pre-con id 的 div,我只想在每次访问网站时显示一次。 I want to use session storage to display none the html div of se-pre-con after it's run once.我想使用会话存储在运行一次后不显示 se-pre-con 的 html div。 But could do with some advice about how to approach this.但是可以提供一些有关如何处理此问题的建议。

 <div class="se-pre-con"></div> // the relevant html

.no-js #loader { display: none;  } // the relevant css
.js #loader { display: block; position: absolute; left: 100px; top: 0; }
.se-pre-con {
position: fixed;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
z-index: 9999;
background: url(Preloader_10.gif) center no-repeat #fff;
}

 <script> // the relevant jquery

// Wait for window load
$(window).load(function() {
    // Animate loader off screen
    $(".se-pre-con").fadeOut("slow");;
});


</script>

It's still not clear where you want to call that code from, but if I were you, I would reverse the code a bit.仍然不清楚您想从哪里调用该代码,但如果我是您,我会稍微反转代码。 That is, always execute your stuff on the load event and decide what to do while in there.也就是说,总是在load事件上执行你的东西,并决定在那里做什么。 At that point it becomes trivial to put the loading of the object you want to fade out inside a function and call that piece of code from somewhere else (and you don't specify where you want to call it from, so I'll have to guess you want to call it after you set the value inside your sessionStorage .那时,将要淡出的对象的加载放在函数中并从其他地方调用该代码段变得微不足道(并且您没有指定要从哪里调用它,所以我将有猜测您想在sessionStorage设置值后调用它。

function fadeObject() {
    $(".se-pre-con").fadeOut("slow");
}

$(window).load(function() {
    if (sessionStorage.getItem('htmldivwithid') !== 'false') {
        fadeObject();
    } else {
        document.getElementById('htmldivwithid').style.display="none";
        sessionStorage.setItem('htmldivwithid','true');
        // call fadeObject()?
    }
});

At this point you can call fadeObject() when you need it, from inside the load event (I put a comment where I expect you might want to call it).此时,您可以在需要时从load事件内部调用fadeObject() (我在您希望调用它的地方放了一条注释)。

fadeObject is not strictly necessary, but in case you need to change the object that fades you now have just one place to change instead of two, once you decide where you want to call it. fadeObject不是绝对必要的,但如果您需要更改淡出的对象,您现在只需更改一个地方,而不是两个,一旦您决定要在哪里调用它。

I answered my own question by using this script in the body section of the html page, it ensures the loader only ever runs once per browser session, exactly as I was trying to achieve.我通过在 html 页面的正文部分使用这个脚本来回答我自己的问题,它确保加载器在每个浏览器会话中只运行一次,正如我试图实现的那样。

<script>
if (sessionStorage.getItem('se-pre-con') !== 'true'){
// if the storage item 'se-pre-con', does not exist, run the code in curly 
braces
document.getElementById("se-pre-con").style.display = 'block';}

else {
document.getElementById('se-pre-con').style.display="none";
// else (when above if statement is not met) hide the loader
}


sessionStorage.setItem('se-pre-con','true');

</script>

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

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