Your script is running before the DOM elements that it makes use of are rendered. Things like #main
don't exist yet because your code is being executed as soon as it's parsed by the browser (unless you specifically ask it to wait) and what you are trying to access is undefined
. Move the script
tags in the body to the bottom of the body or use one of the many methods for delaying execution until the DOM loads.
<body>
<nav class="nav">
<a href="#">link</a>
</nav>
<div id="main">#main</div>
<div id="below-main">#below-main</div>
<script>
$(function() {
//your code
var mainbottom = $('#main').offset().top + $('#main').height();
$(window).on('scroll',function(){
stop = Math.round($(window).scrollTop());
if (stop > mainbottom) {
$('.nav').addClass('past-main');
} else {
$('.nav').removeClass('past-main');
}
});
});
</script>
</body>
or
$(document.ready(function() {
//your code
});
or
window.onload = function() {
//your code
};