html5弹窗代码
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5弹窗示例</title>
<style>
/* 弹窗样式 */
.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.5); /* 半透明黑色背景 */
}
.modal-content {
background-color: #fefefe;
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;
}
</style>
</head>
<body>
<!-- 弹窗内容 -->
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>这是一个弹窗示例。</p>
</div>
</div>
<!-- 触发弹窗的按钮 -->
<button id="myBtn">打开弹窗</button>
<script>
// 获取弹窗和关闭按钮
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
// 点击按钮打开弹窗
btn.onclick = function() {
modal.style.display = "block";
}
// 点击关闭按钮关闭弹窗
span.onclick = function() {
modal.style.display = "none";
}
// 当用户点击弹窗外部区域,关闭弹窗
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
这个示例中,有一个按钮,点击按钮会弹出一个带有文本内容的弹窗。用户可以点击弹窗的关闭按钮或者点击弹窗外部的区域来关闭弹窗。
html<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5弹窗示例</title>
<style>
/* 弹窗样式 */
.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.5); /* 半透明黑色背景 */
}
.modal-content {
background-color: #fefefe;
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;
}
</style>
</head>
<body>
<!-- 弹窗内容 -->
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>这是一个弹窗示例。</p>
</div>
</div>
<!-- 触发弹窗的按钮 -->
<button id="myBtn">打开弹窗</button>
<!-- JavaScript 部分 -->
<script>
// 获取弹窗和关闭按钮
var modal = document.getElementById("myModal");
var btn = document.getElementById("myBtn");
var span = document.getElementsByClassName("close")[0];
// 点击按钮打开弹窗
btn.onclick = function() {
modal.style.display = "block";
}
// 点击关闭按钮关闭弹窗
span.onclick = function() {
modal.style.display = "none";
}
// 当用户点击弹窗外部区域,关闭弹窗
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
</script>
</body>
</html>
这段代码包含了一个简单的弹窗功能。当点击按钮"打开弹窗"时,会显示一个包含文本内容的弹窗。用户可以点击弹窗上的关闭按钮或者点击弹窗外部来关闭弹窗。