【问题标题】:Adding class via js won't trigger css animation通过js添加类不会触发css动画
【发布时间】:2014-04-01 08:03:47
【问题描述】:

我有 animate.css 的修改版本(添加了一些延迟、新的时间和新的位置),并且在默认设置类时效果非常好(html 文档)。但是当我通过js动态添加动画类时,动画没有执行!

更烦人的是,我确实在某个时候让它工作了,但我无法让它再次工作(使用 gumby 框架和 inview js 在元素出现在屏幕上时添加一个类(添加 .animated))。左框是html中已经存在的类,右框是js添加的.animate类。

示例:
http://onepageframework.com/28_2_14/strange_anim.html

任何想法为什么正确的盒子没有动画?

使用 Gumby inview 扩展程序:http://gumbyframework.com/docs/extensions/#!/inview

编辑:添加 html:

<div class="six columns text-center fadeInLeftMedium delay_2 animated">
    <!-- left box content here -->
</div>
<div class="six columns text-center fadeInLeftMedium delay_2 inview" data-classname="animated">
    <!-- right box content here -->
</div>

css:

.animated {
    -webkit-animation-duration: 1s;
       -moz-animation-duration: 1s;
         -o-animation-duration: 1s;
            animation-duration: 1s;
    -webkit-animation-fill-mode: both;
       -moz-animation-fill-mode: both;
         -o-animation-fill-mode: both;
            animation-fill-mode: both;
}

.delay_2 {
    -webkit-animation-delay: 2s;
       -moz-animation-delay: 2s;
         -o-animation-delay: 2s;
            animation-delay: 2s;    
}

@-webkit-keyframes fadeInLeftMedium {
    0% {
        opacity: 0;
        -webkit-transform: translateX(-400px);
    }

    100% {
        opacity: 1;
        -webkit-transform: translateX(0);
    }
}
@-moz-keyframes fadeInLeftMedium {
    0% {
        opacity: 0;
        -moz-transform: translateX(-400px);
    }

    100% {
        opacity: 1;
        -moz-transform: translateX(0);
    }
}
@-o-keyframes fadeInLeftMedium {
    0% {
        opacity: 0;
        -o-transform: translateX(-400px);
    }

    100% {
        opacity: 1;
        -o-transform: translateX(0);
    }
}
@keyframes fadeInLeftMedium {
    0% {
        opacity: 0;
        transform: translateX(-400px);
    }

    100% {
        opacity: 1;
        transform: translateX(0);
    }
}

.fadeInLeftMedium {
    -webkit-animation-name: fadeInLeftMedium;
    -moz-animation-name: fadeInLeftMedium;
    -o-animation-name: fadeInLeftMedium;
    animation-name: fadeInLeftMedium;
}

【问题讨论】:

  • 没有代码,这个问题“缺乏足够的信息来诊断问题”
  • 需要看看你是如何用 JS 和相关的 css 添加类的。
  • js 是通过 gumby inView 扩展添加的:gumbyframework.com/docs/extensions/#!/inview

标签: html css web css-animations gumby-framework


【解决方案1】:

原因与您不能通过随后删除和添加类来重新触发基于 CSS 的动画的原因相同。原因是浏览器会批量处理这些修改并优化动画。 原因及解决方法讨论here

从文章中,您的选择是(转述):

  1. 在重新添加课程之前添加一小段延迟(不推荐)
  2. 克隆元素,将其移除,然后插入克隆
  3. 有 2 个相同的动画(附加到不同的 css 规则)并在它们之间切换
  4. 在删除和添加类名之间触发重排:
        element.classList.remove("run-animation");
     // element.offsetWidth = element.offsetWidth; //doesn't work in strict mode
        void element.offsetWidth; // reading the property requires a recalc
        element.classList.add("run-animation");
  1. 将元素的 CSS animation-play-state 属性更改(循环)为 pausedrunning(不重新启动动画)

【讨论】:

  • 这里编辑总结文章内容。我个人认为 4 是最好的方法,现在我自己也在使用它。
【解决方案2】:

通过交换类,看起来它可以工作了(在类中进行分类,并以 fadeInLeftMedium 作为数据类名):

<div class="six columns text-center fadeInLeftMedium delay_2 animated">
    <!-- left box content here -->
</div>
<div class="six columns text-center animated delay_2 inview" data-classname="fadeInLeftMedium">
    <!-- right box content here -->
</div>

【讨论】:

    【解决方案3】:

    我认为您只需要将动画添加为一个类而不是 data-classname="animated"...

    所以基本上:

    <div class="six columns text-center fadeInLeftMedium delay_2 inview" data-classname="animated">
        <!-- right box content here -->
    </div>
    

    应该是:

    <div class="six columns text-center fadeInLeftMedium delay_2 inview animated">
        <!-- right box content here -->
    </div>
    

    否则动画缺少指定的动画持续时间,如果没有指定动画持续时间属性,动画将无法工作。

    【讨论】:

    • 整个想法是使用inview来添加类,这样当你向下滚动到那个元素时就会看到动画。左边的盒子和你设置的一样,它确实有动画。但如果元素位于页面下方,动画将在用户看到之前触发。
    • @code-zoop 我只是想说你需要更像$('your-element').addClass('animated, and whatever other classes); 这样的类作为类而不是数据添加。
    • 是的,我明白了。但是 inview 扩展从 data-classname 中获取数据并将其添加为一个类。这就是 inview 扩展的全部想法,它确实添加了应该的类,但动画没有执行:gumbyframework.com/docs/extensions/#!/inview
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-29
    • 1970-01-01
    • 2015-12-29
    • 2020-07-24
    • 1970-01-01
    • 2018-05-24
    • 2018-11-09
    相关资源
    最近更新 更多