当 div 上的转换结束并且所有 CHILD div 上的转换结束时,会触发 webkitTransitionEnd 事件吗?

发布于 2024-11-27 06:21:37 字数 914 浏览 2 评论 0原文

总之,

我遇到的情况大致如下:

我的 HTML 页面 a 包含 div(我将其称为 “parentDiv”),我正在其上执行转换。当该转换结束时,它应该调用 "onTransitionEndParent"

parentDiv 包含一个 div(我将其称为 "childDiv"),我正在对它执行不同的操作 过渡。当该转换结束时,它应该调用“onTransitionEndChild”

所以,我的代码大致如下:

parentDiv.addEventListener("webkitTransitionEnd", onTransitionEndParent, false);
childDiv.addEventListener("webkitTransitionEnd", onTransitionEndChild, false);

我发现的问题...

onTransitionEndParent 在parentDiv 的转换结束时被调用(正确)。然而,它ALSO在childDiv的转换结束时调用(不是我所期望的...)

换句话说...

  • onTransitionEndChild在childDiv的转换结束时调用
  • onTransitionEndParent在parentDiv的转换结束时调用 当 childDiv 的转换结束时再次

这是正确的行为,还是我做错了什么?

有没有办法确保 onTransitionEndParent 仅在 ParentDiv 的转换结束时调用,而不是在其任何子 div 的转换结束时调用?

非常感谢。

All,

I have a situation that looks roughly like this:

My HTML page a contains div (which I'll call "parentDiv") on which I'm performing a transition. When that transition ends, it should call "onTransitionEndParent"

parentDiv contains a div (which I'll call "childDiv") on which I'm performing a different transition. When that transition ends, it should call "onTransitionEndChild".

So, my code looks roughly like this:

parentDiv.addEventListener("webkitTransitionEnd", onTransitionEndParent, false);
childDiv.addEventListener("webkitTransitionEnd", onTransitionEndChild, false);

The problem I'm finding...

onTransitionEndParent is called when the parentDiv's transition ends (correct). However, it's ALSO called when childDiv's transition ends (not what I expected...)

In other words...

  • onTransitionEndChild is called when childDiv's transition ends
  • onTransitionEndParent is called when parentDiv's transition ends
    AND AGAIN when childDiv's transition ends

Is this the correct behavior, or am I doing something wrong?

Is there a way to make sure that onTransitionEndParent is ONLY called when the parentDiv's transition ends, and NOT when any of it's child div's transitions end?

Many thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬﹪忆之殇 2024-12-04 06:21:37

transitionEnd 是所谓的冒泡事件,它从子级分派(冒泡)到其父级。

您的选择:

  1. 要么分析事件对象的 event.target 属性 - 它
    应包含已结束转换的元素。
  2. 或者在每个子元素上安装transitionEnd事件处理程序并调用
    event.stopPropagation() 以防止其冒泡。

transitionEnd is so called bubbling event that is being dispatched (bubbles up) from child to its parents.

Options for you:

  1. Either analyze event.target property of the event object - it
    should contain element with ended transition.
  2. Or to install transitionEnd event handlers on each child element and call
    event.stopPropagation() so to prevent its bubbling.
老街孤人 2024-12-04 06:21:37

要添加到 @c-smiles 答案,当元素上有多个转换时,检查 event.propertyName 会有所帮助。所以,举例来说——

button.addEventListener('transitionend', function(event) {
  if (event.target.classList.contains('button') && event.propertyName === 'opacity') {
    button.classList.add('hide');
  }
}, false);

To add to @c-smiles answer, checking for event.propertyName helps when there are multiple transitions on the element. So, for example -

button.addEventListener('transitionend', function(event) {
  if (event.target.classList.contains('button') && event.propertyName === 'opacity') {
    button.classList.add('hide');
  }
}, false);
花桑 2024-12-04 06:21:37

上次 chrome 更新后我遇到了同样的问题。子动画触发了父动画的动画结束。这种情况仅发生在桌面版 Chrome 中,而不是 FF 或移动版 Chrome 中。

对于任何感兴趣的人,我通过使用以下方法解决了这个问题:

        $(element).on("animationend webkitAnimationEnd", function(){
            //do something
        })
        .children().on("animationend webkitAnimationEnd", function(){
             return false;   //you can just use event.stopPropagation()
         });

I encountered the same problem after the last chrome update. Children animations triggered animationend for the parent. This only happened in Chrome for desktop and not in FF or Chrome for mobile.

For anyone interested I solved this by using:

        $(element).on("animationend webkitAnimationEnd", function(){
            //do something
        })
        .children().on("animationend webkitAnimationEnd", function(){
             return false;   //you can just use event.stopPropagation()
         });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文