将鼠标悬停在过渡 css 上

tro*_*mar 1 css animation transition hover

这个问题可能很明显,但我是 css 新手。

我正在动画一个形状,所以当你悬停它时,它会伸展。我已经通过一个很好的轻松过渡完成了悬停,但是当您离开鼠标时,过渡不起作用。有没有办法让它在悬停时刻也发生?

.shape1{
position: absolute;
background:red
top:512px;
width:180px;
height:140px;
}

.shape1:hover {
height: 160px;
top:492px;
transition: 0.2s ease;
}
Run Code Online (Sandbox Code Playgroud)

Ger*_*ann 6

你的答案

您已transition在元素的悬停状态上添加了该属性。因此,transition当您离开cursor元素时 不会应用。

.shape1{
    position: absolute;
    background: red;
    top: 512px;
    width: 180px;
    height: 140px;
    transition: .2s ease; /* move this here from :hover */
}
Run Code Online (Sandbox Code Playgroud)

更多信息

除此之外,您还可以将特定属性添加到transition. 例如,如果您只想对高度进行动画处理,您可以像这样:

.shape1 {
    transition: height .2s ease;
    /* this inly affects height, nothing else */
}
Run Code Online (Sandbox Code Playgroud)

您甚至可以为每个属性定义不同的转换时间:

.shape1 {
    transition: height .2s ease, background-color .5s linear;
    /* stacking transitions is easy */
}
Run Code Online (Sandbox Code Playgroud)