Javascript 如何在页面加载时显示div?
我指的是以下链接: 在这里,单击链接后加载div模式窗口。 我试图摆脱那个链接,打开页面加载模式 我的代码如下: 一切都是一样的,除了我添加了脚本和删除了链接Javascript 如何在页面加载时显示div?,javascript,jquery,css,html,Javascript,Jquery,Css,Html,我指的是以下链接: 在这里,单击链接后加载div模式窗口。 我试图摆脱那个链接,打开页面加载模式 我的代码如下: 一切都是一样的,除了我添加了脚本和删除了链接 <!DOCTYPE html> <html> <head> <title>Modal Window</title> <style type="text/css"> .modalDialog { p
<!DOCTYPE html>
<html>
<head>
<title>Modal Window</title>
<style type="text/css">
.modalDialog
{
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
right: 0;
bottom: 0;
left: 0;
background: rgba(0,0,0,0.8);
z-index: 99999;
opacity: 0;
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
pointer-events: none;
}
.modalDialog:target
{
opacity: 1;
pointer-events: auto;
}
.modalDialog > div
{
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -moz-linear-gradient(#fff, #999);
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
}
.close
{
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
-webkit-border-radius: 12px;
-moz-border-radius: 12px;
border-radius: 12px;
-moz-box-shadow: 1px 1px 3px #000;
-webkit-box-shadow: 1px 1px 3px #000;
box-shadow: 1px 1px 3px #000;
}
.close:hover
{
background: #00d9ff;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$("#openModal").show();
});
</script>
</head>
<body>
<div id="openModal" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<h2>
Modal Box</h2>
<p>
This is a sample modal box that can be created using the powers of CSS3.</p>
<p>
You could do a lot of things here like have a pop-up ad that shows when your website
loads, or create a login/register form for users.</p>
</div>
</div>
</body>
</html>
我一定是做错了什么
有什么想法吗?正如@Charaf jra所建议的,
您应通过以下方式触发单击:
$(document).ready(function() { $("#openModal").trigger('click'); });
另一方面,,
大多数情况下,您可以通过以下方式触发它:
$(document).ready(function() { $(".modalDialog").modal() }
要触发单击事件,请使用触发器: 问题在于.show并没有改变$openModal的不透明度 做 正如OP所指出的,解散模型按钮不起作用
你有不透明度:0;例如,modalDialog。在这种情况下,您需要使用fadeTo而不是show
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$(".modalDialog").fadeTo(1,1);
});
</script>
尝试$document.readyfunction{$openModal.trigger'click';};使用setTimeout的坏习惯你应该把它去掉@harsha,除非你真的必须使用它。@Dementric:好的,但是你能告诉我为什么这是个坏习惯吗?我想知道。setTimeout不保证加载了任何对象,也不保证存在任何函数或任何东西。你应该检查一下它是否在那里。编辑,如果你已经验证了它们是否存在,那么就没有理由使用setTimeout了吗?@Dementic:谢谢,这很有意义复制别人的答案是否定的。欢迎这么做。大多数情况下是因为模式没有初始化,而只是显示。我也尝试过增加fadeTo的持续时间。但是没有运气!!它以毫秒为单位,因此将其增加到1000,您应该会注意到差异。@Icarus3,请签出新的fiddle链接
$(window).load(function(event){
$(".modalDialog").show();
})
$(document).ready(function() {
$("#openModal").trigger('click');
});
$(document).ready(function() {
// the first arg is duration of the effect, second is the opacity to end at
$("#openModal").fadeTo(500, 1);
});
<script type="text/javascript" language="javascript">
$(document).ready(function() {
$(".modalDialog").fadeTo(1,1);
});
</script>