/*css文件*/
/* 遮罩层 */
#modal-overlay {
visibility: hidden;
position: absolute;
left: 0px; /*如果 left和top不设置为0,边框会有白边 */
top: 0px;
width: 100%;
height: 100%;
text-align: center;
z-index: 1000;
backgroun-color: #333;
opacity: 0.5;
}
/*内容*/
.modal-data {
width: 300px;
margin: 100px auto;
background-color: #fff;
border: 1px solid #000;
padding: 15px;
text-align: center;
}
<!-- html内容-->
<div id="modal-overlay">
<div class="modal-data">
<p>简单的模态对话框</p>
<p>点击<a href="javascript:overlay()">关闭</a></p>
</div>
</div>
<button onclick="showModal()">显示模态对话框</button>
//js代码
function overlay() {
var el = document.getElementById('modal-overlay');
el.style.visibility = (el.style.visibility == "visible") ? "hidden" : "visible";
}
function showModal() {
var el = document.getElementById('modal-overlay');
el.style.visibility = "visible";
}
模态框的美化
<!-- html代码 -->
<!-- 遮罩层 -->
<div id="openModal" class="modalDialog">
<div> <!-- 对话框 -->
<a href="#close" title="Close" class="close">X</a> <!-- 关闭按钮-->
<h2>Modal Box</h2>
<p>This is a sample modal box that can be created using the power of CSS3.</p>
<p>You could do a lot of things here like have a pop-up ad that shows when your website loads, or create a login/register form for users.</p>
</div>
</div>
/* css代码 */
.modalDialog {
position: fixed;
font-family: Arial, Helvetica, sans-serif;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color; rgba(0, 0, 0, 0.8);
z-index: 9999;
opacity: 0; /*初始未显示,opacity: 1;显示*/
-webkit-transition: opacity 400ms ease-in;
-moz-transition: opacity 400ms ease-in;
transition: opacity 400ms ease-in;
/* ease:逐渐慢下来;linear:匀速;ease-in:由慢到快;ease-out:由快到慢;ease-in-out:先慢到快再到慢*/
pointer-events: none; /*元素永远不会成为鼠标事件的target。*/
}
.modalDialog:target {
opacity: 1;
}
.modalDialog > div {
width: 400px;
position: relative;
margin: 10% auto;
padding: 5px 20px 13px 20px;
border-radius: 10px;
background: #fff;
background: -webkit-linear-gradient(#fff, #999);
background: -o-linear-gradient(#fff, #999);
background: -moz-linear-gradient(#fff, #999);
}
.close {
background: #606061;
color: #FFFFFF;
line-height: 25px;
position: absolute;
right: -12px;
text-align: center;
top: -10px;
width: 24px;
text-decoration: none;
font-weight: bold;
border-radius: 50%;
box-shadow: 1px 1px 3px #000;
pointer-events: auto;
}
.close:hover {
background: #00d9ff;
}