jQuery 页面加载后显示弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19707236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Show popup after page load
提问by arifix
I created a jQuery popup by following an online tutorial.
我按照在线教程创建了一个 jQuery 弹出窗口。
I want to show this popup after page load/after page load it appears + how to code it as like it appears after 5 second of page load.
我想在页面加载后/页面加载后显示此弹出窗口 + 如何将其编码为页面加载 5 秒后出现的样子。
Due to my low knowledge on jQuery I am not able to make it work as of my requirements.
由于我对 jQuery 知之甚少,我无法使其按照我的要求工作。
Any idea how to do it?
知道怎么做吗?
Javascript-
Javascript-
<script type="text/javascript">
function PopUp(){
document.getElementById('ac-wrapper').style.display="none";
}
</script>
CSS-
CSS-
#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;
}
HTML-
HTML-
<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>
回答by nrsharma
When the DOM is finished loading you can add your code in the $(document).ready()
function.
当 DOM 完成加载后,您可以在$(document).ready()
函数中添加您的代码。
Remove the onclick from here:
从这里删除 onclick:
<input type="submit" name="submit" value="Submit" onClick="PopUp()" />
Try this:
尝试这个:
$(document).ready(function(){
setTimeout(function(){
PopUp();
},5000); // 5000 to load it after 5 seconds from page load
});
回答by th1rdey3
try something like this
尝试这样的事情
<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>
and your html
和你的 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>
Demo JsFiddle
演示 JsFiddle
回答by Paul Lo
<script type="text/javascript">
jQuery(document).ready(function(){
setTimeout(PopUp(),5000); // invoke Popup function after 5 seconds
});
</script>
回答by M. Lak
Use this below code to display pop-up box on page load:
使用以下代码在页面加载时显示弹出框:
$(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>
I refereed this code from here Demo
我从这里Demo 中引用了这段代码
回答by Brij Patel
You can also do this much easier with a plugin called jQuery-confirm. All you have to do is add the script tag and the style sheet they provide in your page
您还可以使用名为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>
And then an example of calling the alert box is:
然后调用警报框的示例是:
<script>
$.alert({
title: 'Alert!',
content: 'Simple alert!',
});
回答by Abdullah Tahan
<script type="text/javascript">
$(window).on('load',function(){
setTimeout(function(){ alert(" //show popup"); }, 5000);
});
</script>