I'm confused, I've been trying to use a onscroll function to get a value forscrollTop. In an onload function it worked and returned 0 to the console, but with onscroll I get nothing. Tried it in codepen (https://codepen.io/dillonbrannick/pen/LqvzeR) and the console returns exactly as it should, not sure why on my web page's the console isn't returning anything for the exact same function.
The function in question:
window.onscroll = function() {
var y = document.documentElement.scrollTop;
console.log(y);
};
As Tyler pointed out in the comments the problem is due to the parallax effect, so I've updated the question to reflect this. The parralax effect was built off of Keith Clark's Parallax code (https://keithclark.co.uk/articles/pure-css-parallax-websites).
window.onscroll = function() {
var y = document.documentElement.scrollTop;
console.log(y);
};
body{
height:100%;
margin: 0px 0 0px 0;
overflow:hidden;
}
.parallax {
perspective: 1px;
height: 100vh;
overflow-x: hidden;
overflow-y: visible;
transform-origin-x: 100%;
}
.parallax__layer {
position: absolute;
top: 0;
right: 0;
bottom: 0;
left: 0;
transform-origin-x: 100%;
}
.parallax__layer--base {
transform: translateZ(0);
}
.parallax__layer--back_01 {
transform: translateZ(-5px) scale(8);
}
.bg {
position:relative;
left:100px;
top:200px;
height:100px;
width:100px;
background-color:red;
}
p {
background-color:blue;
height:250px;
color:white;
}
.wrapper {
position:relative;
width:80%;
max-width:960px;
margin:0 auto 0 auto;
height: auto;
overflow:hidden;
}
.more{
margin-top:200px;
position:relative;
}
<body>
<div class="parallax">
<div class="parallax__layer parallax__layer--back_01">
<div class="bg"></div>
</div>
<div class="parallax__layer parallax__layer--base">
<div class="wrapper">
<p>content</p>
<p class="more">More</p>
</div>
</div>
</div>
</body>
Thanks
window, but rather a container (<body>or otherwise). If you scroll around and run your code in console at different points (document.documentElement.scrollTop;), you'll notice the result is always0regardless of where you've scrolled to.onscrollevent to your<div class="paralax">instead of thewindowelement, the event fires as you're expecting. That said, the result is still0simply because you're getting thescrollTopofdocumentElement, which as explained, isn't actually scrolling at all.