Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?