效果图
实现原理
通过元素的 定位层级 z-index 以及 指引的显示隐藏
实现代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
}
.main {
width: 100%;
height: 100%;
background-color: #f0f0f0;
overflow: hidden;
}
ul {
display: flex;
flex-wrap: wrap;
}
li {
list-style: none;
width: 300px;
height: 400px;
text-align: center;
background-color: #fff;
font-size: 20px;
position: relative;
margin: 20px;
}
li>div:nth-child(1) {
width: 300px;
height: 400px;
line-height: 400px;
position: absolute;
}
li>div:nth-child(2) {
width: 230px;
height: 25px;
line-height: 25px;
position: absolute;
right: 0;
bottom: -33px;
background-color: #fff;
border-radius: 5px;
cursor: pointer;
left: 135px;
display: none;
}
.center {
position: fixed;
left: 0;
top: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, 0.5);
z-index: 1;
display: none;
}
</style>
</head>
<body>
<div class="main">
<ul>
<li>
<div>这是列表一</div>
<div>
<span>测试第一个数据</span>
<span>第一步</span>
</div>
</li>
<li>
<div>这是列表四</div>
<div>
<span>测试第四个数据</span>
<span>第四步</span>
</div>
</li>
<li>
<div>这是列表二</div>
<div>
<span>测试第二个数据</span>
<span>第二步</span>
</div>
</li>
<li>
<div>这是列表三</div>
<div>
<span>测试第三个数据</span>
<span>第三步</span>
</div>
</li>
</ul>
<div class="center">
</div>
</div>
<script>
let Mask = document.querySelector(".center")
let domlist = document.querySelectorAll("li")
console.log(Mask)
// 默认第一项展示
showDom(0)
// 设置元素的显示
function showDom(index) {
Mask.style.display = "block"
domlist[index].children[1].style.display = "block"
domlist[index].style.zIndex = 5
}
// 设置元素的隐藏
function hideDom() {
Mask.style.display = "none"
for (let i = 0; i < domlist.length; i++) {
domlist[i].children[1].style.display = "none"
domlist[i].style.zIndex = 1
}
}
domClick()
// 点击事件
function domClick() {
domlist[0].children[1].onclick = function () {
console.log("141")
hideDom()
showDom(2)
}
domlist[1].children[1].onclick = function () {
hideDom()
}
domlist[2].children[1].onclick = function () {
hideDom()
showDom(3)
}
domlist[3].children[1].onclick = function () {
hideDom()
showDom(1)
}
}
</script>
</body>
</html>