代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>放大镜</title>
<style>
* {
margin: 0;
padding: 0;
list-style: none;
}
.list {
width: 640px;
height: 340px;
border: 1px solid black;
background-color: slategrey;
}
.list li {
width: 200px;
height: 100px;
text-align: center;
line-height: 100px;
font-size: 25px;
background-color: salmon;
float: left;
margin-left: 10px;
margin-top: 10px;
}
.mb {
width: 642px;
height: 342px;
background-color: rgba(176, 173, 173, 0.317);
position: absolute;
top: 0;
left: 0;
}
.biger {
width: 300px;
height: 200px;
font-size: 25px;
text-align: center;
line-height: 200px;
background-color: salmon;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
</style>
</head>
<body>
<ul class="list">
<li onclick="big(this)">1</li>
<li onclick="big(this)">2</li>
<li onclick="big(this)">3</li>
<li onclick="big(this)">4</li>
<li onclick="big(this)">5</li>
<li onclick="big(this)">6</li>
<li onclick="big(this)">7</li>
<li onclick="big(this)">8</li>
<li onclick="big(this)">9</li>
</ul>
<!-- <div class="mb"></div> -->
</body>
</html>
<script>
function big(that) {
//拷贝当前节点
var cop = that.cloneNode(true);
// 将拷贝的节点创建类名
cop.className = 'biger';
// 创建蒙版div
var div_ = document.createElement('div');
//将创建的蒙版,起类名
div_.className = 'mb';
// 将复制的节点追加到蒙版中
div_.appendChild(cop);
//将蒙版追加到body下
var body_ = document.getElementsByTagName('body')[0];
body_.appendChild(div_);
// 点击蒙版删除复制的节点和蒙版
div_.onclick = function () {
body_.removeChild(div_);
body_.removeChild(cop);
}
}
</script>