Jump to content
michaelbaillot

How TO - Hide Menu on Scroll after X pixels

Recommended Posts

Hi w3schools,

thanks for your amazing website. I tried to mix two of your "how to" scripts (Hide Menu on Scroll and Shrink Navigation Menu on Scroll) so that my navbar hide after 180px from top.

But… I'm not a coder, I try to learn and it doesn't work for the moment:

<script>

var prevScrollpos = window.pageYOffset;
	
window.onscroll = function() {
var currentScrollPos = window.pageYOffset;
	if (document.body.scrollTop < 180 || document.documentElement.scrollTop < 180 || prevScrollpos > currentScrollPos) {
    	document.getElementById("navbar").style.top = "0";
	} else {
    	document.getElementById("navbar").style.top = "-180px";
	}
	prevScrollpos = currentScrollPos;
}
	
</script>

Any ideas?

Thanks a lot,

Best,

Michael

best regards,

Michael

 

Website: https://www.schema-studio.ch/

 

Share this post


Link to post
Share on other sites

What do you mean it doesn't work, what actually happens?

Share this post


Link to post
Share on other sites
On 5/14/2019 at 7:31 PM, justsomeguy said:

What do you mean it doesn't work, what actually happens?

As you cas see below, I've tried several tests: some are partly working, others not at all…

 

<!-- script permettant de gérer l'affichage du header en fcontion du scroll
<script>

	var prevScrollpos = window.pageYOffset;
window.onscroll = function() {
  var currentScrollPos = window.pageYOffset;
  if (prevScrollpos > currentScrollPos) {
    document.getElementById("navbar").style.top = "0";
  } else {
	document.getElementById("navbar").style.top = "-180px";
  }
  prevScrollpos = currentScrollPos;
}
	
</script> -->

<!-- V2 // script permettant de gérer l'affichage du header en fcontion du scroll
<script>

window.onscroll = function() {
  if (document.body.scrollTop > 180 || document.documentElement.scrollTop > 180) {
    document.getElementById("navbar").style.top = "-180px";
  } else {
    document.getElementById("navbar").style.top = "0";
  }
}
	
</script> -->

<!-- V3 // script permettant de gérer l'affichage du header en fcontion du scroll
<script>

var prevScrollpos = window.pageYOffset;
	
window.onscroll = function() {
	if (document.body.scrollTop > 180 || document.documentElement.scrollTop > 180) {
    	document.getElementById("navbar").style.top = "-180px";
	} else {
    	document.getElementById("navbar").style.top = "0";
		var currentScrollPos = window.pageYOffset;
			if (prevScrollpos > currentScrollPos) {
			document.getElementById("navbar").style.top = "0";
			} else {
			document.getElementById("navbar").style.top = "-180px";
		}
		prevScrollpos = currentScrollPos;
	}
}
	
</script> -->

 

Share this post


Link to post
Share on other sites

Use your developer tools to set a breakpoint in that scroll handler to see what the values are.  With the code on your site now:

if (document.body.scrollTop < 180 || document.documentElement.scrollTop < 180 || prevScrollpos > currentScrollPos) {

In my browser, document.body.scrollTop is always 0 so that if statement is always true.  Use a breakpoint to see what's going on in your browser.

Share this post


Link to post
Share on other sites
4 hours ago, justsomeguy said:

Use your developer tools to set a breakpoint in that scroll handler to see what the values are.  With the code on your site now:

 


if (document.body.scrollTop < 180 || document.documentElement.scrollTop < 180 || prevScrollpos > currentScrollPos) {

 

In my browser, document.body.scrollTop is always 0 so that if statement is always true.  Use a breakpoint to see what's going on in your browser.

Hi, I've tried to add a breakpoint following this link. I'm wondering if it's well set. The only value I get is currentScrollPos=0…

screen.png

Share this post


Link to post
Share on other sites

Yeah, that's the only local variable that has been created by that line.  You can check the global scope if you want, or you can make a watch expression to watch a specific variable or value, or in my browser if I hover over scrollTop it shows a popup that has the current value.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...