我该如何修复此脉冲动画?

时间:2019-02-23 13:41:57

标签: javascript html css

我正在项目中设置新的脉冲动画

但这是行不通的,我该如何解决?我的问题是什么?

我想在该圆上单击(仅单击一次而不按住鼠标的长按)时会跳动,并在2秒后停止

var  abox = document.getElementsByClassName("pulsediv")[0];
function pulsing(){

       abox.classList.toggle("pulse");
}
@import "compass/css3";

body, html {
  height: 100%;
  background: #fff;
}




.pulsediv{

  position: relative;
  top: 50%;
  left: 50%;
  margin-left: -50px;
  margin-top: -50px;
  display: block;
  width: 100px;
  height: 100px;
  font-size: 1.3em;
  font-weight: light;
  font-family: 'Trebuchet MS', sans-serif;
  text-transform: uppercase;
  text-align: center;
  line-height: 100px;
  letter-spacing: -1px;
  color: white;
  border: none;
  border-radius: 50%;
  background: #5a99d4;
  cursor: pointer;
  box-shadow: 0 0 0 0 rgba(#5a99d4, .5);
}
.pulse {

   animation: pulse 2s;

}

@-webkit-keyframes pulse {
  0% {
    @include transform(scale(.9));
  }
  70% {
    @include transform(scale(1));
    box-shadow: 0 0 0 50px rgba(#5a99d4, 0);
  }
    100% {
    @include transform(scale(.9));
    box-shadow: 0 0 0 0 rgba(#5a99d4, 0);
  }
  }
  <span class="pulsediv" onclick="pulsing">Hot</span>

1 个答案:

答案 0 :(得分:1)

我修复了动画,并用Javascript调用了onClick函数,

var abox = document.getElementsByClassName("pulsediv")[0];
// reset the transition by...
abox.addEventListener("click", function(e){
  e.preventDefault();
  // -> removing the class
  abox.classList.remove("pulse");
  // -> and re-adding the class
  setTimeout(() => abox.classList.add("pulse"), 0);
}, false);
body, html {
  height: 100%;
  background: #fff;
}

.pulsediv {
  position: relative;
  top: 50%;
  left: 50%;
  margin-left: -50px;
  margin-top: -50px;
  display: block;
  width: 100px;
  height: 100px;
  font-size: 1.3em;
  font-weight: light;
  font-family: 'Trebuchet MS', sans-serif;
  text-transform: uppercase;
  text-align: center;
  line-height: 100px;
  letter-spacing: -1px;
  color: white;
  border: none;
  border-radius: 50%;
  background: #5a99d4;
  cursor: pointer;
  box-shadow: 0 0 0 0 rgba(90, 153, 212, 0.5);
}

.pulse {
  animation: pulse 2s;
}

@-webkit-keyframes pulse {
  0% {
    transform: scale(0.9));
  }
  70% {
    transform: scale(1);
    box-shadow: 0 0 0 50px rgba(90, 153, 212, 0);
  }
  100% {
    transform: scale(0.9));
    box-shadow: 0 0 0 0 rgba(90, 153, 212, 0);
  }
}
<span class="pulsediv">Hot</span>