CSS:悬停时关键帧动画消失

标签 css css-animations keyframe

悬停时,我的 div 的边框上有一个动画。

body {
  padding: 50px;
  background-color: #000;
  text-align: center;
}

div {
  width: 50px;
  height: 50px;
  border: 1px solid rgba(255,255,255,0.1);
  display: inline-block;
  margin: 0 10px;
}

div:hover {
   animation: blink 750ms forwards;
}


@keyframes blink {
  0% {
    border-left-color: rgba(255,255,255,0.1);
    border-right-color: rgba(255,255,255,0.1);
  }

  33% {
    border-left-color: rgba(255,255,255,1);
    border-right-color: rgba(255,255,255,1);
  }

  66% {
    border-left-color: rgba(255,255,255,0.1);
    border-right-color: rgba(255,255,255,0.1);
  }

  100% {
    border-left-color: rgba(255,255,255,1);
    border-right-color: rgba(255,255,255,1);
  }
}
<div></div>
<div></div>
<div></div>

动画按预期工作正常,但当我将鼠标悬停在 div 上时,动画突然停止。即使光标不在,我也想保持动画完整。

我尝试在 div 上添加 transition: border-color 300ms; 但它的行为仍然相同。

最佳答案

你可以试试这个:

body {
  padding: 50px;
  background-color: #000;
  text-align: center;
}

div {
  width: 50px;
  height: 50px;
  border: 1px solid rgba(255,255,255,0.1);
  display: inline-block;
  margin: 0 10px;
  transition:1000s; /*block the change*/
}

div:hover {
   animation: blink 750ms; /*remove forwards*/
   /*Add the last state of the animation*/
   border-left: 1px solid rgba(255,255,255,1);
   border-right: 1px solid rgba(255,255,255,1);
   /*---*/
   transition:.5s; /*make the change fast on hover*/
}


@keyframes blink {
  0% {
    border-left-color: rgba(255,255,255,0.1);
    border-right-color: rgba(255,255,255,0.1);
  }

  33% {
    border-left-color: rgba(255,255,255,1);
    border-right-color: rgba(255,255,255,1);
  }

  66% {
    border-left-color: rgba(255,255,255,0.1);
    border-right-color: rgba(255,255,255,0.1);
  }

  100% {
    border-left-color: rgba(255,255,255,1);
    border-right-color: rgba(255,255,255,1);
  }
}
<div></div>
<div></div>
<div></div>

更新

为确保动画结束,您需要保持悬停状态。一个想法是考虑一个将覆盖所有屏幕的伪元素,以确保您将悬停直到结束:

body {
  padding: 50px;
  background-color: #000;
  text-align: center;
}

div.box {
  width: 50px;
  height: 50px;
  border: 1px solid rgba(255,255,255,0.1);
  display: inline-block;
  margin: 0 10px;
  transition:1000s; /*block the change*/
  position:relative;
}

div.box:hover {
   animation: blink 750ms; /*remove forwards*/
   /*Add the last state of the animation*/
   border-left: 1px solid rgba(255,255,255,1);
   border-right: 1px solid rgba(255,255,255,1);
   /*---*/
   transition:.5s; /*make the change fast on hover*/
}
div.box:hover:before {
  content:"";
  position:fixed;
  top:0;
  left:0;
  right:0;
  bottom:0;
  z-index:1;
  animation:disappear 0.1s 0.75s forwards;
}

@keyframes disappear {
  to {bottom:100%;}
}

@keyframes blink {
  0% {
    border-left-color: rgba(255,255,255,0.1);
    border-right-color: rgba(255,255,255,0.1);
  }

  33% {
    border-left-color: rgba(255,255,255,1);
    border-right-color: rgba(255,255,255,1);
  }

  66% {
    border-left-color: rgba(255,255,255,0.1);
    border-right-color: rgba(255,255,255,0.1);
  }

  100% {
    border-left-color: rgba(255,255,255,1);
    border-right-color: rgba(255,255,255,1);
  }
}
<div class="box"></div>
<div class="box"></div>
<div class="box"></div>

关于CSS:悬停时关键帧动画消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51652852/

相关文章:

html - CSS3 动画显示在 2 个属性上,而只选择了 1 个

html - 在 CSS 中平滑地围绕对象旋转

javascript - 无法访问外部 css 或 javascript 文件

javascript - HTML/CSS 广告定位

html - 如何防止元素在html中重叠

php - 取SQL段落和空行(PHP、CSS、$MySQL)

css - 如何在页面完全加载后开始动画?

css - CSS3 旋转是否开始缓慢然后结束缓慢?

html - 条纹 table 上的背景颜色褪色

CSS 关键帧动画在本地工作,但不在我的网站上