Hello Wix Community,
I’m currently working on a project in the Wix Code Editor, where I aim to create a scroll-controlled animation that displays image frames sequentially in a manner similar to a GIF. I’ve successfully obtained the value of scrollY, but I’m encountering challenges when implementing an event handler to control the image visibility.
Being relatively new to JavaScript, I suspect there might be some obvious mistakes in my code. I would greatly appreciate any guidance or suggestions and whether it’s possible or not.
Here’s my code:
import wixWindowFrontend from 'wix-window-frontend';
let currentFrame = 0; // Start with the first image in the range
const endFrame = 17; // Set the last image in the range
function updateFramesVisibility() {
wixWindowFrontend.getBoundingRect()
.then((windowSizeInfo) => {
const scrollY = windowSizeInfo.scroll.y;
// Calculate the height of each frame
const frameHeight = windowSizeInfo.window.height / (endFrame - currentFrame + 1);
// Calculate the index of the current frame
const newFrame = Math.floor(scrollY / frameHeight) + currentFrame;
// Update visibility only if the frame is within the specified range
if (newFrame >= currentFrame && newFrame <= endFrame && newFrame !== currentFrame) {
// Hide the previous frame
$w(`#image${currentFrame}`).hide();
// Show the new frame
$w(`#image${newFrame}`).show();
// Update the current frame
currentFrame = newFrame;
}
});
}
$w.onReady(function () {
// Set up scroll event handler
wixWindowFrontend.onScroll(updateFramesVisibility);
});