HTML5循環(huán)彈框代碼
<!DOCTYPE html>
<html>
<head>
<title>HTML5循環(huán)彈框</title>
<style>
/* 遮罩層 */
.overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 9999;
display: none;
}
/* 對話框 */
.dialog {
position: fixed;
top: 50%;
left: 50%;
margin-top: -150px;
margin-left: -250px;
width: 500px;
height: 300px;
background-color: #fff;
border-radius: 5px;
z-index: 10000;
display: none;
}
</style>
</head>
<body>
<button id="btnShowDialog">顯示對話框</button>
<div class="overlay"></div>
<div class="dialog">
<h2>這是一個對話框</h2>
<p>對話框中的內(nèi)容...</p>
<button id="btnCloseDialog">關(guān)閉對話框</button>
</div>
<script>
window.onload = function() {
var btnShowDialog = document.getElementById('btnShowDialog');
var btnCloseDialog = document.getElementById('btnCloseDialog');
var overlay = document.querySelector('.overlay');
var dialog = document.querySelector('.dialog');
btnShowDialog.onclick = function() {
overlay.style.display = 'block'; // 顯示遮罩層
dialog.style.display = 'block'; // 顯示對話框
};
btnCloseDialog.onclick = function() {
overlay.style.display = 'none'; // 隱藏遮罩層
dialog.style.display = 'none'; // 隱藏對話框
};
overlay.onclick = function() {
btnCloseDialog.click(); // 點(diǎn)擊遮罩層相當(dāng)于點(diǎn)擊關(guān)閉按鈕
};
};
</script>
</body>
</html>