我正在构建自己网站的版本 2,并且正在尝试制作弹出滑出动画。问题是我尝试过制作一个,但总是搞砸,而且没有任何效果。我做了一个滑入动画,可以工作,但现在我需要一个滑出动画。
如何为我的弹出窗口制作滑出弹出动画。如果有人愿意帮忙,我将不胜感激!
代码如下:
function openPopup() {
document.getElementById("popup").style.display = "block";
}
function closePopup() {
document.getElementById("popup").style.display = "none";
}
.popup {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
align-items: center;
justify-content: center;
z-index: 9999;
}
.popup-content {
background-color: #ffffff;
padding: 20px;
border-radius: 5px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.2);
text-align: center;
width: 400px;
height: 400px;
position: relative;
right: -600px;
top: 200px;
animation: popUPAnimation 1s forwards;
}
@keyframes popUPAnimation {
0% {
transform: scale(0);
}
50% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
.popup-content h1 {
color: #333333;
font-size: 2.5rem;
text-align: center;
text-decoration: underline;
}
.popup-content h2 {
color: #333333;
font-size: 1.3rem;
text-align: center;
margin-top: 10px;
}
.solid-popup {
position: absolute;
border-top: 1px solid #bbb;
width: 70%;
margin-top: 20px;
margin-left: 40px;
}
.popup-content button {
background: #EEEEEE;
border: none;
border-radius: 5px;
padding: 5px 10px;
margin: 10px;
position: relative;
top: 30px;
cursor: pointer;
transition: all .45s ease;
}
.popup-content button:hover {
background: #dedede;
}
.popup-close {
position: absolute;
top: -10px;
right: 5px;
font-size: 3rem;
cursor: pointer;
color: #333333;
transition: all .45s ease;
}
.popup-close:hover {
transform: scale(1.1);
color: #0f0f0f;
}
<button onclick="openPopup()">Open popup</button>
<div id="popup" class="popup">
<div class="popup-content">
<span class="popup-close" onclick="closePopup()">×</span>
<h1>Sort products</h1>
<h2>Categories</h2>
<hr class="solid-popup">
<div class="popup-buttons">
<div>
<button onclick="window.location.href='#';">General Websites</button>
</div>
<div>
<button onclick="window.location.href='#';">Fivem Websites</button>
</div>
<div>
<button onclick="window.location.href='#';">Custom made websites</button>
</div>
<div>
<button onclick="window.location.href='#';">Ecommerce Websites</button>
</div>
<div>
<button onclick="window.location.href='#';">All Websites</button>
</div>
</div>
</div>
</div>
HTML5已经进化了,你应该使用
<dialog>
元素,它节省了很多代码。
const
btShowDial = document.querySelector('#bt-show_mDialog')
, dialogBox = document.querySelector('#dialog-box')
, btCloseDial = dialogBox.querySelector('b.popup-close')
;
btShowDial.onclick =_=> { dialogBox.showModal() }
;
btCloseDial.onclick =_=>
{
dialogBox.classList.add('close');
setTimeout(()=>
{
dialogBox.close()
dialogBox.classList.remove('close');
}, 1500);
};
dialog[open] {
animation: popUPAnim 700ms forwards;
}
dialog.close {
animation: popOutAnim 700ms forwards;
}
@keyframes popUPAnim {
0% { transform : scale(0); }
80% { transform : scale(1.1); }
100% { transform : scale(1); }
}
@keyframes popOutAnim{
0% { transform : scale(1); }
20% { transform : scale(1.1); }
100% { transform : scale(0); }
}
dialog {
position : relative;
border : 1px solid black;
padding : 0;
border-radius : 5px;
text-align : center;
min-width : 24em;
}
dialog::backdrop {
background : #0af3cc5d;
}
dialog > b.popup-close {
position : absolute;
top : -12px;
right : 1px;
font-size : 3rem;
cursor : pointer;
color : #333333;
transition : all .45s ease;
caret-color : transparent;
}
dialog > b.popup-close:hover {
transform : scale(1.1);
color : #0f0f0f;
}
#dialog-box button {
display : block;
margin : 0.5em auto;
}
<button id="bt-show_mDialog">show modal dialog..</button>
<dialog id="dialog-box">
<b class="popup-close">×</b>
<h1>Sort products</h1>
<h3>Categories</h3>
<hr>
<button> General Websites </button>
<button> Fivem Websites </button>
<button> Custom made websites </button>
<button> Ecommerce Websites </button>
<button>All Websites</button>
</dialog>