I have created a fiddle with infinite horizontal scroll using CSS animation and I want the first element in the list to be sticky.
It works as expected when using the left
property in the @keyframes
whereas it does not work when the transform: translateX
property is used.
The reason for changing to transform is due to
- browser performance
- The elements inside the cards are flickering in a real-time scenario (where I have more complex element structure inside
.card
) which is expected to be solved by updating the code to thetransform
property.
Can anyone provide a solution for this scenario using transform
?
.container {
display: flex;
overflow: hidden;
}
.scroller {
display: flex;
position: relative;
animation: autoScroll 10s linear infinite;
}
.card {
padding: 10px;
margin: 0 10px;
width: 70px;
background: lightblue;
}
.sticky-el {
width: 100px;
background: cyan;
position: sticky;
left: 0;
}
@keyframes autoScroll {
0% {
transform: translateX(0);
//left: 0;
}
100% {
transform: translateX(-100%);
//left: -100%;
}
}
<div class="container">
<div class="scroller">
<div class="card sticky-el">Sticky 1</div>
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
</div>
<div class="scroller">
<div class="card sticky-el">Sticky 2</div>
<div class="card">Card 21</div>
<div class="card">Card 22</div>
<div class="card">Card 23</div>
</div>
</div>
2
Answers
Check if you need this.
To achieve the desired effect, you can use a combination of position: fixed and JavaScript to create a similar scrolling effect while keeping the sticky behavior.