So in my project, I currently have 2 sections.
Section 1 as it is reached, the page has to stick to top and scroll from left to right
Section 2 as it is reached, the page has to stick to top and scroll from right to left
this is my current function I am running.
var windowWidth = window.innerWidth;
var horLength = document.querySelector(".element-wrapper").scrollWidth;
var horLength2 = document.querySelector(".element-wrapper2").scrollWidth;
var distFromTop = document.querySelector(".horizontal-section").offsetTop;
var distFromTop2 = document.querySelector(".horizontal-section2").offsetTop;
var scrollDistance = distFromTop + horLength - windowWidth;
var scrollDistance2 = distFromTop2 + horLength2 - windowWidth;
document.querySelector(".horizontal-section").style.height = horLength + "px";
document.querySelector(".horizontal-section2").style.height = horLength2 + "px";
window.onscroll = function() {
var scrollTop = window.pageYOffset;
var scrollTop2 = window.pageYOffset;
if (scrollTop >= distFromTop && scrollTop <= scrollDistance) {
document.querySelector(".element-wrapper").style.transform = "translateX(-" + (scrollTop - distFromTop) + "px)";
}
if (scrollTop2 >= distFromTop2 && scrollTop2 <= scrollDistance2) {
document.querySelector(".element-wrapper2").style.transform = "translateX(" + (scrollTop2 - distFromTop2) + "px)";
}
}
Now section 1
works perfectly and scrolls from left to right as I want it to do.
But section 2
has the same var value set window.pageYOffset
and is getting overriden by the value of var scrollTop
, I know thats what is causing the issue, but how can I perhaps reverse the Y offset value for it to not run my scrollTop2
with the scrollTop
while its running the first function for section
1?
If I were to delete section 1
from my code, section 2
runs perfectly fine but if I run them together, section 2
is overriden by section 1
.