实现一个居中半透明的模态窗

<!DOCTYPE html>
<html>
<head>
<title>Modal Example</title>
<style>
body {
  font-family: sans-serif;
}

.modal {
  display: none; /* Hidden by default */
  position: fixed; /* Stay in place */
  z-index: 1; /* Sit on top */
  left: 0;
  top: 0;
  width: 100%; /* Full width */
  height: 100%; /* Full height */
  overflow: auto; /* Enable scroll if needed */
  background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
}

.modal-content {
  background-color: #fefefe;
  margin: 15% auto; /* 15% from the top and centered */
  padding: 20px;
  border: 1px solid #888;
  width: 80%; /* Could be more or less, depending on screen size */
  max-width: 600px; /* Limit the maximum width */
  border-radius: 5px; /* Rounded corners */
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2); /* Add a shadow */
}

.close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
}

.close:hover,
.close:focus {
  color: black;
  text-decoration: none;
  cursor: pointer;
}
</style>
</head>
<body>

<h2>Modal Example</h2>

<button id="myBtn">Open Modal</button>

<div id="myModal" class="modal">

  <div class="modal-content">
    <span class="close">&times;</span>
    <p>Some text in the Modal..</p>
  </div>

</div>

<script>
// Get the modal
var modal = document.getElementById("myModal");

// Get the button that opens the modal
var btn = document.getElementById("myBtn");

// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];

// When the user clicks the button, open the modal 
btn.onclick = function() {
  modal.style.display = "block";
}

// When the user clicks on <span> (x), close the modal
span.onclick = function() {
  modal.style.display = "none";
}

// When the user clicks anywhere outside of the modal, close it
window.onclick = function(event) {
  if (event.target == modal) {
    modal.style.display = "none";
  }
}
</script>

</body>
</html>

Explanation and Key Features:

  • HTML Structure:

    • The modal is contained within a div with the class modal.
    • Inside the modal is another div with the class modal-content which holds the actual content of the modal.
    • A close button (a span with the class close) is included within the modal-content.
    • A button (myBtn) is used to trigger the modal's opening.
  • CSS Styling:

    • Centering: The modal-content is centered using margin: 15% auto;. auto for left and right margins centers the element horizontally. 15% top margin positions it vertically.
    • Translucency/Opacity: The .modal background is set to rgba(0,0,0,0.4) which creates a semi-transparent black overlay.
    • Full Screen Overlay: The .modal is positioned fixed and takes up the full width and height of the screen.
    • Rounded Corners and Shadow: border-radius and box-shadow are used for visual enhancement.
  • JavaScript Functionality:

    • Opening the Modal: Clicking the button sets the modal's display to block.
    • Closing the Modal: Clicking the close button or clicking anywhere outside the modal sets the modal's display to none.

How to Use:

  1. Save the code as an HTML file (e.g., modal.html).
  2. Open the file in a web browser.
posted @ 2024-12-05 09:11  王铁柱6  阅读(18)  评论(0)    收藏  举报