.box {
position: relative;
width: 300px;
height: 200px;
background-color: aqua;
transition: all 1s;
}
.box2 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(rgb(255,255,255),rgb(255,255,255));
transition: all 1s;
}
.box:hover .box2 {
background-image: linear-gradient(red, yellow);
}
<div class="box">
<div class="box2"></div>
</div>
运行上面代码可以看到transition是不生效的,这时可以利用opacity来过渡.
.box2 {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-image: linear-gradient(red, yellow);
transition: all 1s;
opacity: 0;
}
.box:hover .box2 {
opacity: 1;
}
这样就不会很突兀了