Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fade in element on scroll down using css

Tags:

jquery

css

Example: http://www.chartjs.org/

When scrolling down each article shows up when it gets in the view-port of the browser. The css is

#examples article {
    -webkit-transition: opacity 200ms ease-in-out;
    -ms-transition: opacity 200ms ease-in-out;
    -moz-transition: opacity 200ms ease-in-out;
    -o-transition: opacity 200ms ease-in-out;
    transition: opacity 200ms ease-in-out;
    position: relative;
    margin-top: 20px;
    clear: both;
}

I tried this css but it does not work. Is there some javascript that works together with the css? How can I achieve this kind of scroll->fadeIn effect?

like image 650
UpCat Avatar asked Mar 23 '13 18:03

UpCat


1 Answers

Mohammeds answer does not take into account any absolutely positioned images... we should use offset instead of position

$(window).scroll(function () {

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

        var bottom_of_object = $(this).offset().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).animate({
                'opacity': '1'
            }, 500);

         }
     });
});
like image 175
WiseStrawberry Avatar answered Sep 29 '22 18:09

WiseStrawberry