SvekarP SvekarP - 5 months ago 14
jQuery Question

change navbar-fixed-top color when scrolling

I'm trying to change

navbar
color when you start scrolling the page, and using some well known
jquery
code but it's not working. I'm also combining css transition with it. I'm little confuse why is this not transitioning after
* 2
height's of my
navbar
.

Here is my JSFIDDLE, can someone check it and explain me what is wrong in this code.

function checkScroll(){
var startY = $('.navbar').height() * 2;

if ($(window).scrollTop() > startY) {
$('.navbar')addClass("scrolled");
} else {
$('.navbar').removeClass("scrolled");
}
}

if ($('.navbar').lenght > 0) {
$(window).on("scroll load resize", function(){
checkScroll();
});
}

Answer

typo in the code (lenght) - should be :

if ($('.navbar').length> 0) {
    $(window).on("scroll load resize", function(){
        checkScroll();
    });
}

also missing dot before the addClass:

 $('.navbar').addClass("scrolled");