I’m working on developing infinite scroll on my wordpress website, so I have two "Back-to-top" and "Go-to-bottom" buttons. They do their job, but I would like them to remain hidden until the user scrolls a bit. Is there a way to do it through CSS? Everything I tried just failed.
Here’s the code I’m using for the buttons:
<div style="position: fixed; bottom: 70px; right: 25px; z-index: 900">
<a href="#top">
<img class=" lazyloaded" src="https://imthemoisturizer.com/wp-content/uploads/2022/04/Top.svg" data-src="https://imthemoisturizer.com/wp-content/uploads/2022/04/Top.svg" alt="Boton subir" width="40" height="40">
</a>
</div>
<div style="position: fixed; bottom: 25px; right: 25px; z-index: 900">
<a href="#fin">
<img class=" lazyloaded" src="https://imthemoisturizer.com/wp-content/uploads/2022/04/Bottom.svg" data-src="https://imthemoisturizer.com/wp-content/uploads/2022/04/Bottom.svg" alt="Boton bajada" width="40" height="40">
</div>
I tried adding margin-top: 100vh
, which would be my ideal solution, and some of the code shared by Afif Temani here, but I can’t make it work.
Thanks for your time and help!
2
Answers
Add below CSS and JS and it will hide button initially; and show up when you scroll
The whole solution is a bit complicated. Let me demonstrate in a JSFiddle. Try it.
HTML
Added a CSS class to the buttons’ DIV.
CSS
I set the buttons to hide initially.
JavaScript (require jQuery)
If you don’t want to use jQuery, you can replace two lines with
$
with vanilla JavaScriptdocument.getElementByClassName('btn_controls')
bla bla bla.p.s. IMO as you’re developing websites, you must learn JavaScript.