簡體   English   中英

滾動元素淡入淡出與關鍵幀無法正常工作

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

我找到了這個代碼: DEMO

滾動元素時,將顯示該元素。 但是,效果只是不透明度的變化。

我試圖在元素出現時添加關鍵幀動畫,但是當第一個元素出現時,所有其他元素同時出現。

與關鍵幀的演示

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

            }

        }); 

    });

});

您只需要告訴每個函數添加動畫的元素和刪除改變不透明度的位,不透明度的變化已經是動畫的一部分。

工作實例

$(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