Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Modure Rares
PLUS
Modure Rares
Courses Plus Student 9,041 Points

Sticky navigation with javascript. Please help me guys.

I need to make a sticky navigation when scrolling but my code has no effect. Please help me I really need this done.

$(function(){
        $(window).scroll(function(){
            if($(document).scrollTop() > 300){
                $('#navbar').addClass('sticky');
            } else {
                $('#navbar').removeClass('sticky');
            }
        })
    });
Modure Rares
Modure Rares
Courses Plus Student 9,041 Points

I tried this too but it still doesnt work.

$(function(){
        if($(document).scrollTop() > 250 ){
            $('#navbar').addClass('sticky');
        } else {
            $('#navbar').removeClass('sticky');
        }
    });
Cengiz Demir
Cengiz Demir
5,055 Points

You need to .offset().top;

Raymon Oleaga
Raymon Oleaga
19,298 Points

The firs code works form me. I would just make sure you are targeting the correct ID and or that the class that you are using does infract stick... so maybe you need to check your CSS.

When you scroll do you see when the class is added or not at all?