CSS动画一开始并未隐藏

时间:2017-12-06 19:29:57

标签: html css animation

我想要一个div,里面有一个字。当div悬停在上面时,这个单词应该带有动画。这是动画和表现我想要的方式,除了...页面加载时“hello”这个词是可见的。我希望它被隐藏,然后动画进入视图。我需要修理什么?

#test{
  border: 1px solid #000;
}
#hello {
    animation-name: hello;
    animation-duration: 0.5s;
    animation-fill-mode: backwards;
    animation-play-state: paused;
}
#hello:hover {
   animation-fill-mode: forwards;
    animation-play-state: running;
    visibility: visible;
}

@keyframes hello {
    0%      {visibility:(hidden);transform:scale(0.5); opacity:(0.0);}
    1%      {visibility:(visible);}
    50%     {transform:scale(1.2); opacity:(0.5);}
    100%    {transform:scale(1.0); opacity:(1.0);}
}
<div id="test">
  <div id="hello">
    Hello!
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

您只需从属性值中删除(),因为它会使其无效。此外,最好在1%的动画中更改不透明度,以便能够看到缩放效果。

您也可以从CSS中删除visiblity属性,因为您使用的是不透明度

#test {
  border: 1px solid #000;
  background:#fff;
  overflow:hidden; /* added this to avoid the scroll to appear and disappear*/
}

#hello {
  animation-name: hello;
  animation-duration: 0.5s;
  animation-fill-mode: backwards;
  animation-play-state: paused;
}

#test:hover #hello {
  animation-fill-mode: forwards;
  animation-play-state: running;
  /*visibility: visible; can be removed*/
} 

@keyframes hello {
  0% {
    /*visibility: hidden; can be removed*/
    transform: scale(0.5);
    opacity: 0;
  }
  1% {
    /*visibility: visible; can be removed*/
    opacity:0.3;
  }
  50% {
    transform: scale(1.2);
    opacity:0.5;
  }
  100% {
    transform: scale(1.0);
    opacity:1.0;
  }
}
<div id="test">
  <div id="hello">
    Hello!
  </div>
</div>