Seeking assistance with debugging this jQuery code that manipulates the top margin of an element within a container. The container has a fixed size and overflow set to hidden to create a parallax effect. The parallax effect is functioning properly, but it breaks if the page is reloaded after scrolling even a single pixel. Any suggestions on how to resolve this issue? Below is the code snippet.
// Parallax for Header
$(window).on("load", function() {
var startingScrollTop = parseInt($(window).scrollTop());
console.log(startingScrollTop);
var startingElementTop = parseInt($headerText.css("margin-top"));
console.log(startingElementTop);
$(window).on("scroll", function() {
var currentScrollTop = parseInt($(window).scrollTop());
console.log(currentScrollTop);
if (currentScrollTop > startingScrollTop) {
var scrollDistance = currentScrollTop - startingScrollTop;
console.log(scrollDistance);
var parallaxDistance = scrollDistance / 2;
console.log(parallaxDistance);
var newElementMarginTop = startingElementTop + parallaxDistance;
console.log(newElementMarginTop);
$headerText.css("margin-top", newElementMarginTop + "px");
} else if (currentScrollTop <= "0") {
$headerText.css("margin-top", "0px");
}
});
});
Your help is greatly appreciated.