在文档就绪时触发弹出窗口或div id

时间:2015-12-09 07:54:57

标签: javascript jquery html css

我需要帮助才能在页面加载时触发弹出窗口。

当我点击“你好”链接时,弹出窗口将会打开。

预期结果:

弹出窗口应该打开而不点击文档准备

上的超链接或弹出窗口

HTML

<a class="button2" href="#popup1">hello</a>

<div id="popup1" class="overlay">
<div class="popup">
    <div class="contentSpec">
        <h2>Would you like to visit</h2>
        <h1>someURL</h1>
    </div>
    <a class="close" href="#"></a>
    <div class="content">

        <div class="box">
        <a class="button" href="#popup1">YES</a>
        <a class="button1" href="#popup1">NO</a>
        </div>
    </div>
</div>
</div>

CSS

body {
  font-family: Arial, sans-serif;
  background-size: cover;
}


.box {
  margin: 6% auto 3% auto;
  padding: 35px;
  background-clip: padding-box;
  text-align: center;
}

.button, .button1 {
    padding: 1.5% 4% 1.5% 4%;
    margin: 2%!important;
    text-decoration:none;
    -webkit-box-sizing: content-box;
    -moz-box-sizing: content-box;
    box-sizing: content-box;
    cursor: pointer;
    margin: 0 auto; 
    -webkit-border-radius: 40px;
    border-radius: 40px;
    font: normal 1em;
    text-align: center;
    -o-text-overflow: clip;
    text-overflow: clip;
    letter-spacing: 1px;
    background: rgba(0,0,0,0);
    -webkit-transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
    -moz-transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
    -o-transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
    transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
}
.button{
    border: 2px solid rgb(30,205,151);
    color: rgb(30, 205, 151);
}
.button1{
    border: 2px solid orange;
    color: orange;
}
.button:hover {
    background: rgb(30,205,151);
    color:#fff;
}
.button1:hover {
    background: orange;
    color:#fff;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 5% 2%;
  background: #fff;
  border-radius: 40px;
  border:10px solid #ccc;
  width: 50%;
  border:3px solid rgba(55, 174, 204, 0.9);
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #666;
  font-size: 40px;
  text-align:center;
  font-family: Tahoma, Arial, sans-serif;
  padding-bottom:10px;
  margin:0;
}
.popup h1 {
  margin-top: 0;
  color: #333;
  font-size:40px;
  text-align:center;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 0px;
  right: 2%;
  transition: all 200ms;
  font-size: 80px;
  font-weight: bold;
  text-decoration: none;
  border-radius:50px;
  color: #333;
}
.popup .close:before {
  content: "×";
}
.popup .close:hover {
  color: orange;
}
.popup .content {
  max-height: 30%;
  text-align:center;
  overflow: auto;
}

.product{
color:#777;
}
.contentSpec{
margin:0 auto;
text-align:center;
}

JS

$(document).ready(function() {
    $(".button2").trigger('click');
});

FIDDLE

提前感谢任何帮助。

3 个答案:

答案 0 :(得分:2)

只需在document.ready()上使用此代码:

window.location="#popup1";

Here is your updated JSFiddle

Here is CodePen demo因为JSFiddle搞砸了而且没有正确更新

完整代码:

&#13;
&#13;
$(document).ready(function() {
    //$(".button2").trigger('click');
  
  window.location="#popup1";
});

$(".button").click(function(){
  window.location="http://www.google.com/";
});
$(".button1").click(function(){
  window.location="http://www.facebook.com/";
});
&#13;
body {
  font-family: Arial, sans-serif;
  background-size: cover;
}


.box {
  margin: 6% auto 3% auto;
  padding: 35px;
  background-clip: padding-box;
  text-align: center;
}

.button, .button1 {
	padding: 1.5% 4% 1.5% 4%;
	margin: 2%!important;
	text-decoration:none;
	-webkit-box-sizing: content-box;
	-moz-box-sizing: content-box;
	box-sizing: content-box;
	cursor: pointer;
	margin: 0 auto; 
	-webkit-border-radius: 40px;
	border-radius: 40px;
	font: normal 1em;
	text-align: center;
	-o-text-overflow: clip;
	text-overflow: clip;
	letter-spacing: 1px;
	background: rgba(0,0,0,0);
	-webkit-transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
	-moz-transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
	-o-transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
	transition: background-color 0.3s cubic-bezier(0, 0, 0, 0), color 0.3s cubic-bezier(0, 0, 0, 0), width 0.3s cubic-bezier(0, 0, 0, 0), border-width 0.3s cubic-bezier(0, 0, 0, 0), border-color 0.3s cubic-bezier(0, 0, 0, 0);
}
.button{
	border: 2px solid rgb(30,205,151);
	color: rgb(30, 205, 151);
}
.button1{
	border: 2px solid orange;
	color: orange;
}
.button:hover {
	background: rgb(30,205,151);
	color:#fff;
}
.button1:hover {
	background: orange;
	color:#fff;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.7);
  transition: opacity 500ms;
  visibility: hidden;
  opacity: 0;
}
.overlay:target {
  visibility: visible;
  opacity: 1;
}

.popup {
  margin: 70px auto;
  padding: 5% 2%;
  background: #fff;
  border-radius: 40px;
  border:10px solid #ccc;
  width: 50%;
  border:3px solid rgba(55, 174, 204, 0.9);
  position: relative;
  transition: all 5s ease-in-out;
}

.popup h2 {
  margin-top: 0;
  color: #666;
  font-size: 40px;
  text-align:center;
  font-family: Tahoma, Arial, sans-serif;
  padding-bottom:10px;
  margin:0;
}
.popup h1 {
  margin-top: 0;
  color: #333;
  font-size:40px;
  text-align:center;
  font-family: Tahoma, Arial, sans-serif;
}
.popup .close {
  position: absolute;
  top: 0px;
  right: 2%;
  transition: all 200ms;
  font-size: 80px;
  font-weight: bold;
  text-decoration: none;
  border-radius:50px;
  color: #333;
}
.popup .close:before {
  content: "×";
}
.popup .close:hover {
  color: orange;
}
.popup .content {
  max-height: 30%;
  text-align:center;
  overflow: auto;
}

.product{
color:#777;
}
.contentSpec{
margin:0 auto;
text-align:center;
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<a class="button2" href="#popup1">hello</a>


<div id="popup1" class="overlay">
	<div class="popup">
		<div class="contentSpec">
			<h2>Would you like to visit</h2>
			<h1>someURL</h1>
		</div>
		<a class="close" href="#"></a>
		<div class="content">
			
			<div class="box">
			<a class="button" href="#popup1">YES</a>
			<a class="button1" href="#popup1">NO</a>
			</div>
		</div>
	</div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:2)

我相信这就是你要找的东西?

$(document).ready(function() {
    window.location.hash = "#popup1";
});

这是更新的Fiddle

答案 2 :(得分:1)

您需要使用:

$(document).ready(function() {
  $(".button2")[0].click();
});

因为您需要触发本机DOM API单击方法。 [0]返回DOM节点,这相当于get(0)。否则,如果在jQuery对象上触发click()事件,则会特别丢弃本机方法。

看一下 SO Question