如何实现点击弹出窗口,再点击关闭窗口
时间: 2023-07-19 12:10:24 浏览: 94
实现点击弹出窗口,再点击关闭窗口的方法有很多种,其中一种比较简单的方法是使用 JavaScript 和 CSS。
首先,在 HTML 文件中创建一个按钮或者链接,当用户点击按钮或链接时,弹出一个新的窗口。
```html
<button onclick="openPopup()">点击我打开弹窗</button>
<div id="popup" class="popup">
<div class="popup-content">
<span class="close" onclick="closePopup()">×</span>
<p>这里是弹窗的内容</p>
</div>
</div>
```
接着,在 JavaScript 文件中编写打开和关闭弹窗的函数。
```javascript
function openPopup() {
document.getElementById("popup").style.display = "block";
}
function closePopup() {
document.getElementById("popup").style.display = "none";
}
```
最后,在 CSS 文件中定义弹窗的样式。
```css
.popup {
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);
}
.popup-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;
}
```
这样,当用户点击按钮时,就会弹出一个新的窗口,当用户点击关闭按钮时,弹窗会关闭。
阅读全文
相关推荐















