要创建简单的模态对话框,您可以使用HTML、CSS和JavaScript。以下是一些步骤:
<button id="open-modal">打开模态对话框</button>
<div id="modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>模态对话框的内容在这里</p>
</div>
</div>
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgba(0,0,0,0.4);
}
.modal-content {
background-color: #fff;
margin: 15% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: black;
text-decoration: none;
cursor: pointer;
}
// 获取模态对话框和按钮
var modal = document.getElementById("modal");
var btn = document.getElementById("open-modal");
var close = document.getElementsByClassName("close")[0];
// 单击按钮打开模态对话框
btn.onclick = function() {
modal.style.display = "block";
}
// 单击关闭按钮或模态对话框外部关闭模态对话框
close.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
现在,您应该可以在页面上创建一个简单的模态对话框了!