当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 原生js实现简单的模态框示例

原生js实现简单的模态框示例

2017年12月12日  | 移动技术网IT编程  | 我要评论

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实现简单的模态框示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网