Solomon Hodge
Solomon Hodge

Reputation: 11

How to make div fade in/out on scroll

Need some advice: I'm looking to create a fadeIn/fadeOut script that works responsively as the page scrolls. What I want to do:

Here's a sample code that creates the fadein, but only does it once. I need it to fade in and out each time it is triggered.

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

        }

    }); 

    });

    });

Any advice?

Thanks, and I hope that made sense!

Upvotes: 1

Views: 1279

Answers (1)

Mike Bartlett
Mike Bartlett

Reputation: 3093

Have you considered jQuery Waypoints? It's a plugin that allows you to execute functions whenever you scroll to an element.

http://imakewebthings.com/jquery-waypoints/

You can then execute a .fadeIn() on that element when you scroll to it.

Upvotes: 2

Related Questions