Fade In on Scroll Down, Fade Out on Scroll Up - based on element position in window

前端 未结 4 1040
抹茶落季
抹茶落季 2020-11-28 04:13

I\'m trying to get a series of elements to fade in on scroll down when they are fully visible in the window. If I keep scrolling down, I do not want them to fade out, but if

4条回答
  •  鱼传尺愫
    2020-11-28 04:58

    Sorry this is and old thread but some people would still need this I guess,

    Note: I achieved this using Animate.css library for animating the fade.

    I used your code and just added .hidden class (using bootstrap's hidden class) but you can still just define .hidden { opacity: 0; }

    $(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).removeClass('hidden');
            $(this).addClass('animated fadeInUp');
        }    else {
                $(this).addClass('hidden');
                   }
    
    }); 
    }); 
    });
    

    Another Note: Applying this to containers might cause it to be glitchy.

提交回复
热议问题