Simple implementation code of JavaScript mask of model function

  • 2021-07-09 06:34:18
  • OfStack

Thoughts:

Create a mask and set the stacking order of the mask to ensure that other elements can be covered


position: absolute;
top: 0;
left: 0;
display: none;
background-color: rgba(9, 9, 9, 0.63);
width: 100%;
height: 100%;
z-index: 1000; • Set the background color and display format of the contents in the mask 
width: 50%;
text-align: center;
background: #ffffff;
border-radius: 6px;
margin: 100px auto;
line-height: 30px;
z-index: 10001; • Binding event ,  Dynamic switching mask  display  Attribute 
function showModel() {
document.getElementById('myModel').style.display = 'block';
}
function closeModel() {
document.getElementById('myModel').style.display = 'none';
}

Note: The mask should be absolutely positioned, the width and height should occupy the whole page, and the stacking order should be large

Source code


<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<title> Mask </title>
<style>
.container {
width: 900px;
margin: 50px auto;
text-align: center;
}
#myModel {
position: absolute;
top: 0;
left: 0;
display: none;
background-color: rgba(9, 9, 9, 0.63);
width: 100%;
height: 100%;
z-index: 1000;
}
.model-content {
width: 50%;
text-align: center;
background: #ffffff;
border-radius: 6px;
margin: 100px auto;
line-height: 30px;
z-index: 10001;
}
</style>
</head>
<body>
<div class="container">
<button onclick="showModel()"> Ejection mask </button>
<div id="myModel" onclick="closeModel()">
<div class="model-content">
<p> Hello , I am the content ~~</p>
<p>
<button id="closeModel" onclick="closeModel()"> Shut down </button>
</p>
</div>
</div>
</div>
<script>
function showModel() {
document.getElementById('myModel').style.display = 'block';
}
function closeModel() {
document.getElementById('myModel').style.display = 'none';
}
</script>
</body>
</html>

Related articles: