<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
#d1{
width: 400px;
height: 300px;
z-index: 2;
position: fixed;
top: 50%;
left: 50%;
margin-left: -200px;
margin-top: -150px;
overflow: hidden;
display: none;
background-color: white;
}
#d2{
position: fixed;
left: 0;
top:0;
width: 100%;
height: 100%;
background-color: grey;
z-index: 1;
opacity: 0.8;
display: none;
}
#number{
display: block;
width: 90%;
height: 30px;
margin: 40px auto;
border: solid 1px #248CD6;
border-radius: 5px;
outline: none;
padding-left: 5px;
}
#number:focus{
border: solid 1px #248CD6;
border-radius: 5px;
box-shadow: 0 0 8px #248CD6;
}
.submit{
display: flex;
width: 90%;
height: 40px;
margin: 20px auto;
}
.cancel,.confirm{
flex: 1;
margin: 2px;
border: none;
transition: all 0.2s;
}
.cancel:hover,.confirm:hover{
background-color: #248CD6;
color: white;
}
.top{
width: 90%;
height: 20px;
margin: 2px auto;
}
.top a{
float: right;
margin-right: 2px;
text-decoration: none;
color: #3A3C3D;
opacity: 0.5;
}
.top a:hover{
opacity: 1;
}
</style>
</head>
<body>
<div id="d2"></div>
<button onclick="chickMe()">点击</button>
<div id="d1">
<div class="top">
<a href="javascript:void(0)" onclick="cancel()">Χ</a>
</div>
<input type="text" class="number" placeholder="请输入数量" id="number">
<div class="submit">
<button class="cancel" onclick="cancel()">取消</button>
<button class="confirm" onclick="confirm()">确定</button>
</div>
</div>
<script>
function chickMe() {
let d2=document.getElementById("d2");
d2.style.display="block";
let d1=document.getElementById("d1");
d1.style.display="block";
}
function cancel() {
let d2=document.getElementById("d2");
d2.style.display="none";
let d1=document.getElementById("d1");
d1.style.display="none";
console.log("您点击了取消");
}
function confirm() {
console.log("您点击了确定");
}
</script>
</body>
</html>
需要注意的是,需要添加笼罩层,覆盖原来的页面
实现效果如下: