michaelbaillot Posted May 14, 2019 Share Posted May 14, 2019 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/ Link to comment Share on other sites More sharing options...
justsomeguy Posted May 14, 2019 Share Posted May 14, 2019 What do you mean it doesn't work, what actually happens? Link to comment Share on other sites More sharing options...
michaelbaillot Posted May 20, 2019 Author Share Posted May 20, 2019 On 5/14/2019 at 7:31 PM, justsomeguy said: What do you mean it doesn't work, what actually happens? Well, as you can see online, nothing happens… Thanks, best, Michael Link to comment Share on other sites More sharing options...
michaelbaillot Posted May 20, 2019 Author Share Posted May 20, 2019 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> --> Link to comment Share on other sites More sharing options...
justsomeguy Posted May 20, 2019 Share Posted May 20, 2019 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. Link to comment Share on other sites More sharing options...
michaelbaillot Posted May 20, 2019 Author Share Posted May 20, 2019 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… Link to comment Share on other sites More sharing options...
justsomeguy Posted May 20, 2019 Share Posted May 20, 2019 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. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now