js 实现简单的开关灯效果
程序员文章站
2022-05-29 20:49:53
...
代码如下,难点在于position属性和z-index属性
<!DOCTYPE html>
<html>
<head>
<title></title>
<style>
.mask {
position: fixed;
top: 0px;
left: 0px;
width: 100%;
height: 100%;
background-color: black;
opacity: 0.0;
z-index: 100;
}
.player {
position: relative;
height: 500px;
width: 80%;
margin: 20px auto;
text-align: center;
line-height: 400px;
font-size: 100px;
z-index: 101;
background: white;
}
.kg {
display: block;
font-size: 50px;
}
.mask-active {
opacity: 0.9;
}
</style>
</head>
<body>
<div class="mask mask-active"></div>
<div class="player">
播放器
<button class="kg">开关</button>
</div>
</body>
<script>
var log = function() {
console.log.apply(console, arguments);
}
var $ = function(ele) {
return document.querySelector(ele);
}
var kg = $('.kg');
var msk = $('.mask');
kg.addEventListener('click', function() {
if (msk.classList.contains('mask-active')) {
msk.classList.remove('mask-active');
} else {
msk.classList.add('mask-active');
}
})
</script>
</html>