Skip to content Skip to sidebar Skip to footer

Change Div Color After Scrolling 15% Down With Jquery

I have this jquery code below which makes a div scroll. But I would also like it to change the div color to #32200F when is down 15% from the top.THen return to the original barckg

Solution 1:

Try:

$(document).ready(function () {
   var $scrollingDiv = $("#navbar");

   $(window).scroll(function () {
       $scrollingDiv.stop()
           .animate({
           "marginTop": ($(window).scrollTop() + 0) + "px"
       }, "slow");
       $scrollingDiv.css("background-color", (($(window).scrollTop() / $(document).height()) > 0.15) ? "orange" : "");
     });
});

Demo

Solution 2:

<scripttype="text/javascript">
    $().ready(function() {
            var $scrollingDiv = $("#navbar");

            $(window).scroll(function(){            
                    $scrollingDiv
                            .stop()
                            .animate({"marginTop": ($(window).scrollTop() + 0)+ "px"},"slow");
                           $(".yourdiv").css('background-color', 'blue');
            });
    });

Post a Comment for "Change Div Color After Scrolling 15% Down With Jquery"