jQuery Click Popup Layer Popup Modal Box Click Modal Box Disappear Code Sharing

  • 2021-07-13 04:00:41
  • OfStack

No more nonsense, just post the code for everyone. The specific code is as follows:


<!DOCTYPE html>
<html>
<head>
 <title>jQuery Ejection layer   Modal frame </title>
 <script src="./jquery.min.js" type="text/javascript"></script>
 <style>
  .btn{
   height:100px;
  }
  .black_over{
   display: none;
   position: fixed;
   width: 100%;
   height: 100%;
   background-color: black;
   z-index:1001;
   top: 0;
   left: 0;
   right: 0;
   left: 0;
   margin: auto;
   background-color: rgba(0,0,0,0.8);
  }
  .white_content {
   display: none;
   position: fixed;
   z-index:1002;
   overflow: auto;
  }
 </style>
 <script type="text/javascript">
  $(function () {
   // Eject hidden layer 
   function ShowDiv(show_div,bg_div){
    document.getElementById(show_div).style.display='block';
    document.getElementById(bg_div).style.display='block' ;
    var _windowHeight = $(window).height(),// Get the current window height 
      _windowWidth = $(window).width(),// Gets the current window width 
      _popupHeight = $("#"+show_div).height(),// Get the pop-up layer height 
      _popupWeight = $("#"+show_div).width();// Get the pop-up layer width 
    _posiTop = (_windowHeight - _popupHeight)/2;
    _posiLeft = (_windowWidth - _popupWeight)/2;
    $("#"+show_div).css({"left": _posiLeft + "px","top":_posiTop + "px","display":"block"});// Settings position
   };
   // Close the pop-up layer 
   function CloseDiv(show_div,bg_div)
   {
    document.getElementById(show_div).style.display='none';
    document.getElementById(bg_div).style.display='none';
   };
   $(".btn").click(function () {
    var src = $(this).attr("src");
    $("#showcont").attr("src",src);
    ShowDiv('MyDiv','fade')
   });
   $("#fade").click(function () {
    CloseDiv('MyDiv','fade')
   });
  });
 </script>
</head>
<body>
<!--<input id="Button1" type="button" value=" Click the pop-up layer " onclick="ShowDiv('MyDiv','fade')" />-->
<!-- Please change the picture to your own, and click the picture to pop up -->
<img src="tu.png" alt="sd" class="btn" >
<img src="pic.jpg" alt="sd" class="btn" >
<!-- Background layer when ejecting layer DIV-->
<div id="fade" class="black_over">
</div>
<!-- Contents of the pop-up layer -->
<div id="MyDiv" class="white_content">
<img src="tu.png" alt="" id="showcont">
</div>
</body>
</html>

Related articles: