JavaScript弹出窗口只显示一次

时间:2014-06-27 16:22:54

标签: javascript jquery html

这是弹出淡入框的脚本,显示用户何时离开网页。

我试图让它只显示一次,所以当用户点击" X标记"退出,我不想再显示它。目前它始终显示,无论你有多少次点击退出。

感谢任何帮助,因为我对javascript的知识非常有限。

的javascript

$(document).ready(function() {

    $(document).mousemove(function(e) {

        $('#exitpopup').css('left', (window.innerWidth/2 - $('#exitpopup').width()/2));
        $('#exitpopup').css('top', (window.innerHeight/2 - $('#exitpopup').height()/2));

        if(e.clientY <= 30)
        {
            // Show the exit popup
            $('#exitpopup_bg').fadeIn();
            $('#exitpopup').fadeIn();   
        }
    });

    $('#xmark').click(function(){
        $('#exitpopup_bg').fadeOut();
        $('#exitpopup').slideUp();
    }); 

});

的CSS

#exitpopup {
    text-align:center;
    font-family:Arial, Helvetica, sans-serif;
}
#exitpopup h1 {
    margin-top:0px;
    padding-top:0px;
    font-size:55px;
}
#exitpopup h2 {
    margin-top:0px;
    padding-top:0px;
    font-size:35px;
    text-transform:uppercase;
    color: #ff5300;
    font-weight:bold;
    font-style:italic;
}
#exitpopup p {
    text-align:left;
}
.button-popup {
    background-color: #ff5300;
    padding:40px;
    color:#fff;
    border:0px;
    font-size:26px;
    font-weight:bold;
}
.input-popup {
    border: 5px solid white;
    -webkit-box-shadow: 
        inset 0 0 8px rgba(0, 0, 0, 0.1), 
              0 0 16px rgba(0, 0, 0, 0.1);
    -moz-box-shadow: 
        inset 0 0 8px rgba(0, 0, 0, 0.1), 
              0 0 16px rgba(0, 0, 0, 0.1);
    box-shadow: 
        inset 0 0 8px rgba(0, 0, 0, 0.1), 
              0 0 16px rgba(0, 0, 0, 0.1);
    padding: 15px;
    background: #FFE2C6;
    margin: 0 0 10px 0;
    font-weight: bold;
    font-size:16px;
}
#xmark {
    position:absolute;
    margin-left:-20px;
    margin-top:-40px;
}

HTML

<div style="display: none; width:100%; height:100%; position:fixed; background:#000000; opacity: .9; filter:alpha(opacity=0.8); z-index:999998; margin-top:-15px;" id="exitpopup_bg"></div>

<div style=" margin-left: -20px; width:1000px; height:550px; display:none; position:fixed; color:#000; padding:40px 20px 20px 20px;   z-    index:999999; background:rgb(20, 20, 20); background: #f7f7f7; " id="exitpopup"> 

<div id="xmark"><img src="../slike/x-mark.png" /></div> 
</div>

3 个答案:

答案 0 :(得分:1)

你可以只保留一个变量作为弹出窗口是否被打开的标志。当页面刷新时,它将被设置为false。

$(document).ready(function() {
    var opened = false;
    $(document).mousemove(function(e) {

        $('#exitpopup').css('left', (window.innerWidth/2 - $('#exitpopup').width()/2));
        $('#exitpopup').css('top', (window.innerHeight/2 - $('#exitpopup').height()/2));

        if(e.clientY <= 30 && !opened)
        {
            opened = true;
            // Show the exit popup
            $('#exitpopup_bg').fadeIn();
            $('#exitpopup').fadeIn();   
        }
    });

    $('#xmark').click(function(){
        $('#exitpopup_bg').fadeOut();
        $('#exitpopup').slideUp();
    }); 

    });

答案 1 :(得分:0)

你可以添加一个计数器变量。像这样:

    $(document).ready(function() {
    var counter=0;
            ....
     if(counter ==0) {
        // Show the exit popup
        $('#exitpopup_bg').fadeIn();
        $('#exitpopup').fadeIn();   
        counter++;
      }
 } 
  });

答案 2 :(得分:0)

.remove()

的回调中使用.slideup()
$('#exitpopup').slideUp('fast', function(){
    $(this).remove();
});