BurgerDialog - 弹出式对话框组件库
<p><!doctype html></p>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" id="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0">
<link rel="shortcut icon" href="favicon.png" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="waves/waves.min.css" />
<title>BurgerDialog - 弹出式对话框组件库</title>
<style>
* {
font-family: sans-serif;
}
<pre><code> html,
body {
margin: 0;
}
.burger-dialog {
height: 160px;
width: 280px;
background-color: #EDE7F6;
z-index: -1;
position: fixed;
text-align: center;
left: calc(50% - 156px);
top: calc(50% - 96px);
border-radius: 16px;
box-shadow: 0 4px 32px 4px rgba(0, 0, 0, .4);
padding: 16px;
opacity: 0;
display: none;
transform: scale(.85);
transition-property: opacity, transform;
transition-duration: .3s;
transition-timing-function: cubic-bezier(0.77, 0, 0.175, 1);
}
@media (min-width: 1024px) {
.burger-dialog {
width: 640px;
left: calc(50% - 336px);
}
}
.burger-dialog-title {
font-size: 20px;
text-align: left;
margin: 4px 0;
}
.burger-dialog-content {
text-align: left;
margin: 16px 0;
color: rgba(0, 0, 0, .7);
}
.burger-dialog-actions {
position: absolute;
bottom: 16px;
right: 16px;
float: right;
}
.burger-dialog-actions button {
border: none;
padding: 8px 16px;
background-color: #EDE7F6;
color: #673AB7;
border-radius: 16px;
display: inline-block;
margin: 4px 0;
transition-property: filter;
transition-duration: .3s;
}
.burger-dialog-actions button:hover {
filter: brightness(0.8);
}
.cover {
position: fixed;
height: 100%;
width: 100%;
left: 0;
top: 0;
z-index: -1;
background-color: rgba(0, 0, 0, .5);
transition-property: opacity;
transition-duration: .3s;
}
</style>
<style>
.btn {
border: none;
padding: 8px 16px;
background-color: #EDE7F6;
color: #673AB7;
border-radius: 16px;
display: inline-block;
margin: 16px;
transition-property: filter;
transition-duration: .3s;
}
.btn:hover {
filter: brightness(0.8);
}
</style>
</code></pre>
</head>
<body>
<h1 style="text-align: center;color: #673AB7;">BurgerDialog</h1>
<div style="text-align: center;">
<button class="btn waves-block" onclick="openDialog('#dialog-1');">弹出对话框-1</button>
</div>
<div class="burger-dialog" id="dialog-1">
<div class="burger-dialog-title">对话框标题</div>
<div class="burger-dialog-content">对话框内容<br>Made by Burger Studio.</div>
<div class="burger-dialog-actions">
<button class="waves-block" onclick="closeDialog('#dialog-1');">取消</button>
<button class="waves-block" onclick="closeDialog('#dialog-1');">确定</button>
</div>
</div>
<script>
function openDialog(element) {
document.querySelector(element).style.display = "block";
window.setTimeout(function () {
document.querySelector(element).style.opacity = "1";
document.querySelector(element).style.transform = "scale(1)";
document.querySelector(element).style.zIndex = 999;
var cover = document.createElement("div");
document.body.appendChild(cover);
cover.classList.add("cover");
window.setTimeout(function () {
cover.style.opacity = "1";
}, 0);
}, 0);
}
function closeDialog(element) {
document.querySelector(element).style.opacity = "0";
window.setTimeout(function () {
document.querySelector(element).style.display = "none";
document.querySelector(element).style.transform = "scale(.85)";
document.querySelector(element).style.zIndex = -1;
}, 300);
}
</script>
<script src="waves/waves.min.js"></script>
<script>
var config = {
duration: 500,
delay: 0
};
Waves.init(config);
Waves.attach('.waves-block', ['waves-block']);
Waves.attach('.waves-circle', ['waves-circle']);
Waves.attach('.waves-light', ['waves-light']);
Waves.attach('.waves-classic', ['waves-classic']);
Waves.init(config);
</script>
</body>
</html>
这可能是因为在创建cover元素后立即设置了其opacity属性为0,然后又立即设置为1,没有给它一个过渡效果的机会。可以尝试将cover元素的opacity属性的设置移动到setTimeout函数内部的代码块中,以便在打开对话框之后再设置opacity属性,从而触发过渡动画。修改后的代码如下:
<pre><code class="language-javascript">function openDialog(element) {
document.querySelector(element).style.display = "block";
window.setTimeout(function () {
document.querySelector(element).style.opacity = "1";
document.querySelector(element).style.transform = "scale(1)";
document.querySelector(element).style.zIndex = 999;
var cover = document.createElement("div");
document.body.appendChild(cover);
cover.classList.add("cover");
window.setTimeout(function () {
cover.style.opacity = "1";
}, 0);
}, 0);
}
</code></pre>
原文地址: https://www.cveoy.top/t/topic/pX2S 著作权归作者所有。请勿转载和采集!