简体   繁体   English

滚动元素淡入淡出与关键帧无法正常工作

[英]Element fade on scroll with keyframe doesn't work correctly

I found this code: DEMO 我找到了这个代码: DEMO

When you scroll over the element, the element appears. 滚动元素时,将显示该元素。 But, the effect is only an opacity change. 但是,效果只是不透明度的变化。

I tried to add a keyframe animation when the element appears but, when the first element appears, all others elements appear at the same time. 我试图在元素出现时添加关键帧动画,但是当第一个元素出现时,所有其他元素同时出现。

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')  

            }

        }); 

    });

});

You just need to tell the each function which elements to add the animations to and remove the bit changing the opacity, the opacity change is already apart of the animation. 您只需要告诉每个函数添加动画的元素和删除改变不透明度的位,不透明度的变化已经是动画的一部分。

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');
                }
            }

        });

    });

});

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

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