html弹出窗口如何设置在屏幕正中
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Popup in Center</title>
<style>
/* 弹出窗口的样式 */
.popup {
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background-color: white;
padding: 20px;
border: 1px solid #ccc;
z-index: 10;
}
/* 遮罩层的样式 */
.overlay {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: rgba(0, 0, 0, 0.5);
z-index: 5;
}
</style>
</head>
<body>
<button id="openPopup">打开弹出窗口</button>
<div id="overlay" class="overlay" style="display: none;"></div>
<div id="popup" class="popup" style="display: none;">
这是一个弹出窗口。</br>
现在知道怎么做一个居中的弹出窗口了吗
<button id="closePopup">关闭</button>
</div>
<script>
// 打开弹出窗口
document.getElementById('openPopup').onclick = function() {
document.getElementById('overlay').style.display = 'block';
document.getElementById('popup').style.display = 'block';
};
// 关闭弹出窗口
document.getElementById('closePopup').onclick = function() {
document.getElementById('overlay').style.display = 'none';
document.getElementById('popup').style.display = 'none';
};
</script>
</body>
</html>发表评论