原生js实现简单的模态框示例
更新时间:2017年09月08日 08:08:22 投稿:jingxian
下面小编就为大家带来一篇原生js实现简单的模态框示例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
html部分:
<img src="images/8.jpg" alt=""> <button class="btn" id="showMax">显示大图</button> <div id="modalBox" class="modalBox"> <div class="modalBox-matter"> <header class="modalBox-header"> <span class="mtclose">×</span> </header> <div class="modalBox-body"> <img src="images/8-1.jpg"> </div> </div> </div>
js部分:
var btn = document.getElementById('showMax');
var mtclose = document.getElementsByClassName('mtclose')[0];
var modalBox = document.getElementById('modalBox');
btn.addEventListener('click', function(){
modalBox.style.display = "block";
});
mtclose.addEventListener('click', function(){
modalBox.style.display = "none";
});
css部分:
.btn{
width: 100px;
height: 35px;
border-radius: 5px;
font-size: 16px;
color: #F97B39;
position: absolute;
top: 130px;
left: 160px;
opacity: 0.2;
cursor: pointer; /*鼠标小手*/
}
.btn:hover, .btn:focus{ /*focus 取得焦点状态*/
background-color: #8AA7F9;
opacity: 0.5;
color: #FFFFFF;
}
.modalBox{
display: none;
width: 100%;
height: 100%;
position: fixed;
left: 0;
top: 0;
z-index: 1000;
background-color: rgba(0,0,0,0.5);
}
.modalBox-matter{
display: flex; /*/*弹性布局*/
flex-flow: column nowrap;
justify-content: space-between; /*两端对齐*/
width: 50%;
height: 80%;
margin: 30px auto 100px;
border-radius:10px;
-webkit-animation: zoom 0.6s;
animation: zoom 0.6s;
resize: both;
overflow: auto;
}
@keyframes zoom{
from {transform: scale(0)}
to {transform: scale(1)}
}
.modalBox-header{
margin-left: 617px;
}
.mtclose{
color: #602E2A;
font-size: 3em;
font-weight: bold;
transition: all 0.3s;
/*z-index: 1010; */
}
.mtclose:hover, .mtclose:focus{
color: #602E2A;
cursor: pointer;
}
.modalBox-body{
padding: 10px;
font-size: 16px;
}
效果


因为正在进行的一个项目中,需要一个模态框,所以花时间在网上自学的,相对来说比较简单,可以自行修改内容。。。
以上这篇原生js实现简单的模态框示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
JS实现兼容火狐及IE iframe onload属性的遮罩层隐藏及显示效果
这篇文章主要介绍了JS实现兼容火狐及IE iframe onload属性的遮罩层隐藏及显示效果,涉及javascript事件响应及针对页面元素属性的动态操作相关技巧,需要的朋友可以参考下2016-08-08
ElementUI的Dialog弹窗实现拖拽移动功能示例代码
这篇文章主要介绍了ElementUI的Dialog弹窗实现拖拽移动功能,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2023-07-07
JavaScript 节点操作 以及DOMDocument属性和方法
最近发现DOMDocument对象很重要,还有XMLHTTP也很重要 注意大小写一定不能弄错.2007-12-12
javascript代码在ie8里报错 document.getElementById(...) 为空或不是对象的解决方
今天更升级了ie8,发现原来在ie7下可以运行的代码,不能运行了,发现了一些细节,附临时修改办法。2009-11-11


最新评论