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 trialModure Rares
Courses Plus Student 9,041 PointsJava 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>
Modure Rares
Courses Plus Student 9,041 PointsI 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
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
9,448 PointsHi 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";
Ashish Mehra
3,407 PointsAshish Mehra
3,407 PointsYou're adding quotes around window object which return null value back that's why code is not working as expected.