Css元素悬停开始白色动画,但意外结束

时间:2017-12-20 20:01:00

标签: css css3 animation dom

当我悬停元素时动画开始但是当我不聚焦它时,动画不会反转,一切都会恢复动画开始前的样子。 我想让它从动画开始,以动画结束。



div#time{
background-color:#ecf0f1;
display:inline-block;
font-size:calc(3vh + 3vw);
border:5px solid #ecf0f1;
border-radius:0.7em;
top:2vh;
position:fixed;
width:30vw;
opacity:0.8;
 left: 0;
  right: 0;
  margin: 0 auto;
}
div#time:hover{
transition:1s;
border-radius:0;
background-color:#141414;
border:5px solid #141414;
color:white;

}

<center><div id='time'>hiiiiiii</div></center>
&#13;
&#13;
&#13;

https://codepen.io/abooo/pen/LeZOWR?editors=1100

3 个答案:

答案 0 :(得分:4)

您将过渡属性放在错误的位置。

&#13;
&#13;
div#time {
  background-color: #ecf0f1;
  display: inline-block;
  font-size: calc(3vh + 3vw);
  border: 5px solid #ecf0f1;
  border-radius: 0.7em;
  top: 2vh;
  position: fixed;
  width: 30vw;
  opacity: 0.8;
  left: 0;
  right: 0;
  margin: 0 auto;
  transition: 1s;
}

div#time:hover {
  border-radius: 0;
  background-color: #141414;
  border: 5px solid #141414;
  color: white;
}
&#13;
<center>
  <div id='time'>hiiiiiii</div>
</center>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

只需将transition: 1s;移至主选择器,而不是:将鼠标悬停在

&#13;
&#13;
    div#time{
      background-color:#ecf0f1;
      display:inline-block;
      font-size:calc(3vh + 3vw);
      border:5px solid #ecf0f1;
      border-radius:0.7em;
      top:2vh;
      position:fixed;
      width:30vw;
      opacity:0.8;
      left: 0;
      right: 0;
      margin: 0 auto;
  transition:1s;
    }
    div#time:hover{
      border-radius:0;
      background-color:#141414;
      border:5px solid #141414;
      color:white;
    }
&#13;
<center><div id='time'>hiiiiiii</div></center>
&#13;
&#13;
&#13;

答案 2 :(得分:1)

如果将转换移动到实际div,则它适用于两者。

&#13;
&#13;
div#time{
background-color:#ecf0f1;
display:inline-block;
font-size:calc(3vh + 3vw);
border:5px solid #ecf0f1;
border-radius:0.7em;
top:2vh;
position:fixed;
width:30vw;
opacity:0.8;
 left: 0;
  right: 0;
  margin: 0 auto;
  transition:1s;
}
div#time:hover{

border-radius:0;
background-color:#141414;
border:5px solid #141414;
color:white;

}
&#13;
<center><div id='time'>hiiiiiii</div></center>
&#13;
&#13;
&#13;