我的模态 window 代码需要淡出动画
In need of a fade out animation for my modal window code
我刚开始编写脚本,我不知道如何在关闭模式时添加淡出动画(与淡入动画相同)window。
Here is a link to the project: https://jsfiddle.net/myosis/kb0o27vy/
我喜欢它的一切,它只需要一个结束动画。
有人可以帮我吗!?
:)
<!DOCTYPE html>
<html>
<head>
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(255, 255, 255); /* Fallback color */
background-color: rgba(255, 255, 255.4); /* Black w/ opacity */
-webkit-animation-name: fadeIn; /* Fade in the background */
-webkit-animation-duration: 5.4s;
animation-name: fadeIn;
animation-duration: 5.4s;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 0px;
width: 822px;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.02),0 6px 20px 0 rgba(0,0,0,0.02);
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
-webkit-animation-name: fadeOut; /* Fade in the background */
-webkit-animation-duration: 5.4s;
animation-name: fadeOut;
animation-duration: 5.4s
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
color: #5B5B5B;
font-size: 30px;
font-family: source-sans-pro, sans-serif;
font-weight: 300;
position: relative;
}
.modal-body {
width: 538px;
color: #7A7A7A;
font-size: 14px;
font-family: source-sans-pro, sans-serif;
font-weight: 400;
position: relative;
}
.modal-footer {
padding: 2px 16px;
background-color: #ffffff;
color: blac;
}
/* Add Animation */
@-webkit-keyframes fadeIn {
from {opacity: 0}
to {opacity: 1}
}
@-webkit-keyframes fadeOut {
from {opacity: 1}
to {opacity: 0}
}
</style>
</head>
<body>
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="clearfix grpelem" id="u1562-4"><!-- content -->
<p>test</p>
</div>
</div>
<div class="clearfix colelem" id="u1567-4"><!-- content -->
<p>test</p>
</div>
<div class="colelem" id="u1568"><!-- simple frame --></div>
<div class="clearfix colelem" id="u1566-4"><!-- content -->
<p>test</p>
</div>
<div class="clip_frame colelem" id="u1556"><!-- image -->
<img class="block" id="u1556_img" src="" alt="" width="823" height="1024"/>
</div>
<div class="modal-footer">
<h3>-</h3>
</div>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
您可以使用 jQuery 来做到这一点
$('#myBtn').click(function(){
$('#myModal').fadeIn(5000);
});
$('.close').click(function(){
$('#myModal').fadeOut(5000);
});
如果您不想使用 jQuery,这里有一个快速解决方案。我添加了一个 setTimeout
和 CSS 的持续时间,在此超时后,模态设置为 display: none;
。在 setTimeout
之前,我添加了一个动画 class 来淡出模式。
// Get the modal
var modal = document.getElementById('myModal');
// Get the overlay
var overlay = document.getElementById('modalOverlay');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.className = modal.className.replace(/ modalClose/g, '');
overlay.className = overlay.className.replace(/ modalClose/g, '');
modal.style.display = "block";
overlay.style.display = "block";
};
overlay.onclick = function() {
modal.className = modal.className + ' modalClose';
overlay.className = overlay.className + ' modalClose';
setTimeout(function() {
modal.style.display = "none";
overlay.style.display = "none";
}, 300);
};
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.className = modal.className + ' modalClose';
overlay.className = overlay.className + ' modalClose';
setTimeout(function() {
modal.style.display = "none";
overlay.style.display = "none";
}, 300);
};
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
body, html {
width: 100%;
}
.u1568 {
z-index: 23;
width: 200px;
background-color: transparent;
color: #5B5B5B;
line-height: 36px;
font-size: 30px;
font-family: source-sans-pro, sans-serif;
font-weight: 300;
position: relative;
margin-right: -10000px;
left: 25px;
}
.u1567-4 {
z-index: 31;
width: 200px;
background-color: transparent;
color: #9E9E9E;
font-size: 14px;
font-family: source-sans-pro, sans-serif;
font-weight: 400;
font-style: italic;
margin-left: 49px;
margin-top: 9px;
position: relative;
}
#u1568 {
z-index: 35;
width: 538px;
height: 1px;
border-width: 0px;
border-color: transparent;
background-color: #E2E2E2;
margin-left: 49px;
margin-top: 4px;
position: relative;
}
/* The Modal (background) */
#modalOverlay {
position: fixed;
background-color: rgba(0,0,0,0.1);
width: 100%;
height: 100%;
left: 0;
top: 0;
right: 0;
display: none;
z-index: 99;
}
.modal {
display: none;
opacity: 0;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 100;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 50%;
top: 50%;
width: 300px;
height: 300px;
overflow: auto;
margin-left: -150px;
margin-top: -150px;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(255, 255, 255);
/* Fallback color */
background-color: rgba(255, 255, 255.4);
/* Black w/ opacity */
-webkit-animation-name: fadeIn;
/* Fade in the background */
-webkit-animation-duration: .3s;
-webkit-animation-fill-mode: forwards;
animation-name: fadeIn;
animation-duration: .3s;
animation-fill-mode: forwards;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 0px;
width: 822px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.02), 0 6px 20px 0 rgba(0, 0, 0, 0.02);
}
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
@keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
.modalClose {
-webkit-animation-name: fadeOut;
/* Fade in the background */
-webkit-animation-duration: .3s;
animation-name: fadeOut;
animation-duration: .3s;
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
}
/* The Close Button */
.close {
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
color: #5B5B5B;
font-size: 30px;
font-family: source-sans-pro, sans-serif;
font-weight: 300;
position: relative;
}
.modal-body {
width: 538px;
color: #7A7A7A;
font-size: 14px;
font-family: source-sans-pro, sans-serif;
font-weight: 400;
position: relative;
}
.modal-footer {
padding: 2px 16px;
background-color: #ffffff;
color: blac;
}
/* Add Animation */
@-webkit-keyframes fadeIn {
from {
opacity: 0
}
to {
opacity: 1
}
}
@-webkit-keyframes fadeOut {
from {
opacity: 1
}
to {
opacity: 0
}
}
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Overlay -->
<div id="modalOverlay"></div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="clearfix grpelem" id="u1562-4">
<!-- content -->
<p>test</p>
</div>
</div>
<div class="clearfix colelem" id="u1567-4">
<!-- content -->
<p>test</p>
</div>
<div class="colelem" id="u1568">
<!-- simple frame -->
</div>
<div class="clearfix colelem" id="u1566-4">
<!-- content -->
<p>test</p>
</div>
<div class="clip_frame colelem" id="u1556">
<!-- image -->
<img class="block" id="u1556_img" src="" alt="" width="823" height="1024" />
</div>
<div class="modal-footer">
<h3>-</h3>
</div>
</div>
</div>
我喜欢用javascript,所以我用了setInterval函数。
一旦你想让它关闭,你使用:
setInterval(myModal.opacity = myModal.opacity - 0.1, 500);
因此它每 0.5 秒将不透明度更改为 -0.1。当然,您可以更改 500 以适合您的褪色速度,或更改 0.1 以更改不透明度下降的程度。
我刚开始编写脚本,我不知道如何在关闭模式时添加淡出动画(与淡入动画相同)window。
Here is a link to the project: https://jsfiddle.net/myosis/kb0o27vy/
我喜欢它的一切,它只需要一个结束动画。 有人可以帮我吗!?
:)
<!DOCTYPE html>
<html>
<head>
<style>
/* The Modal (background) */
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
padding-top: 100px; /* Location of the box */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(255, 255, 255); /* Fallback color */
background-color: rgba(255, 255, 255.4); /* Black w/ opacity */
-webkit-animation-name: fadeIn; /* Fade in the background */
-webkit-animation-duration: 5.4s;
animation-name: fadeIn;
animation-duration: 5.4s;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 0px;
width: 822px;
box-shadow: 0 4px 8px 0 rgba(0,0,0,0.02),0 6px 20px 0 rgba(0,0,0,0.02);
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
@keyframes animatetop {
from {top:-300px; opacity:0}
to {top:0; opacity:1}
}
/* The Close Button */
.close {
-webkit-animation-name: fadeOut; /* Fade in the background */
-webkit-animation-duration: 5.4s;
animation-name: fadeOut;
animation-duration: 5.4s
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
color: #5B5B5B;
font-size: 30px;
font-family: source-sans-pro, sans-serif;
font-weight: 300;
position: relative;
}
.modal-body {
width: 538px;
color: #7A7A7A;
font-size: 14px;
font-family: source-sans-pro, sans-serif;
font-weight: 400;
position: relative;
}
.modal-footer {
padding: 2px 16px;
background-color: #ffffff;
color: blac;
}
/* Add Animation */
@-webkit-keyframes fadeIn {
from {opacity: 0}
to {opacity: 1}
}
@-webkit-keyframes fadeOut {
from {opacity: 1}
to {opacity: 0}
}
</style>
</head>
<body>
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="clearfix grpelem" id="u1562-4"><!-- content -->
<p>test</p>
</div>
</div>
<div class="clearfix colelem" id="u1567-4"><!-- content -->
<p>test</p>
</div>
<div class="colelem" id="u1568"><!-- simple frame --></div>
<div class="clearfix colelem" id="u1566-4"><!-- content -->
<p>test</p>
</div>
<div class="clip_frame colelem" id="u1556"><!-- image -->
<img class="block" id="u1556_img" src="" alt="" width="823" height="1024"/>
</div>
<div class="modal-footer">
<h3>-</h3>
</div>
</div>
</div>
<script>
// Get the modal
var modal = document.getElementById('myModal');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.style.display = "block";
}
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.style.display = "none";
}
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
您可以使用 jQuery 来做到这一点
$('#myBtn').click(function(){
$('#myModal').fadeIn(5000);
});
$('.close').click(function(){
$('#myModal').fadeOut(5000);
});
如果您不想使用 jQuery,这里有一个快速解决方案。我添加了一个 setTimeout
和 CSS 的持续时间,在此超时后,模态设置为 display: none;
。在 setTimeout
之前,我添加了一个动画 class 来淡出模式。
// Get the modal
var modal = document.getElementById('myModal');
// Get the overlay
var overlay = document.getElementById('modalOverlay');
// Get the button that opens the modal
var btn = document.getElementById("myBtn");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
// When the user clicks the button, open the modal
btn.onclick = function() {
modal.className = modal.className.replace(/ modalClose/g, '');
overlay.className = overlay.className.replace(/ modalClose/g, '');
modal.style.display = "block";
overlay.style.display = "block";
};
overlay.onclick = function() {
modal.className = modal.className + ' modalClose';
overlay.className = overlay.className + ' modalClose';
setTimeout(function() {
modal.style.display = "none";
overlay.style.display = "none";
}, 300);
};
// When the user clicks on <span> (x), close the modal
span.onclick = function() {
modal.className = modal.className + ' modalClose';
overlay.className = overlay.className + ' modalClose';
setTimeout(function() {
modal.style.display = "none";
overlay.style.display = "none";
}, 300);
};
// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
};
body, html {
width: 100%;
}
.u1568 {
z-index: 23;
width: 200px;
background-color: transparent;
color: #5B5B5B;
line-height: 36px;
font-size: 30px;
font-family: source-sans-pro, sans-serif;
font-weight: 300;
position: relative;
margin-right: -10000px;
left: 25px;
}
.u1567-4 {
z-index: 31;
width: 200px;
background-color: transparent;
color: #9E9E9E;
font-size: 14px;
font-family: source-sans-pro, sans-serif;
font-weight: 400;
font-style: italic;
margin-left: 49px;
margin-top: 9px;
position: relative;
}
#u1568 {
z-index: 35;
width: 538px;
height: 1px;
border-width: 0px;
border-color: transparent;
background-color: #E2E2E2;
margin-left: 49px;
margin-top: 4px;
position: relative;
}
/* The Modal (background) */
#modalOverlay {
position: fixed;
background-color: rgba(0,0,0,0.1);
width: 100%;
height: 100%;
left: 0;
top: 0;
right: 0;
display: none;
z-index: 99;
}
.modal {
display: none;
opacity: 0;
/* Hidden by default */
position: fixed;
/* Stay in place */
z-index: 100;
/* Sit on top */
padding-top: 100px;
/* Location of the box */
left: 50%;
top: 50%;
width: 300px;
height: 300px;
overflow: auto;
margin-left: -150px;
margin-top: -150px;
/* Full height */
overflow: auto;
/* Enable scroll if needed */
background-color: rgb(255, 255, 255);
/* Fallback color */
background-color: rgba(255, 255, 255.4);
/* Black w/ opacity */
-webkit-animation-name: fadeIn;
/* Fade in the background */
-webkit-animation-duration: .3s;
-webkit-animation-fill-mode: forwards;
animation-name: fadeIn;
animation-duration: .3s;
animation-fill-mode: forwards;
}
/* Modal Content */
.modal-content {
position: relative;
background-color: #fefefe;
margin: auto;
padding: 0;
border: 0px;
width: 822px;
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.02), 0 6px 20px 0 rgba(0, 0, 0, 0.02);
}
}
/* Add Animation */
@-webkit-keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
@keyframes animatetop {
from {
top: -300px;
opacity: 0
}
to {
top: 0;
opacity: 1
}
}
.modalClose {
-webkit-animation-name: fadeOut;
/* Fade in the background */
-webkit-animation-duration: .3s;
animation-name: fadeOut;
animation-duration: .3s;
animation-fill-mode: forwards;
-webkit-animation-fill-mode: forwards;
}
/* The Close Button */
.close {
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
.modal-header {
color: #5B5B5B;
font-size: 30px;
font-family: source-sans-pro, sans-serif;
font-weight: 300;
position: relative;
}
.modal-body {
width: 538px;
color: #7A7A7A;
font-size: 14px;
font-family: source-sans-pro, sans-serif;
font-weight: 400;
position: relative;
}
.modal-footer {
padding: 2px 16px;
background-color: #ffffff;
color: blac;
}
/* Add Animation */
@-webkit-keyframes fadeIn {
from {
opacity: 0
}
to {
opacity: 1
}
}
@-webkit-keyframes fadeOut {
from {
opacity: 1
}
to {
opacity: 0
}
}
<h2>Animated Modal with Header and Footer</h2>
<!-- Trigger/Open The Modal -->
<button id="myBtn">Open Modal</button>
<!-- The Overlay -->
<div id="modalOverlay"></div>
<!-- The Modal -->
<div id="myModal" class="modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="clearfix grpelem" id="u1562-4">
<!-- content -->
<p>test</p>
</div>
</div>
<div class="clearfix colelem" id="u1567-4">
<!-- content -->
<p>test</p>
</div>
<div class="colelem" id="u1568">
<!-- simple frame -->
</div>
<div class="clearfix colelem" id="u1566-4">
<!-- content -->
<p>test</p>
</div>
<div class="clip_frame colelem" id="u1556">
<!-- image -->
<img class="block" id="u1556_img" src="" alt="" width="823" height="1024" />
</div>
<div class="modal-footer">
<h3>-</h3>
</div>
</div>
</div>
我喜欢用javascript,所以我用了setInterval函数。
一旦你想让它关闭,你使用:
setInterval(myModal.opacity = myModal.opacity - 0.1, 500);
因此它每 0.5 秒将不透明度更改为 -0.1。当然,您可以更改 500 以适合您的褪色速度,或更改 0.1 以更改不透明度下降的程度。