使用js, html,css 实现一个弹窗网页
时间: 2024-02-06 19:09:11 浏览: 18
好的,以下是一个简单的弹窗网页实现:
HTML代码:
```html
<!DOCTYPE html>
<html>
<head>
<title>弹窗网页示例</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<h1>欢迎来到弹窗网页</h1>
<button id="openModal">打开弹窗</button>
<div id="modal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>这是一个弹窗示例。</p>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
```
CSS代码:
```css
.modal {
display: none;
position: fixed;
z-index: 1;
left: 0;
top: 0;
width: 100%;
height: 100%;
overflow: auto;
background-color: rgb(0,0,0);
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;
}
```
JavaScript代码:
```javascript
var modal = document.getElementById("modal");
var btn = document.getElementById("openModal");
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";
}
}
```
这段代码实现了一个简单的弹窗网页,包括一个打开弹窗的按钮和一个弹窗。在点击按钮时,弹窗会显示出来,在点击弹窗内容区域以外的地方时,弹窗会关闭。其中,CSS部分定义了弹窗的样式,JavaScript部分定义了弹窗的行为,HTML部分则是整个网页的结构。