I'm trying to animate back and forth (or infinite scroll to the right) a bunch of images but I'm facing an issue in which when the animation starts (and when it reverses) the images are hidden outside of view.
That's because I'm using translate(-100%).
If possible, I would like to not depend of knowing the image size or limiting the viewport size to make this work.
(Ignore the speed of the animation, I'm going to set to a longer duration later)
.slideshow {
height: 150px;
/*max-width: 800px;*/ /*The width of the page cannot be a problem*/
margin: 0 auto;
position: relative;
overflow: hidden;
transform: translate3d(0, 0, 0);
}
.slideshow > div {
height: 150px;
width: 2000px; /*I don't want to have to input a width.*/
position: absolute;
top: 0;
left: 0;
height: 100%;
transform: translate3d(0, 0, 0);
}
.move > img {
height: 150px;
}
.slideshow .move {
animation: moveSlideshow 10s linear infinite alternate-reverse;
}
.slideshow .move:hover {
-webkit-animation-play-state: paused;
-moz-animation-play-state: paused;
-o-animation-play-state: paused;
animation-play-state: paused;
}
@keyframes moveSlideshow {
0% {
transform: translateX(0%);
}
100% {
transform: translateX(-100%); /*The width of the page needs to be taken into consideration*/
}
}
<div class="slideshow">
<div class="move">
<img src="https://via.placeholder.com/300x150">
<img src="https://via.placeholder.com/300x150">
<img src="https://via.placeholder.com/300x150">
<img src="https://via.placeholder.com/300x150">
<img src="https://via.placeholder.com/300x150">
<img src="https://via.placeholder.com/300x150">
</div>
</div>
transform: translateX(100%) translateX(-50px);, but it does not work.
My first goal was to make an infinite scroller, when the animation reaches the end, it should start again, but without being noticeable (smooth restart, end image needs to be followed by the start image).
If I'm unable to do that, I would like to have a back and forth animation, but starting with the last image touching the right edge of the viewport. (With the code above, the animation starts with the images to the left)
How can I fix this animation? (To enable infinite smooth scrolling or to not disappear from the view)
Edit:
I managed to make it work with the animation back and forth, by using these keyframes:
@keyframes moveSlideshow {
0% {
transform: translateX(0%);
}
100% {
-ms-transform: translateX(-100%) translateX(100vw);
transform: translateX(calc(-100% + 100vw));
}
}
Now, the last image starts at the right and the first image ends at the left. The only problem is that the speed in which the animation plays changes when the viewport width changes.