通过点击运行动画的纯CSS方法

时间:2015-05-01 15:54:12

标签: html css css3 animation css-animations

我尝试在纯CSS中重新创建材质设计涟漪效果,并准备好动画。问题是,当单击元素时,我无法使动画一直运行。我尝试过转换(attempt 1 demoattempt 2 demo),但这些都不会一直运行。

另一种更可能的方法是使用CSS3动画(我现在不担心浏览器支持)。我已准备好动画关键帧,但我之前从未使用动画,并且在点击时我没有看到运行动画的方法。



@-webkit-keyframes ripple {
  0% {
    background-size: 1% 1%;
    opacity: 0.5;
  }
  70% {
    background-size: 1000% 1000%;
    opacity: 0.2;
  }
  100% {
    opacity: 0;
    background-size: 1000% 1000%;
  }
}

@keyframes ripple {
  0% {
    background-size: 1% 1%;
    opacity: 0.5;
  }
  70% {
    background-size: 1000% 1000%;
    opacity: 0.2;
  }
  100% {
    opacity: 0;
    background-size: 1000% 1000%;
  }
}

.button {
  background-color: blue;
  padding: 12px;
  display: inline-block;
  border-radius: 5px;
  color: white;
  font-family: sans-serif;
  text-transform: uppercase;
  position: relative;
  overflow: hidden;
}

.button::after {
  position: absolute;
  content: " ";
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
  background-image: radial-gradient(circle at center, #FFF 0%, #FFF 10%, transparent 10.1%, transparent 100%);
  background-position: center center;
  background-repeat: no-repeat;
  background-color: transparent;
  -webkit-animation: ripple 0.6s 0s normal forwards infinite running ease-in;
  animation: ripple 0.6s 0s normal forwards infinite running ease-in;
}

.button:active::after {
  /*Somehow the animation needs to run on click only, and then run all the way through*/
}

<div class="ripple button"><a>Click this</a></div>
&#13;
&#13;
&#13;

我已经考虑但未能完成工作的事情包括更改动画延迟,使用::after使opacity透明,以及使用动画计时功能。

2 个答案:

答案 0 :(得分:1)

我已经采取了另一种方式: 使用:focus样式保持活动状态。你可以在这里查看: http://codepen.io/jonnitto/pen/OVmvPB?editors=110

答案 1 :(得分:0)

试试这个,但你需要使用jquery让按钮保持活动状态,我没有使用jquery因此按住点击;

@-webkit-keyframes ripple {
  0% {
    background-size: 1% 1%;
    opacity: 0.5;
  }
  70% {
    background-size: 1000% 1000%;
    opacity: 0.2;
  }
  100% {
    opacity: 0;
    background-size: 1000% 1000%;
  }
}

@keyframes ripple {
  0% {
    background-size: 1% 1%;
    opacity: 0.5;
  }
  70% {
    background-size: 1000% 1000%;
    opacity: 0.2;
  }
  100% {
    opacity: 0;
    background-size: 1000% 1000%;
  }
}

.button {
  background-color: blue;
  padding: 12px;
  display: inline-block;
  border-radius: 5px;
  color: white;
  font-family: sans-serif;
  text-transform: uppercase;
  position: relative;
  overflow: hidden;
}



.button:active:after{
  position: absolute;
  content: " ";
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  pointer-events: none;
  background-image: radial-gradient(circle at center, #FFF 0%, #FFF 10%, transparent 10.1%, transparent 100%);
  background-position: center center;
  background-repeat: no-repeat;
  background-color: transparent;
  -webkit-animation: ripple 0.6s 0s normal forwards infinite running ease-in;
  animation: ripple 0.6s 0s normal forwards infinite running ease-in;}
<div class='ripple button'><a href=''>hell</a></div>