不使用bootstrap框架使用模态框
时间: 2023-09-02 21:11:52 浏览: 53
要在不使用 Bootstrap 框架的情况下使用模态框,可以使用原生的 HTML、CSS 和 JavaScript 来实现。
首先,在 HTML 文件中创建一个 div 元素,作为模态框的容器,并设置其样式为隐藏:
```
<div id="myModal" class="modal">
<div class="modal-content">
<span class="close">×</span>
<p>Some text in the Modal..</p>
</div>
</div>
```
然后,使用 CSS 样式来设置模态框的样式。例如:
```
.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: 10% auto;
padding: 20px;
border: 1px solid #888;
width: 80%;
}
.close {
color: #aaaaaa;
float: right;
font-size: 28px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: #000;
text-decoration: none;
cursor: pointer;
}
```
在 JavaScript 文件中,实现打开和关闭模态框的功能。例如:
```
// 获取模态框和关闭按钮
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";
}
}
```
这样就可以在不使用 Bootstrap 框架的情况下实现模态框的功能了。