最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - anime js onclick animate a selector - Stack Overflow

programmeradmin1浏览0评论

I have this effect of anime js

$(function(){

    var bouncingBall = anime({
      targets: '.box',
      translateY: '50vh',
      duration: 300,
      loop: 4,
      direction: 'alternate',
      easing: 'easeInCubic'
    });

});

How can I run the animation only when I'm clicking on a button,
so let's say I have in my HTML:

<div class="box">Button</div>

I have this effect of anime js

$(function(){

    var bouncingBall = anime({
      targets: '.box',
      translateY: '50vh',
      duration: 300,
      loop: 4,
      direction: 'alternate',
      easing: 'easeInCubic'
    });

});

How can I run the animation only when I'm clicking on a button,
so let's say I have in my HTML:

<div class="box">Button</div>
Share Improve this question asked Oct 18, 2017 at 20:50 Nelson DenverNelson Denver 1151 gold badge3 silver badges12 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

OK, I got it, In the javascript I should have had this line

document.querySelector('.box').onclick = bouncingBall.play;

so when I click on the class box it will animate the bouncingBall

Your js code should look like this:

$(function(){
    var bouncingBall = anime({
      targets: '.box',
      translateY: '50vh',
      duration: 300,
      loop: 4,
      direction: 'alternate',
      easing: 'easeInCubic',
      autoplay: false //if you don't want play animation on page load
    });

    document.querySelector('.box').onclick = bouncingBall //onclick event
});

Documentation: http://animejs./documentation/#playPause

发布评论

评论列表(0)

  1. 暂无评论