Sticky Navigation Bar with scroll effect doesn’t work on browser while it does on codepen

I’m trying to add a sticky nav bar on my web site which takes into effect after i scroll down to a specific point. well i know how positions work and i did well with sticking it. but problem is it is not sticking when i try to use JS and JQuery for scroll down purpose. I referred to a tut for same thing and happened to see a code on “codepen.io” as well. But when i tried the same exact code on my computer and browser it didn’t work either. strange thing is that the same code works on jsfiddle.net and codepen.io but when i try it with my notepad++ it doesn’t work at all. I don’t know where i’m making mistakes.

here is pastebin for my code >> Sticky Navigation complete code

here is Jsfiddle link >> Sticky Navigation with scroll down effect

<!DOCTYPE HTML>
<html>
<head>
    <title>
        Sticky Navigation Bar
    </title>
    <script src="jquery.min.js" type="text/javascript"></script>

    <script>
        var  mn = $(".main-nav");
        mns = "main-nav-scrolled";
        hdr = $('header').height();

        $(window).scroll(function() {
            if( $(this).scrollTop() > hdr ) {
                mn.addClass(mns);
            } else {
                mn.removeClass(mns);
            }
        });
    </script>

    <style>
        *{
        box-sizing: border-box;
        }
        body {
          margin: 0;
          padding-top: 250px;
        }
        header {
          height: 300px;
          padding-top: 50px;
          background: #f07057;
        }
        .main-nav,
        .main {
          position: relative; 
        }
        .main-nav {
          background: #fff;
          height: 80px;
          z-index: 150;
          margin-bottom: -80px;
          box-shadow: 0 2px 3px rgba(0,0,0,.4);
        }
        header,
        .main-nav-scrolled {
          position: fixed;
          width: 100%;
          top: 0;
        }
        .main {
          background: #f2f2e8;
          padding: 110px 50px 50px;
          column-count: 2;
          column-gap: 40px;
        }
    </style>        
</head>

<body>
    <header>
        <h1>This is a Sticky Nav Demo!</h1>
        <p>Creating one of these isn't so bad. Let's learn how with this sweet little demo!</p>
    </header>
    <nav class="main-nav">
        <a href="#">Nav Link 1</a>
        <a href="#">Nav Link 2</a>
        <a href="#">Nav Link 3</a>
        <a href="#">Nav Link 4</a>
    </nav>
    <div class="main">
        <h2>Lorem ipsum dolor sit amet</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean fringilla, purus in porttitor pellentesque, erat arcu tincidunt diam, dapibus faucibus leo mauris at sapien. In porttitor vehicula sodales. Vivamus massa neque, facilisis eu felis ut, aliquet convallis nisi. Nam elementum tellus vitae gravida fermentum. Nullam et imperdiet leo. Integer ut euismod lorem, in placerat lacus. Curabitur bibendum arcu ut feugiat commodo. Suspendisse ut mi vel orci ullamcorper tincidunt. Nam vitae fringilla nibh. Nullam hendrerit blandit velit eu hendrerit.</p>

        <p>Praesent eu enim non massa pellentesque lobortis. In in sagittis dolor. Aliquam non massa erat. Ut aliquet gravida tellus, sed volutpat nibh condimentum et. Nunc quam purus, vehicula quis venenatis et, porttitor vel dolor. Cras facilisis dui id elit bibendum, in ullamcorper leo ultricies. Praesent rutrum lacus sit amet sem convallis, ut interdum dolor vestibulum.</p>

        <p>Tum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In iaculis pharetra odio, sit amet consectetur elit facilisis ac. Praesent eget tristique ipsum. Donec malesuada bibendum lacinia. Praesent non purus sodales, pulvinar mi vitae, tincidunt leo. Phasellus vitae elit ut nisl semper fringilla id rutrum dolor. Donec a massa adipiscing, cursus risus vitae, porttitor tortor. Nullam sagittis est sapien, sit amet pharetra turpis imperdiet vel. Etiam sit amet ligula pretium, vulputate eros ac, bibendum velit. Aenean convallis ante purus, ac bibendum orci laoreet ac. Donec a convallis mauris. Nulla non lacus non ipsum pretium tempor. In hac habitasse platea dictumst. Aenean at ipsum vulputate, sagittis dui ut, pharetra neque. Nam eget sodales orci. Aliquam pharetra nunc at nisl pellentesque, nec fringilla enim iaculis.</p>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean fringilla, purus in porttitor pellentesque, erat arcu tincidunt diam, dapibus faucibus leo mauris at sapien. In porttitor vehicula sodales. Vivamus massa neque, facilisis eu felis ut, aliquet convallis nisi. Nam elementum tellus vitae gravida fermentum. Nullam et imperdiet leo. Integer ut euismod lorem, in placerat lacus. Curabitur bibendum arcu ut feugiat commodo. Suspendisse ut mi vel orci ullamcorper tincidunt. Nam vitae fringilla nibh. Nullam hendrerit blandit velit eu hendrerit.</p>

        <p>Praesent eu enim non massa pellentesque lobortis. In in sagittis dolor. Aliquam non massa erat. Ut aliquet gravida tellus, sed volutpat nibh condimentum et. Nunc quam purus, vehicula quis venenatis et, porttitor vel dolor. Cras facilisis dui id elit bibendum, in ullamcorper leo ultricies. Praesent rutrum lacus sit amet sem convallis, ut interdum dolor vestibulum.</p>

        <p>Tum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. In iaculis pharetra odio, sit amet consectetur elit facilisis ac. Praesent eget tristique ipsum. Donec malesuada bibendum lacinia. Praesent non purus sodales, pulvinar mi vitae, tincidunt leo. Phasellus vitae elit ut nisl semper fringilla id rutrum dolor. Donec a massa adipiscing, cursus risus vitae, porttitor tortor. Nullam sagittis est sapien, sit amet pharetra turpis imperdiet vel. Etiam sit amet ligula pretium, vulputate eros ac, bibendum velit. Aenean convallis ante purus, ac bibendum orci laoreet ac. Donec a convallis mauris. Nulla non lacus non ipsum pretium tempor. In hac habitasse platea dictumst. Aenean at ipsum vulputate, sagittis dui ut, pharetra neque. Nam eget sodales orci. Aliquam pharetra nunc at nisl pellentesque, nec fringilla enim iaculis.</p>
    </div>
</body>
</html>

Answer

I’ve got my problem solved by placing:

<script>
    var  mn = $(".main-nav");
    mns = "main-nav-scrolled";
    hdr = $('header').height();

    $(window).scroll(function() {
        if( $(this).scrollTop() > hdr ) {
            mn.addClass(mns);
        } else {
            mn.removeClass(mns);
        }
    });
</script>

at the end of the document, before closing </body> tag.

Leave a Reply

Your email address will not be published. Required fields are marked *