简体   繁体   English

不使用会话存储弹出会话存储

[英]Session Storage Pop Up Not Using Session Storage

I am designing a website and have a pop up that appears. 我正在设计一个网站,并出现一个弹出窗口。 The pop up functions how I would like it, appearing when the page is visited and goes away when the 'x' is pressed in the top corner of the pop up. 弹出窗口的功能与我想要的功能相同,在访问页面时显示,在弹出窗口的顶角按“ x”时消失。 But, I want the pop up to go away (for that session) after being 'x'ed out. 但是,我希望弹出窗口在“退出”后消失(针对该会话)。 After some investigation, I have found the session storage is the way to do this. 经过一番调查,我发现会话存储是实现此目的的方法。 I have tried using session storage, but I can't get it to work. 我曾尝试使用会话存储,但无法使其正常工作。 I am totally new to Java Script so please bare with my terrible code. 我对Java Script完全陌生,所以请不要输入我的糟糕代码。

HTML: HTML:

<div id="pop-up"> 
    <i onclick="fcnx1(),setTimeout(fcnx2, 700)" class="fas fa-times fa-2x" style="padding: 4%; float:right;"></i>
    <p>Have your logo here! If you are interested in becoming a sponsor, feel free to email us at <a href="mailto:sponsor@beaverauv.org">sponsor@beaverauv.org</a>.</p>
</div>

CSS: CSS:

#pop-up {
  position: absolute;
  bottom: 7%; 
  right: 0;
  height: 50%;
  width: 30%;
  background: #F0F0F0;
  border-top: solid 5px rgb(25,28,31);
  border-bottom: solid 5px rgb(25,28,31);
  border-left: solid 5px rgb(25,28,31);
  transition:all 385ms linear;
}

#pop-up p {
  text-align: center;
  margin:5%;
  position: absolute;
  top:50%;
  transform: translateY(-50%);
}

.hide {
  opacity: 0;
}

.gone {
  display: none;
}

JavaScript: JavaScript:

<script type="text/javascript">
    if(sessionStorage.getItem('hidepopup') == true){
        document.getElementById("pop-up").classList.toggle("gone")
    } 

    function fcnx1() {
        document.getElementById("pop-up").classList.toggle("hide");
        sessionStorage.setItem('hidePop', true);
    }

    function fcnx2() {
        document.getElementById("pop-up").classList.toggle("gone");
    }
</script>

Data stored in sessionStorage is of type string , not boolean , so do eg like this and quote the value "true" 存储在sessionStorage中的数据的类型为string ,而不是boolean ,因此例如执行以下操作并引用值“ true”

A note, you also spelled the key 'hidepopup' different in the get/set call. 注意,您在get / set调用中还拼写了不同的键'hidepopup'

if(sessionStorage.getItem('hidepopup') == "true"){
    document.getElementById("pop-up").classList.toggle("gone")
} 

function fcnx1() {
    document.getElementById("pop-up").classList.toggle("hide");
    sessionStorage.setItem('hidepopup', "true");
}

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

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