写一段弹出框的html5代码
时间: 2023-12-01 09:22:54 浏览: 41
<!DOCTYPE html>
<html>
<head>
<title>弹出框示例</title>
<meta charset="UTF-8">
<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.4); /* 背景颜色,半透明 */
}
/* 弹出框内容样式 */
.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>
<!-- 触发弹出框的按钮 -->
<button onclick="openModal()">打开弹出框</button>
<!-- 弹出框内容 -->
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close" onclick="closeModal()">×</span>
<p>这是一个弹出框示例</p>
</div>
</div>
<script>
// 打开弹出框
function openModal() {
document.getElementById("myModal").style.display = "block";
}
// 关闭弹出框
function closeModal() {
document.getElementById("myModal").style.display = "none";
}
</script>
</body>
</html>
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)