效果:鼠标经过时显示遮罩层
复习:遮罩层用绝对定位,不占有位置。因此外面的.box用相对定位。
代码:用css实现
<!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>
.box {
position: relative;
margin: 0 auto;
width: 400px;
height: 270px;
border: 1px solid red;
}
img {
width: 100%;
height: 100%;
}
.mask {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3) url(images/tudou.png) no-repeat;
}
/* 不能直接写 .mask:hover,因为默认是不显示的,所以点不到它。因此可以写成,鼠标到box以后,让其子元素mask显示。 */
.box:hover .mask{
display: block;
}
</style>
</head>
<body>
<div class="box">
<div class="mask"></div>
<img src="../../js/images/bg1.jpg" alt="">
</div>
</body>
</html>
代码:用伪类选择实现。
<!-- <!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>
.box {
position: relative;
margin: 0 auto;
width: 400px;
height: 270px;
border: 1px solid red;
}
img {
width: 100%;
height: 100%;
}
.mask {
display: none;
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3) url(images/tudou.png) no-repeat;
}
/* 不能直接写 .mask:hover,因为默认是不显示的,所以点不到它。因此可以写成,鼠标到box以后,让其子元素mask显示。 */
.box:hover .mask{
display: block;
}
</style>
</head>
<body>
<div class="box">
<div class="mask"></div>
<img src="../../js/images/bg1.jpg" alt="">
</div>
</body>
</html> -->
<!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>
.box {
position: relative;
margin: 0 auto;
width: 400px;
height: 270px;
border: 1px solid red;
}
img {
width: 100%;
height: 100%;
}
/* 用伪元素的方式来写 */
.box:hover::after {
content: '';
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.3) url(images/tudou.png) no-repeat;
}
</style>
</head>
<body>
<div class="box">
<img src="../../js/images/bg1.jpg" alt="">
</div>
</body>
</html>