页面加载后显示弹出窗口。

3
我按照在线教程创建了一个jQuery弹出窗口。
我想在页面加载后显示这个弹出窗口,或者在页面加载后它会自动出现,同时我也想知道如何编写代码,使其在页面加载后5秒钟后自动出现。
由于我对jQuery的了解较少,无法满足我的要求。
有什么好的建议吗?

function PopUp(){
    document.getElementById('ac-wrapper').style.display="none"; 
}
#ac-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255,255,255,.6);
    z-index: 1001;
}

#popup {
    width: 555px;
    height: 375px;
    background: #FFFFFF;
    border: 5px solid #000;
    border-radius: 25px;
    -moz-border-radius: 25px;
    -webkit-border-radius: 25px;
    box-shadow: #64686e 0px 0px 3px 3px;
    -moz-box-shadow: #64686e 0px 0px 3px 3px;
    -webkit-box-shadow: #64686e 0px 0px 3px 3px;
    position: relative;
    top: 150px; left: 375px;
}
<div id="ac-wrapper">
    <div id="popup">
        <center>
            <h2>Popup Content Here</h2>
            <input type="submit" name="submit" value="Submit" onClick="PopUp()" />
        </center>
    </div>
</div>

<p>Page Content Here......</p>


2
使用Jquery ui,它有一个设置,可以为您完成所有这些操作。 - Liam
将你的代码放在 $(document).ready() 函数中,并从输入文本框中移除 onclick。请检查我的答案。 - nrsharma
"http://uposonghar.com/popup.html" 是一个钓鱼链接。 - J.S. Orris
7个回答

10

当DOM加载完成后,您可以将代码添加到 $(document).ready() 函数中。

从这里移除 onclick:

<input type="submit" name="submit" value="Submit" onClick="PopUp()" />

试试这个:

$(document).ready(function(){
   setTimeout(function(){
      PopUp();
   },5000); // 5000 to load it after 5 seconds from page load
});

1
setTimeout后面缺少一个括号"(". 应该是 setTimeout( function(){ PopUp(); }, 5000);。我尝试过编辑,但只有一个字符无法进行编辑。 - blackandorangecat
请问如何在页面加载时关闭弹出窗口HTML加载?能帮忙吗? - user94
为什么我的模态弹窗在页面加载时就被加载了? - user94

6
尝试以下方法:

尝试以下方法:

<script type="text/javascript">
function PopUp(hideOrshow) {
    if (hideOrshow == 'hide') document.getElementById('ac-wrapper').style.display = "none";
    else document.getElementById('ac-wrapper').removeAttribute('style');
}
window.onload = function () {
    setTimeout(function () {
        PopUp('show');
    }, 5000);
}
</script>

还有你的HTML

<div id="ac-wrapper" style='display:none'>
    <div id="popup">
        <center>
             <h2>Popup Content Here</h2>    
            <input type="submit" name="submit" value="Submit" onClick="PopUp('hide')" />
        </center>
    </div>
</div>

演示 JsFiddle


为什么我的模态弹出窗口在页面加载时就会加载? - user94
@user94,因为问题要求在页面加载时显示模态框。删除window.onload部分应该可以防止它在页面加载时加载。 - th1rdey3
谢谢。我还有一个问题。我正在使用Slick垂直滑块,并在其中添加了一个“阅读更多”按钮,但它不起作用。 - user94

1
  <script type="text/javascript">
  jQuery(document).ready(function(){
         setTimeout(PopUp(),5000); // invoke Popup function after 5 seconds 
  });
   </script>

为什么我的模态弹出窗口在页面加载时就会加载? - user94

1
使用以下代码在页面加载时显示弹出框:
$(document).ready(function() { 
  var id = '#dialog';
  var maskHeight = $(document).height();
  var maskWidth = $(window).width();
  $('#mask').css({'width':maskWidth,'height':maskHeight}); 
  $('#mask').fadeIn(500); 
  $('#mask').fadeTo("slow",0.9); 
        var winH = $(window).height();
  var winW = $(window).width();
        $(id).css('top',  winH/2-$(id).height()/2);
  $(id).css('left', winW/2-$(id).width()/2);
     $(id).fadeIn(2000);  
     $('.window .close').click(function (e) {
  e.preventDefault();
  $('#mask').hide();
  $('.window').hide();
     });  
     $('#mask').click(function () {
  $(this).hide();
  $('.window').hide();
 });  
 
});

<div class="maintext">
<h2> Main text goes here...</h2>
</div>
<div id="boxes">
<div style="top: 50%; left: 50%; display: none;" id="dialog" class="window"> 
<div id="san">
<a href="#" class="close agree"><img src="close-icon.png" width="25" style="float:right; margin-right: -25px; margin-top: -20px;"></a>
<img src="san-web-corner.png" width="450">
</div>
</div>
<div style="width: 2478px; font-size: 32pt; color:white; height: 1202px; display: none; opacity: 0.4;" id="mask"></div>
</div>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.js"></script> 

0

你也可以使用一个名为jQuery-confirm的插件更轻松地完成这个任务。你只需要在你的页面中添加他们提供的脚本标签和样式表即可。

<link rel="stylesheet" 
href="https://cdnjs.cloudflare.com/ajax/libs/jquery-
confirm/3.3.0/jquery-confirm.min.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-
confirm/3.3.0/jquery-confirm.min.js"></script>

然后调用警告框的示例是:

   <script>
        $.alert({
    title: 'Alert!',
    content: 'Simple alert!',
     });

为什么我的模态弹出窗口在页面加载时就会加载? - user94

0
    <script type="text/javascript">
        $(window).on('load',function(){
            setTimeout(function(){ alert(" //show popup"); }, 5000); 
        });
    </script>

2
尽管此代码可能解决了提问者的问题,最好还是加上一个解释说明你的代码是如何解决提问者的问题的。这样,未来的访客就可以从您的帖子中学习并将其应用到自己的代码中。SO不是编码服务,而是知识资源。此外,高质量、完整的答案更有可能被点赞。这些功能以及所有帖子必须是自包含的要求是SO作为平台的一些优势,这使其区别于论坛。您可以编辑以添加其他信息和/或使用源文档补充您的解释。 - SherylHohman

0
如果您不想使用jQuery,请使用以下代码:
<script>
 // without jquery
document.addEventListener("DOMContentLoaded", function() {
 setTimeout(function() {
  // run your open popup function after 5 sec = 5000
  PopUp();
 }, 5000)
});
</script>

或者使用 jQuery

<script>
  $(document).ready(function(){
   setTimeout(function(){
   // open popup after 5 seconds
   PopUp();
  },5000);  
 });
</script>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接