我有一个不起作用的弹出窗口。当我点击按钮时,屏幕变暗,就像弹出即将出现但没有出现弹出窗口。请帮忙!!!
这是html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<!-- the button to call pop up -->
<a href="#" class='btn open-modal' data-modal="#modal1">Open Modal</a>
<!-- pop up beginning -->
<div class='modal' id='modal1'>
<div class='content'>
<h1 class='title'>This is a modal</h1>
<p>
Here is some text and stuff. cool cool
</p>
<a class='btn close-modal' data-modal="#modal1" href="#">K Bye</a>
</div>
</div>
<-- pop up ending -->
<!-- the jquery script -->
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js"></script>
</body>
</html>
这是css文件
* {
box-sizing: border-box;
}
body {
font-family: "Nunito", sans-serif;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.btn {
padding: 20px 50px;
display: inline-block;
background: #EF233C;
color: white;
text-decoration: none;
transition: 0.35s ease-in-out;
font-weight: 700;
}
.btn:hover {
background: #dc1029;
}
.overlay {
width: 100%;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 40px;
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.75);
opacity: 0;
pointer-events: none;
transition: 0.35s ease-in-out;
max-height: 100vh;
overflow-y: auto;
}
.overlay.open {
opacity: 1;
pointer-events: inherit;
}
.overlay .modal {
background: white;
text-align: center;
padding: 40px 80px;
box-shadow: 0px 1px 10px rgba(255, 255, 255, 0.35);
opacity: 0;
pointer-events: none;
transition: 0.35s ease-in-out;
max-height: 100vh;
overflow-y: auto;
}
.overlay .modal.open {
opacity: 1;
pointer-events: inherit;
}
.overlay .modal.open .content {
transform: translate(0, 0px);
opacity: 1;
}
.overlay .modal .content {
transform: translate(0, -10px);
opacity: 0;
transition: 0.35s ease-in-out;
}
.overlay .modal .title {
margin-top: 0;
}
javascript文件
$(".modal").each( function(){
$(this).wrap('<div class="overlay"></div>')
});
$(".open-modal").on('click', function(e){
e.preventDefault();
e.stopImmediatePropagation;
var $this = $(this),
modal = $($this).data("modal");
$(modal).parents(".overlay").addClass("open");
setTimeout( function(){
$(modal).addClass("open");
}, 350);
$(document).on('click', function(e){
var target = $(e.target);
if ($(target).hasClass("overlay")){
$(target).find(".modal").each( function(){
$(this).removeClass("open");
});
setTimeout( function(){
$(target).removeClass("open");
}, 350);
}
});
});
$(".close-modal").on('click', function(e){
e.preventDefault();
e.stopImmediatePropagation;
var $this = $(this),
modal = $($this).data("modal");
$(modal).removeClass("open");
setTimeout( function(){
$(modal).parents(".overlay").removeClass("open");
}, 350);
});
答案 0 :(得分:1)
您必须将display: block
应用于.overlay .modal.open
。
这是因为即使您的.open
课程附加到.modal
,display: none
来自.modal
的{{1}}需要被display: block
替换为该.modal
{ {1}}可见。
.overlay .modal.open {
opacity: 1;
pointer-events: inherit;
display: block;
width: 60%;
height: 60%;
margin: auto;
}
参考代码:
$(".modal").each(function() {
$(this).wrap('<div class="overlay"></div>')
});
$(".open-modal").on('click', function(e) {
e.preventDefault();
e.stopImmediatePropagation;
var $this = $(this),
modal = $($this).data("modal");
$(modal).parents(".overlay").addClass("open");
setTimeout(function() {
$(modal).addClass("open");
}, 350);
$(document).on('click', function(e) {
var target = $(e.target);
if ($(target).hasClass("overlay")) {
$(target).find(".modal").each(function() {
$(this).removeClass("open");
});
setTimeout(function() {
$(target).removeClass("open");
}, 350);
}
});
});
$(".close-modal").on('click', function(e) {
e.preventDefault();
e.stopImmediatePropagation;
var $this = $(this),
modal = $($this).data("modal");
$(modal).removeClass("open");
setTimeout(function() {
$(modal).parents(".overlay").removeClass("open");
}, 350);
});
* {
box-sizing: border-box;
}
body {
font-family: "Nunito", sans-serif;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
}
.btn {
padding: 20px 50px;
display: inline-block;
background: #EF233C;
color: white;
text-decoration: none;
transition: 0.35s ease-in-out;
font-weight: 700;
}
.btn:hover {
background: #dc1029;
}
.overlay {
width: 100%;
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
padding: 40px;
position: fixed;
top: 0;
left: 0;
background: rgba(0, 0, 0, 0.75);
opacity: 0;
pointer-events: none;
transition: 0.35s ease-in-out;
max-height: 100vh;
overflow-y: auto;
}
.overlay.open {
opacity: 1;
pointer-events: inherit;
}
.overlay .modal {
background: white;
text-align: center;
padding: 40px 80px;
box-shadow: 0px 1px 10px rgba(255, 255, 255, 0.35);
opacity: 0;
pointer-events: none;
transition: 0.35s ease-in-out;
max-height: 100vh;
overflow-y: auto;
}
.overlay .modal.open {
opacity: 1;
pointer-events: inherit;
display: block;
width: 60%;
height: 60%;
margin: auto;
}
.overlay .modal.open .content {
transform: translate(0, 0px);
opacity: 1;
}
.overlay .modal .content {
transform: translate(0, -10px);
opacity: 0;
transition: 0.35s ease-in-out;
}
.overlay .modal .title {
margin-top: 0;
}
<link href='https://fonts.googleapis.com/css?family=Varela+Round' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/main.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<body>
<!-- the button to call pop up -->
<a href="#" class='btn open-modal' data-modal="#modal1">Open Modal</a>
<!-- pop up beginning -->
<div class='modal' id='modal1'>
<div class='content'>
<h1 class='title'>This is a modal</h1>
<p>
Here is some text and stuff. cool cool
</p>
<a class='btn close-modal' data-modal="#modal1" href="#">K Bye</a>
</div>
</div>
<-- pop up ending -->
<!-- the jquery script -->
<script src="https://code.jquery.com/jquery-3.1.1.min.js" integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8=" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
<script type="text/javascript" src="script.js"></script>
</body>