I’m working on a Shopify page where I need to create scrolling animations for two images. Right now I have one image that moves correctly when the user scrolls, but the second image stays in place. I’m testing everything with hardcoded values first before converting to proper Liquid syntax.
jQuery(document).ready(function($) {
$(window).on("load scroll", function() {
var scrollElement = $(".photo1"),
elementCount = scrollElement.length;
window.requestAnimationFrame(function() {
for (var j = 0; j < elementCount; j++) {
var activeElement = scrollElement.eq(j),
windowPosition = $(window).scrollTop(),
elementPosition = activeElement.offset().top,
elementSize = activeElement.height(),
viewportCenter = window.innerHeight * 0.5 - elementSize * 0.5,
scrollDistance = windowPosition - elementPosition + viewportCenter;
activeElement.css({
transform: "translate3d(0," + scrollDistance * -0.35 + "px, 0)"
});
}
});
});
});
.heading {
text-align: center;
}
.content {
max-width: 50%;
text-align: center;
margin: 0 auto;
}
.photo1 img {
margin-top: -175px;
height: 500px;
max-height: 10%;
object-fit: contain;
overflow: hidden;
}
.photo2 img {
height: 500px;
position: absolute;
right: 20px;
}
<div class="wrapper">
<div class="heading">
<h1>Main Heading</h1>
</div>
<div class="content">
<p>Sample text content goes here for demonstration purposes.</p>
</div>
<div class="photo1">
<img src="image1.jpg"/>
</div>
<div class="photo2">
<img src="image2.jpg"/>
</div>
</div>
How can I modify this code to make the second image also respond to scroll events and move upward when scrolling?