<!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>Document</title>
<style>
.box {
margin: 0 auto;
/* 父盒子水平居中 */
position: relative;
/* 父盒子相对定位(子绝父相) */
width: 300px;
height: 300px;
background-color: skyblue;
overflow: hidden;
/* 溢出父盒子,隐藏 */
color: blue;
}
.box1 {
position: absolute;
/* 子盒子绝对定位(子绝父相) */
left: -386px;
top: 0;
width: 300px;
height: 300px;
/* background-color: skyblue; */
background: linear-gradient(90deg, rgba(255, 255, 255, 0),
rgba(255, 255, 255, .5), rgba(255, 255, 255, 0));
transform: skew(-30deg);
/* 给子盒子设置背景和变形 */
/* transition: all .5s; */
}
.box:hover .box1 {
/* 给父盒子设置:hover伪类,鼠标经过父盒子box,
子盒子box1从父盒子box左边-386px的位置,移动到右边386px的位置。
添加过渡,时间为0.5秒。 */
left: 386px;
transition: all .5s;
}
</style>
</head>
<body>
<div class="box">
Adele
<div class="box1">
</div>
</div>
</body>
</html>
另外,过渡如果加给子盒子box1的话,鼠标经过父盒子box时,会闪光一下,鼠标离开父盒子box时,又闪光一下。所以,如果想鼠标经过父盒子box时,只闪光一下,那么,过渡需要加到.box:hover .box1 { }里。