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

Java script scrollTop();

I want to make a sticky nav when scrolling and I`ve been testing the scrollTop() function bu it doesnt work. Here is my code please help me. Thanks.

<script type="text/javascript"> $(function(){ if( $('window').scrollTop() > 300){ var number = $('window').scrollTop(); console.log('number'); } else { console.log('cf rares ?'); } }); </script>

You're adding quotes around window object which return null value back that's why code is not working as expected.

Modure Rares
Modure Rares
Courses Plus Student 9,041 Points

I changed my code but now it doesnt apply any other css to my page. Please help me. ``` Java Script $(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
$(function(){
        $(window).scroll(function(){
            if($(document).scrollTop() > 300){
                $('#navbar').addClass('sticky');
            } else {
                $('#navbar').removeClass('sticky');
            }
        })
    });

1 Answer

William Siewert
William Siewert
9,448 Points

Hi there, In future reference you can place all of your code within 3 backticks ` on both sides of your code. Also, after the first 3 backticks you can place the language, (html, javascript, css etc.) this allows it to color code the language appropriately. For other references when composing questions/responses, please see the "Markdown Cheatsheet" below your textbox.

var example = "Example JavaScript Code";