用户每次单击元素时执行效果

时间:2019-05-02 11:01:15

标签: jquery html css css3

每次用户使用jquery单击元素时,如何执行此效果?

我在单击时添加了一个涟漪图类,但是当我第二次单击该元素时,它无法执行,因为已经添加了该类,如何解决此问题?

<div class="circle-ripple"></div>
html, body {
  width: 100%;
  height: 100%;
}

body {
  background-color: #4e4e4e;
  display: flex;
  align-items: center;
  justify-content: center;

}

.circle-ripple {
  background-color: #35ffc3;
  width: 1em;
  height: 1em;
  border-radius: 50%;
}
.ripple {
    -webkit-animation: ripple 0.7s linear;
          animation: ripple 0.7s linear;
  animation-duration:0.5s;

}
@-webkit-keyframes ripple {
  0% {
    box-shadow: 0 0 0 0 rgba(101, 255, 120, 0.3), 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3);
  }
  100% {
    box-shadow: 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3), 0 0 0 8em rgba(101, 255, 120, 0);
  }
}

@keyframes ripple {
  0% {
    box-shadow: 0 0 0 0 rgba(101, 255, 120, 0.3), 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3);
  }
  100% {
    box-shadow: 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3), 0 0 0 8em rgba(101, 255, 120, 0);
  }
}
$(document).ready(function(){
  $(".circle-ripple").click(function(){
    $(this).addClass("ripple");
  });
});

3 个答案:

答案 0 :(得分:1)

我猜

setTimeout是一个更好的选择

$(document).ready(function(){
  $(".circle-ripple").click(function(){
    var data = $(this)
    data.addClass("ripple");
     setTimeout(function() {
        data.removeClass('ripple');
    }, 500)
    
  });
});
html, body {
  width: 100%;
  height: 100%;
}

body {
  background-color: #4e4e4e;
  display: flex;
  align-items: center;
  justify-content: center;

}

.circle-ripple {
  background-color: #35ffc3;
  width: 1em;
  height: 1em;
  border-radius: 50%;
}
.ripple {
    -webkit-animation: ripple 0.7s linear;
          animation: ripple 0.7s linear;
  animation-duration:0.5s;

}
@-webkit-keyframes ripple {
  0% {
    box-shadow: 0 0 0 0 rgba(101, 255, 120, 0.3), 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3);
  }
  100% {
    box-shadow: 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3), 0 0 0 8em rgba(101, 255, 120, 0);
  }
}

@keyframes ripple {
  0% {
    box-shadow: 0 0 0 0 rgba(101, 255, 120, 0.3), 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3);
  }
  100% {
    box-shadow: 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3), 0 0 0 8em rgba(101, 255, 120, 0);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.0/jquery.min.js"></script>
<div class="circle-ripple"></div>

答案 1 :(得分:1)

如果要避免超时,可以改为像这样重新插入元素(我只更改了JavaScript):

$(document).ready(function(){
  $(".circle-ripple").click(function(){
      // reinsert div with ripple class
      const new_element = $(this).clone(true);
      new_element.addClass("ripple");
      $(this).before(new_element);
      $(this).remove();
  });
});
html, body {
  width: 100%;
  height: 100%;
}

body {
  background-color: #4e4e4e;
  display: flex;
  align-items: center;
  justify-content: center;

}

.circle-ripple {
  background-color: #35ffc3;
  width: 1em;
  height: 1em;
  border-radius: 50%;
}
.ripple {
    -webkit-animation: ripple 0.7s linear;
          animation: ripple 0.7s linear;
  animation-duration:0.5s;

}
@-webkit-keyframes ripple {
  0% {
    box-shadow: 0 0 0 0 rgba(101, 255, 120, 0.3), 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3);
  }
  100% {
    box-shadow: 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3), 0 0 0 8em rgba(101, 255, 120, 0);
  }
}

@keyframes ripple {
  0% {
    box-shadow: 0 0 0 0 rgba(101, 255, 120, 0.3), 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3);
  }
  100% {
    box-shadow: 0 0 0 1em rgba(101, 255, 120, 0.3), 0 0 0 3em rgba(101, 255, 120, 0.3), 0 0 0 5em rgba(101, 255, 120, 0.3), 0 0 0 8em rgba(101, 255, 120, 0);
  }
}
<div class="circle-ripple"></div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

答案 2 :(得分:0)

我认为您应该在某个超时后删除该类。

$(document).ready(function(){
  $(".circle-ripple").click(function(){
    var selector = $(this);
    selector.addClass("ripple");
    setTimeout(function() { 
      selector.removeClass("ripple");
      }, 2000);
  });
});

根据需要设置超时值。希望对您有所帮助。

答案是在纳赫曼发表评论后编辑的。