控制CSS动画计数表单输入

时间:2018-04-04 16:37:06

标签: javascript html css

我一直试图控制每分钟的滴速。是否可以根据Drops in each minute:输入控制下降速率?就像输入值为60时一样,每分钟会有60个水滴(animation: drop 1s)。实现这一目标的最佳方法是什么? 提前感谢您的建议。

代码的小提琴链接是here

body {
  background: #e8e5ea;
}

.wrap {
  position: absolute;
  width: 100px;
  height: 200px;
  left: 50%;
  margin-left: -50px;
  top: 50%;
  margin-top: -100px;
}

.drop {
  width: 40px;
  height: 40px;
  left: 50%;
  margin-left: -20px;
  position: absolute;
  animation: drop 2s cubic-bezier(0.55, 0.085, 0.68, 0.53) 0s infinite;
}

.drop circle {
  fill: #2a96ed;
}

.drop-outer {
  position: absolute;
  box-sizing: border-box;
  /* border: 1px solid #333; */
  width: 100px;
  height: 200px;
  overflow: hidden;
  border-bottom-right-radius: 50px;
  border-bottom-left-radius: 50px;
  backface-visibility: hidden;
  transform: translate3d(0, 0, 0);
  background-clip: padding-box;
  -webkit-mask-image: -webkit-radial-gradient(circle, white 100%, black 100%);
}

.ripple {
  position: absolute;
  box-sizing: border-box;
  width: 240px;
  height: 240px;
  top: 68px;
  left: -70px;
  perspective: 100;
  transform: rotateX(65deg);
}

.ripple .ripple-svg {
  position: absolute;
  width: 240px;
  height: 240px;
  opacity: 0;
}

.ripple .ripple-svg circle {
   fill: none;
   stroke: #2a96ed;
   stroke-width: 10px;
   stroke-alignment: inner;
}

.ripple-1 {
  animation: ripple 2s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0s infinite;
}

.ripple-1 .ripple-svg {
  animation: fade-in-out 2s linear 0s infinite;
}

.ripple-1 .ripple-svg circle {
  animation: border 2s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0s infinite;
}

.ripple-2 {
  animation: ripple 2s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.2s infinite;
}

.ripple-2 .ripple-svg {
  animation: fade-in-out 2s linear 0.2s infinite;
}

.ripple-2 .ripple-svg circle {
  animation: border 2s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.2s infinite;
}

.ripple-3 {
  animation: ripple 2s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.35s infinite;
}

.ripple-3 .ripple-svg {
  animation: fade-in-out 2s linear 0.35s infinite;
}

.ripple-3 .ripple-svg circle {
  animation: border 2s cubic-bezier(0.25, 0.46, 0.45, 0.94) 0.35s infinite;
}



@keyframes drop {
  0% {
    transform: scale3d(0.01,0.01,0.01) translateY(0)
  }
  10% {
    transform: scale3d(1,1,1) 
  }
  44% {
    transform: scale3d(1,1,1) translateY(200px)
  }
  100% {
    transform: scale3d(1,1,1) translateY(200px)
  }
}

@keyframes fade-in-out {
  0% {opacity: 0}
  42% {opacity: 0}
  52% {opacity: 1}
  65% {opacity: 1}
  100% {opacity: 0}
}

@keyframes ripple {
  0% { transform: rotateX(65deg) scale3d(0.2, 0.2, 0.2) }
  42% { transform: rotateX(65deg) scale3d(0.2, 0.2, 0.2) }
  100% { transform: rotateX(65deg) scale3d(0.9, 0.9, 0.9) }
}

@keyframes border {
  0% { stroke-width: 6px }
  42% { stroke-width: 6px }
  100% {stroke-width: 2px }
}
Drops in each minute: <input type="number" id="number"/><br>

<div class="wrap">
  <div class="drop-outer">
    <svg class="drop" viewBox="0 0 40 40" version="1.1"
    xmlns="http://www.w3.org/2000/svg">
    <circle cx="20" cy="20" r="20"/>
    </svg>
  </div>
  <div class="ripple ripple-1">
      <svg class="ripple-svg" viewBox="0 0 60 60" version="1.1"
    xmlns="http://www.w3.org/2000/svg">
    <circle cx="30" cy="30" r="24"/>
      </svg>
  </div>
  <div class="ripple ripple-2">
      <svg class="ripple-svg" viewBox="0 0 60 60" version="1.1"
    xmlns="http://www.w3.org/2000/svg">
    <circle cx="30" cy="30" r="24"/>
      </svg>
  </div>
  <div class="ripple ripple-3">
      <svg class="ripple-svg" viewBox="0 0 60 60" version="1.1"
    xmlns="http://www.w3.org/2000/svg">
    <circle cx="30" cy="30" r="24"/>
      </svg>
  </div>
</div>

2 个答案:

答案 0 :(得分:1)

仅当您通过JavaScript指定相关样式时。

你的例子相当复杂而且不是this,所以这里有一个简化的例子来说明这个想法:

&#13;
&#13;
const animateInner = document.querySelector('#animate .inner');
const input = document.querySelector('input');

document.querySelector('button').addEventListener('click', function() {
  console.log('update to', `${input.value || 0}s`);
  animateInner.style.animationDuration = `${input.value || 0}s`;
});
&#13;
#animate {
  position: relative;
  width: 200px;
  height: 200px;
  border: 1px solid #000;
}

#animate .inner {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: #F00;
  animation-name: fill;
  animation-iteration-count: infinite;
}

@keyframes fill {
  from {
    top: 100%;
  }

  to {
    top: 0;
  }
}
&#13;
<label>Duration in seconds: <input /></label>
<button>Update</button>
<div id="animate"><div class="inner"></div></div>
&#13;
&#13;
&#13;

简而言之,您将要控制动画持续时间&#34;属性。这将影响它发生的速度。既然你想要每分钟掉落#34;你就必须做一些数学运算才能把它转换为持续时间。

由于您的动画看起来有几件事情齐声协力,因此您希望同时更新它们。

答案 1 :(得分:0)

所以你可以使用

  

animation-iteration-count CSS3属性,秘诀是你得到号码并更新css

$(function(){
    $('#number').on('change', function(){
    var count = $(this).val();    
    $('.drop').css('animation-iteration-count', count);
    $('.ripple-1').css('animation-iteration-count', count);
    $('.ripple-1 .ripple-svg').css('animation-iteration-count', count);
    $('.ripple-1 .ripple-svg circle').css('animation-iteration-count', count);    
    $('.ripple-2').css('animation-iteration-count', count);
    $('.ripple-2 .ripple-svg').css('animation-iteration-count', count);
    $('.ripple-2 .ripple-svg circle').css('animation-iteration-count', count); 
    $('.ripple-3').css('animation-iteration-count', count);
    $('.ripple-3 .ripple-svg').css('animation-iteration-count', count);
    $('.ripple-3 .ripple-svg circle').css('animation-iteration-count', count);     
  });
});

JSfiddle

相关问题