jQuery实现简单弹窗遮罩效果

我还是喜欢你,像日月星辰,经久不息。仲夏时节,气候宜人,大地散发着诱人的清香。五彩斑斓的蝴蝶丰容靓饰,顾盼生姿,体态轻盈的蜜蜂乱欢飞舞,寻香弄绿,一派繁忙的景象。

效果图:

图(1)初始图

图(2)点击按钮后

代码如下:

<!DOCTYPE HTML>
<html>
 <head>
 <meta charset="UTF-8" />
 <title></title>
 <style>
  #mask {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 999;
  background: #666;
  opacity: 0.5;
  filter: alpha(opacity=50)-moz-opacity: 0.5;
  display: none;
  }
  .popup {
  position: absolute;
  left: 50%;
  width: 600px;
  height: 200px;
  background: #fff;
  z-index: 1000;
  border: 1px solid #333;
  display: none;
  }
  .btn_close {
  position: absolute;
  top: 5px;
  right: 5px;
  }
 </style>
 </head>
 <body>
 <button class="btn_show">遮罩层</button>
 <div id="mask"></div>
 <div class="popup">
  <button class="btn_close">x</button>
 </div>
 <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.js"></script>
 <script>
  $(function() {
  $('.btn_show').click(function() {
   $('#mask').css({
   display: 'block',
   height: $(document).height()
   })
   var $Popup = $('.popup');
   $Popup.css({
   left: ($('body').width() - $Popup.width()) / 2+ 'px',
   top: ($(window).height() - $Popup.height()) / 2 + $(window).scrollTop() + 'px',
   display: 'block'
   })
  })
  $('.btn_close').click(function() {
   $('#mask,.popup').css('display', 'none');
  })
  })
 </script>
 </body>
</html>

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持!

您可能有感兴趣的文章
jquery ajax实现文件上传功能实例代码

SpringMVC+Jquery实现Ajax功能

关于二次封装jquery ajax办法示例详解

ajax实现用户名校验的传统和jquery的$.post方式(实例讲解)

jQuery Ajax的readyState和status的区别和使用详解