简体   繁体   English

'transitionend'事件不会持续发射

[英]'transitionend' event does not fire consistently

The following code works inconsistently with Chrome but also Firefox (with 'transitionend'). 以下代码与Chrome和Firefox(与“transitionend”)不一致。 The function slogan_fade_next is just console.log('end'); 函数slogan_fade_next只是console.log('end'); . I always get the classname applied to the first span element but anything after that is hit-and-miss when I click the refresh button, reload, or anything else. 我总是将类名应用于第一个span元素,但是当我单击刷新按钮,重新加载或其他任何内容时,其后的任何内容都会被击中。

The class of slogan-fadein applied to slogan[] changes the opacity of the element from zero to one but the callback function fade_setup isn't called consistently. 应用于slogan[]slogan-fadein类将元素的不透明度从零更改为1,但回调函数fade_setup未被一致地调用。

function fade_setup(){
    var el = document.getElementsByClassName('slogan')[0];
    el = el.getElementsByTagName('span');
    for(var i=0;el[i];i++){
      el[i].addEventListener('webkitTransitionEnd',slogan_fade_next,false);
    }
    el[0].className='slogan-fadein';
  }

  document.addEventListener('DOMContentLoaded', fade_setup);

instead of 代替

    document.addEventListener('DOMContentLoaded', fade_setup);

can you use 你能用吗?

    document.addEventListener('load', fade_setup)

With your current implementation, the JavaScript may be running before the browser has finished applying styles and, therefore, before any transitions are defined. 使用当前的实现,JavaScript可能在浏览器完成应用样式之前运行,因此,在定义任何转换之前。

The problem is caused by a timing issue with applying the styles and anything else as mentioned by Stephen. 问题是由应用样式的时间问题和Stephen提到的任何其他问题引起的。 The problem is, things aren't settled by the time I try to fire the first fade in so I triggered that with window.onload=slogan_fade_next; 问题是,当我尝试触发第一次淡入时,事情没有解决,所以我用window.onload=slogan_fade_next;触发了它window.onload=slogan_fade_next; . Everything has settled in by the time my first element has done its thing. 当我的第一个元素完成它的时候,一切都已经解决了。

I've given absolutely no more thought to this other than "it works" and I'm sure I'll come up with a better way to do this. 除了“它的工作原理”之外我完全不再考虑这个了,我相信我会想出一个更好的方法来做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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