我正在设计一个网站,并有一个弹出的出现。弹出功能是我想要的,当页面被访问时出现,当'x‘被按下弹出的上角时就消失了。但是,我希望弹出(那个会话)在“x”结束后消失。经过一些调查之后,我发现会话存储就是这样做的。我尝试过使用会话存储,但我无法让它工作。我对Java脚本完全陌生,所以请用我糟糕的代码赤裸裸地使用它。
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:
#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:
<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>发布于 2018-07-06 21:44:50
存储在sessionStorage中的数据是string类型的,而不是boolean类型的,因此可以这样做,并引用值"true“
注意,您还在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");
}https://stackoverflow.com/questions/51217948
复制相似问题