I’m aiming to recreate an animation using JavaScript and CSS. It involves a list of words from a CMS, where the quantity of a single word can change. The goal is to create an endless vertical scrolling effect without a visible loop restart, similar to a marquee. I’m struggling with modifying my code to achieve the desired behavior. CSS animations are a bit challenging for me to grasp, so any assistance would be greatly appreciated. I’m open for a new markup structure if necessary.
My code: https://codesandbox.io/s/2v85nk?file=/src/TextSlider.js
The desired effect: https://drive.google.com/file/d/1bDVqeulKpwFyDKA9sKqxduR7OFgBZKK0/view?usp=sharing
I attempted to modify these two lines within .slider-item, making some progress but not quite reaching the desired outcome.
.slider-item {
opacity: 0.5; // Added to show the previous item slightly while transitioning
transform: translateY(-100%); // Altered scrolling direction
}
2
Answers
The new element appearing in view seems to work fine. But to make is more like desired effect you need to show old element moving out of view smoothly.
First thing if you need transition on
opacity
as well add it to.slider-item
class. You can add transitions for multiple property :.slider-item
keeps the element below and out of visible area..slider-item.active
brings it into visible area.Now just add class
.slider-item.prev-active
class that will move the element above and visible area. This should be added to element whoseindex == currentIndex -1
. There however is a catch with using this condition as is. After a full round – The(texts.length -1)th
element won’t move out becausecurrentIndex - 1
is -1 at this point.I modified this condition a bit to work well when the index becomes 0 after a full round:
The css class:
The class can be added as :
Full code :
We can use scroll snapping: