javascript - 带有关键帧的滚动条上的元素淡入淡出无法正常工作

标签 javascript jquery css css-animations

我找到了这段代码:DEMO

当您在元素上滚动时,该元素会出现。但是,效果只是不透明度的变化。

我尝试在元素出现时添加关键帧动画,但是当第一个元素出现时,所有其他元素同时出现。

DEMO with Keyframe

$(document).ready(function() {

    /* Every time the window is scrolled ... */
    $(window).scroll( function(){

        /* Check the location of each desired element */
        $('.hideme').each( function(i){

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* If the object is completely visible in the window, fade it it */
            if( bottom_of_window > bottom_of_object ){

              $(this).css({'opacity':'1'});
              $('.e1').addClass('animated fadeInUp')
              $('.e2').addClass('animated fadeInLeft')  

            }

        }); 

    });

});

最佳答案

您只需要告诉每个函数要向哪些元素添加动画并删除改变不透明度的位,不透明度的改变已经是动画的一部分。

Working Example

$(document).ready(function () {

    /* Every time the window is scrolled ... */
    $(window).scroll(function () {

        /* Check the location of each desired element */
        $('.hideme').each(function (i) {

            var bottom_of_object = $(this).position().top + $(this).outerHeight();
            var bottom_of_window = $(window).scrollTop() + $(window).height();

            /* If the object is completely visible in the window, fade it it */
// Changes made here 
            if (bottom_of_window > bottom_of_object) {
                if ($(this).hasClass('e1')) {
                    $(this).addClass('animated fadeInUp');
                }
                if ($(this).hasClass('e2')) {
                    $(this).addClass('animated fadeInLeft');
                }
            }

        });

    });

});

关于javascript - 带有关键帧的滚动条上的元素淡入淡出无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21463942/

相关文章:

javascript - node.js 无法从 mongoexport 读取 json

javascript - 如何在 Javascript 函数构造函数中将 DOM 元素 ID 分配给属性

HTML 默认图像大小

javascript - 我的滑出菜单可以在 HTML 中运行,但不能在 PHP 中运行

php - 有什么方法可以在 HTML TABLE 中使用 Rowspan 在一行中显示两个数据?

javascript - js动画功能滚动到div的底部而不是顶部

javascript - 如何用 Angular 显示图像

javascript - "Barba is not defined"错误

javascript - 将类添加到特定图像的父 div

javascript - 如何使用 JS/jQuery 处理两个按钮单击(同时向左+向右)?