简体   繁体   中英

animationEnd event handler - event heard two times

I have a code:

$('#sectionName').on('animationend webkitAnimationEnd oAnimationEnd', function () {
    alert("Hello");
});

which is meant to trigger the alert when animation of #sectionName ends.

It does its job but it is also triggered first time the website is launched.

Is there a way to prevent it? Some way to trigger this alert only when the animationend event is heard for the second time?

PS I am using wow.js, so the #sectionName object slides in, after scrolling to certain page point, and then I want the alert to be displayed.

Try it simple:

Var firstTime = true
$('#sectionName').on('animationend webkitAnimationEnd oAnimationEnd', function () {
    if(firstTime){
        firstTime = false;
        return;
    }
    alert("Hello");
});

You can use wow.js callback to catch an animation is started:

var sections = ['sectionName', 'sectionName2']
var wow = new WOW({
  callback: function(box) {
    var $box = $(box);
    if ( sections.indexOf( $box.attr('id') ) >= 0 ) {
      $box.on('animationend webkitAnimationEnd oAnimationEnd', function () {
          alert("Hello");
      });    
    }
  }
}).init();

[ https://jsfiddle.net/6g01kLsh/ ]

Only tested for animationend and webkitAnimationEnd .

var isWebkitAnim = window.onanimationend === undefined && window.onwebkitanimationend !== undefined
var animationEndEvent = isWebkitAnim ? 'webkitAnimationEnd' : 'animationend'

$('#sectionName').on(animationEndEvent, function() {
    alert("Hello")
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM