1

I'm trying to make an object "sticky" between 2 divs. So far, what I've been able to find only allows me to make an object sticky after a div, but not also before another div.

I'm not very familiar with Javascript, and I've looked around at many other questions on If/else statements combined with and (&&)/or (||) operators (+ tried to find out how to format these statements in general), but so far, no dice.

I tried modifying the code from w3schools here (as seen below): https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_navbar_sticky

What I was trying to achieve here was the sticky class being removed from the navbar after someone scrolls down and hits the top of the footer. The sticky class is also removed once the viewer scrolls up past the navbar.

if ((window.pageYOffset < sticky) works, but (window.pageYOffset >= sticky2) does not, and I'm really not sure why despite trying a few different variants (e.g. I've also tried: if (window.pageYOffset >= sticky) && (window.pageYOffset < sticky2)).

Could someone point me in the right direction? Thank you very much!

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
  margin: 0;
  font-size: 28px;
}

.header {
  background-color: #f1f1f1;
  padding: 30px;
  text-align: center;
}

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

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

#navbar a:hover {
  background-color: #ddd;
  color: black;
}

#navbar a.active {
  background-color: #4CAF50;
  color: white;
}

.content {
  padding: 16px;
}

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

.sticky + .content {
  padding-top: 60px;
}
</style>
</head>
<body>

<div class="header">
  <h2>Scroll Down</h2>
  <p>Scroll down to see the sticky effect.</p>
</div>

<div id="navbar">
  <a class="active" href="javascript:void(0)">Home</a>
  <a href="javascript:void(0)">News</a>
  <a href="javascript:void(0)">Contact</a>
</div>

<div class="content">
  <h3>Sticky Navigation Example</h3>
  <p>The navbar will stick to the top when you reach its scroll position.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>

<div id="footer">Footer here<br>
addasasd
<br>
radadsad
<br>
Footer here<br>
addasasd
<br>
radadsad
<br>
Footer here<br>
addasasd
<br>
radadsad
<br>
Footer here<br>
addasasd
<br>
radadsad
<br>Footer here<br>
addasasd
<br>
radadsad
<br></div>

<script>
window.onscroll = function() {myFunction()};

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

function myFunction() {
  if (window.pageYOffset >= sticky) {
    navbar.classList.add("sticky");
  } if ((window.pageYOffset < sticky) || (window.pageYOffset >= sticky2)) {
    navbar.classList.remove("sticky");
  }
}
</script>

</body>
</html>

Below is a screenshot of an error I'm encountering despite using basically the same code:

enter image description here

Grace
  • 247
  • 2
  • 9

1 Answers1

1

The error is var footer = document.getElementbyId("footer"); because the method is getElementById(). If you correct that your code works:

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

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

function myFunction() 
{
  if (window.pageYOffset >= sticky) 
  {
    navbar.classList.add("sticky");
  } 
  
  if ((window.pageYOffset < sticky) || (window.pageYOffset >= sticky2)) 
  {
    navbar.classList.remove("sticky");
  }
}
body {
  margin: 0;
  font-size: 28px;
}

.header {
  background-color: #f1f1f1;
  padding: 30px;
  text-align: center;
}

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

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

#navbar a:hover {
  background-color: #ddd;
  color: black;
}

#navbar a.active {
  background-color: #4CAF50;
  color: white;
}

.content {
  padding: 16px;
}

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

.sticky + .content {
  padding-top: 60px;
}
<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>

<div class="header">
  <h2>Scroll Down</h2>
  <p>Scroll down to see the sticky effect.</p>
</div>

<div id="navbar">
  <a class="active" href="javascript:void(0)">Home</a>
  <a href="javascript:void(0)">News</a>
  <a href="javascript:void(0)">Contact</a>
</div>

<div class="content">
  <h3>Sticky Navigation Example</h3>
  <p>The navbar will stick to the top when you reach its scroll position.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
  <p>Some text to enable scrolling.. Lorem ipsum dolor sit amet, illum definitiones no quo, maluisset concludaturque et eum, altera fabulas ut quo. Atqui causae gloriatur ius te, id agam omnis evertitur eum. Affert laboramus repudiandae nec et. Inciderint efficiantur his ad. Eum no molestiae voluptatibus.</p>
</div>

<div id="footer">Footer here<br>
addasasd
<br>
radadsad
<br>
Footer here<br>
addasasd
<br>
radadsad
<br>
Footer here<br>
addasasd
<br>
radadsad
<br>
Footer here<br>
addasasd
<br>
radadsad
<br>Footer here<br>
addasasd
<br>
radadsad
<br></div>
</body>
</html>

For the next time, if you enable the browser console (F12) you will be able to see the error in few seconds. ;)

In this case you would have seen:

enter image description here

Have a good day.

Fabio_MO
  • 713
  • 1
  • 13
  • 26
  • Thank you so much Fabio! I had no idea it was letter case specific! Thanks a lot for the tip on highlighting errors as well. I've tried putting the code in a page, but am having another error :/ (Uncaught TypeError: Cannot read property 'offsetTop' of null) I don't see how I'm defining anything differently so I'm very confused as to why this is happening. If it's not too much trouble, would you mind taking a look? I'll add the screenshot of the error and code above. – Grace Jun 07 '18 at 09:49
  • 1
    @Grace I suggest you to chek if the element exist: https://stackoverflow.com/questions/22057610/uncaught-typeerror-cannot-read-property-value-of-null – Fabio_MO Jun 07 '18 at 09:59
  • @Grace If the check is not ok, try to wait till the page is fully loaded: https://stackoverflow.com/questions/978740/javascript-how-to-detect-if-document-has-loaded-ie-7-firefox-3 – Fabio_MO Jun 07 '18 at 10:00
  • Thanks so much again Fabio!! Shachi's comment on that question helped (I should have put the script BELOW my footer, which is why it wasn't executing correctly). Your comment on waiting for the page to fully load made me take note of that. – Grace Jun 07 '18 at 10:03