Hello, I can't get an infinite horizontal image slider to work correctly,
Images are getting positionned incorrectly, because they have different widths.
"My code" is here:
codepen.io/eze4fz4e4zegzegzeg/pen/bGzRoRM
It's based on this Codepen:
codepen.io/supah/pen/VwegJwV
It's very close to it, except my images each have different widths and a common height.
On "My code" HTML i've changed each image widths using style="width:XXvw"
I also changed wrapWidth so that it is a sum of all the items clientWidth
let wrapWidth = 0;
for (let i = 0; i < $items.length; i++) {
wrapWidth += $items[i].clientWidth;
}
On the original codepen, it's using let itemWidth = $items[0].clientWidth
So I also changed the dispose() function's set() and wrap(), so that it doesn't use the first item's clientWidth, like this:
const dispose = (scroll) => {
gsap.set($items, {
x: (i) => {
// Use the clientWidth of each item in $items
return i * $items[i].clientWidth + scroll;
},
modifiers: {
x: (x, target) => {
const currentIndex = Array.from($items).indexOf(target);
// Wrap based on the clientWidth of the current item
const s = gsap.utils.wrap(-$items[currentIndex].clientWidth, wrapWidth - $items[currentIndex].clientWidth, parseInt(x));
return `${s}px`;
},
},
});
};
dispose(0)
Would you know how I can fix my codepen?
I think the problem is in the dispose() function where the index is getting messed up.. But I don't get it
Thank you for reading