I have successfully created a CSS Parallax effect, but I am facing some challenges with the scrollTop property while using overflow: hidden on the html element.
The CSS requires overflow: hidden to be applied either on the html tag or any relevant container, and overflow-x: hidden; overflow-y: auto on the body tag or any relevant container.
Whenever overflow is applied, whether on html and body tags or elsewhere, scrollTop does not function as intended.
I have managed to detect scrolling on the body to trigger an alert or add a class dynamically, but my goal is to remove the added class when the user scrolls back up to the top of the page.
Below is my current code. It only adds the relevant class on scroll:
HTML
<header>
</header>
<div class="parallax">
<div class="background"></div>
<div class="sheet-1">
<h1 class="page-header">My Parallax</h1>
</div>
<div class="sheet-2"></div>
</div>
SASS
\:root
font-size: 10px
*, *::before, *::after
box-sizing: border-box
margin: 0
padding: 0
text-decoration: none
html
overflow-y: auto
body
height: 100vh
overflow-y: auto
overflow-x: hidden
-webkit-overflow-scrolling: touch
perspective: 0.1rem
<!-- The rest of the SASS code remains unchanged -->
<p>JS</p>
<pre><code>$("body").on("scroll",function(){
$('header').addClass('black');
});
You can check out the project on Codepen here.