0

I'm trying to make a sticky navbar on scroll with CSS and JavaScript, but it's show as a normal navbar, and it's not sticking when scrolling. How can I make it stick when it is scrolling? i added the header to the code

window.onscroll = function() {
  myFunction()
};

var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky")
  } else {
    navbar.classList.remove("sticky");
  }
}
#navbar {
  overflow: hidden;
  background-color: #333;
}


/* Navbar links */

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px;
  text-decoration: none;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

.sticky+.content {
  padding-top: 60px;
}
<div class="header">
	<h2>S I U</h2>
	<p>T h e  f u t u r e  &  B e y o n d</p>
</div>

<div id="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>

13
  • As you mentioned it, did you try some JavaScript? Commented Feb 14, 2019 at 0:07
  • 1
    You have .sticky as a selector in your CSS but you don't have that class anywhere in your HTML. Commented Feb 14, 2019 at 0:08
  • 2
    Possible duplicate of Sticky NavBar onScroll? Commented Feb 14, 2019 at 0:08
  • 1
    Hmm, it's a little unclear what exactly you wish to accomplish. Would you please provide some specific details? Commented Feb 14, 2019 at 0:15
  • 1
    Perhaps it's simply a matter of inserting some content above your nav? Or possibly you have not set up your document correctly? Hard to say! Commented Feb 14, 2019 at 0:26

2 Answers 2

3

Almost there! You need to apply the .sticky class to your nav in order for it to stick.

window.onscroll = function() { myFunction() };
var navbar = document.getElementById("navbar");
var sticky = navbar.offsetTop;

function myFunction() {
    if (window.pageYOffset >= sticky) {
        navbar.classList.add("sticky")
    } else {
        navbar.classList.remove("sticky");
    }
}
body {
   height: 10000px;
}

#navbar {
  overflow: hidden;
  background-color: #333;
}


/* Navbar links */

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px;
  text-decoration: none;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

.sticky+.content {
  padding-top: 60px;
}
<div class="header">
  <h2>Scroll Down</h2>
  <p>Scroll down to see the sticky effect.</p>
</div>

<div id="navbar">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>

Sign up to request clarification or add additional context in comments.

11 Comments

it worked, but now it's sticking in the top of the page not on scrolling.
@Josephizz if that's the case, the full code is here: w3schools.com/howto/tryit.asp?filename=tryhow_js_navbar_sticky
In which case you are correct to not apply the sticky class in your HTML. I have edited my answer to reflect this (with the code straight from the w3 resource)
Are you, perchance, using Opera Mini? caniuse.com/#search=position%3A%20fixed
Just not in your document, I assume? Are you sure you've got everything syntactically correct? Javascript in <script> tag at the bottom of body, and all that?
|
2

You aren't applying your .sticky class. Add it to your navbar (I added a bunch of div's to force some scrolling)

#navbar {
  overflow: hidden;
  background-color: #333;
}


/* Navbar links */

#navbar a {
  float: left;
  display: block;
  color: #f2f2f2;
  text-align: center;
  padding: 14px;
  text-decoration: none;
}

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

.sticky+.content {
  padding-top: 60px;
}
<div id="navbar" class="sticky">
  <a href="#home">Home</a>
  <a href="#news">News</a>
  <a href="#contact">Contact</a>
</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>
<div>Test to cause some stroll</div>

2 Comments

i want it to stick at the top of the page "after" scrolling only.
I see. In that case, create a javascript function to apply .sticky when a user scrolls. If they are at the top of the page, you can detatch the class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.