JavaScript 实现简易弹出层(Lightbox)效果

作者:像素猫时间:2026-10-30 14:58:27浏览:306

本文分享了一个基于 JavaScript 和 CSS 实现的简易弹出层(Lightbox)示例。通过定义遮罩层与内容层的样式,结合简单的脚本控制显示与隐藏,实现点击触发弹窗及关闭功能。代码结构清晰,适合初学者参考学习前端交互效果的基础实现方式。

JavaScript 弹出层实现方案

本文分享了一个实用的弹出层(Div)实现方案,供读者参考。

核心代码结构


<html> 
<head> 
<meta charset="utf-8" />
  <title>LIGHTBOX EXAMPLE</title> 
  <style> 
   .black_overlay{ display: none; position: absolute; top: 0%; left: 0%; width: 100%; height: 100%; background-color: black; z-index:1001; -moz-opacity: 0.6; opacity:.60; filter: alpha(opacity=60); } 
   .white_content { display: none; position: absolute; top: 25%; left: 25%; width: 50%; height: 50%; padding: 16px; border: 16px solid orange; background-color: white; z-index:1002; overflow: auto; } 
   </style> 
   <script>
   function one(){
    document.getElementById('light').style.display='block';
    document.getElementById('fade').style.display='block'
   }
   function two(){
    document.getElementById('light').style.display='none';
    document.getElementById('fade').style.display='none'
   }
   </script> 
</head> 
<body> 
  <p>可以根据自己要求修改css样式
    <a href="javascript:void(0)" onclick="one()">点击这里打开窗口</a>
  </p> 
  <div id="light" class="white_content"> 
    This is the lightbox content. 
    <a href="javascript:void(0)" onclick="two()">Close</a>
  </div> 
  <div id="fade" class="black_overlay"> 
  </div> 
</body> 
</html> 

以上就是本文的全部内容,希望能为读者提供帮助。如需获取更多相关技术内容,请持续关注本站更新。

作者最新文章