CSS animation for horizontal scrolling with sticky positioning is not functioning as intended

I've designed a creative way to achieve infinite horizontal scrolling using CSS animation, and my goal is to make the first element in the list stick in place.

Everything works smoothly when I utilize the left property within the @keyframes. However, things don't go as planned when I switch over to using the transform: translateX property.

The rationale behind this shift to the transform property includes:

  1. Improving browser performance
  2. Addressing the issue of elements flickering within real-time scenarios (where there's a more complex structure inside .card), which should ideally be resolved by updating the code to incorporate the transform property.

Could someone offer a solution for this situation utilizing the transform property?

.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>

Answer №1

Make sure to check whether this is necessary.
You can use a combination of position: fixed and JavaScript to achieve a similar scrolling effect while maintaining the sticky behavior.

.container {
display: flex;
overflow: hidden;
position: relative;
  }

  .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: -webkit-sticky;
position: sticky;
top: 0;
  }
  
  @keyframes autoScroll {
0% {
  transform: translateX(0);
}

100% {
  transform: translateX(-100%);
}
  }
  <div class="container">
<div class="scroller" id="scroller1">
  <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>
  <!-- Add more cards as needed -->
</div>

<div class="scroller" id="scroller2">
  <div class="card">Card 21</div>
  <div class="card">Card 22</div>
  <div class="card">Card 23</div>
  <div class="card">Card 24</div>
  <!-- Add more cards as needed -->
</div>
  </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

In a local setting, the KendoUI chips are displaying without their borders

I recently tested out the code from kendo on StackBlitz and it worked perfectly: https://i.stack.imgur.com/Nrp2A.png However, when running the same code on my localhost, all the styling for the chip is missing: https://i.stack.imgur.com/1pUH9.png The c ...

What strategies can I employ with flexbox to achieve my design goals?

How can I achieve my design requirements using flexbox? design requirements The current code that I have implemented follows the flexbox approach to meet the design requirements. However, the output of this code does not match the screenshot of my design ...

Is there a way I can position my privacy alert so that it appears in front of all DIVs, RevSlider,

Have you had a chance to visit my website at ? I recently added a privacy alert in compliance with the new EU Regulation. However, I'm facing an issue where the alert is overlapping with some of my website components such as other DIVs and Revolution ...

Utilizing jQuery for animating SVG elements with dynamic color changes and scaling effects upon hover

Seeking assistance from coding experts! I have created an icon and am attempting to modify it so that the color changes when hovered over. Additionally, I want the white square to scale down by 50% starting from the top-left corner of its current position. ...

Leveraging the power of HTML 5 to dynamically insert content into a jQuery table

I am experiencing an issue with my jquery stock ticker. It displays the latest stocks including Company Name, stock price, and percentage changed. The stock price can be in GBP, USD, or YEN, but the current ticker does not indicate the currency. I attemp ...

Utilizing regular expressions in Powershell to modify a CSS file

Snippet from my current theme.css file: .actionBlock[selected] { color: var(--gray11); background-color: var(--gray11); } I am trying to modify color: var(--gray11); to color: #FFF; /* UPDATED : var(--gray11) */ After attempting in Po ...

Ways to rearrange div elements using JavaScript

I need assistance with reordering div elements using JavaScript, as I am unsure of how to accomplish this task. Specifically, I have two divs implemented in HTML, and I would like the div with id="navigation" to appear after the div with class="row subhea ...

What is the process of sending a variable from PHP to HTML?

I am trying to pass a PHP variable to HTML. Here is the code I have created: <?php $neal = "mynema"; ?> Now, I want to pass this variable into my HTML like so: <html> <audio src="http://tts-api.com/tts.mp3?q=<?php echo $neal; ?>" ...

Creating dynamic text bubble to accommodate wrapped text in React using Material-UI (MUI)

I am currently developing a chat application using ReactJS/MUI, and I have encountered an issue with the sizing of the text bubbles. The bubble itself is implemented as a Typography component: <Typography variant="body1" sx={bubbleStyle}> ...

Which JSF element is capable of displaying a <div> element?

For example, h:inputText will display an "input type='text'". Which JSF tag is used to render a "div" element? ...

Issues with the last-child selector in CSS being unresponsive

I am having trouble applying styles to the final <p> in a specific div. I would like to achieve this without introducing a new class or ID. Despite attempting to use the last-child selector, I have not been successful in accomplishing my goal. I ha ...

"Converting jQuery Form into a Wizard feature with the ability to hide specific steps

I'm working on a form where I need to hide a fieldset when a specific event is triggered. Inside the first fieldset, there is a select tag and when a certain option is selected, the second fieldset should be hidden. <form id="form1"> <fi ...

Toggle Checkbox Group for Both Multiple and Single Selection Options

Need help with a function to enable only one checkbox for single selection or multiple checkboxes for multiple selection. Use MultipleCheckbox(0) for single selection or MultipleCheckbox(1) for multiple selection. function MultipleCheckbox(elem){ i ...

Discovering the page load times of web elements on a website with the help of Selenium WebDriver

On my HTML5 webpage, there are 4 charts that each take a different amount of time to load after the static content has already loaded. The loading process is indicated by a 'rendering' circle image for all 4 charts. I am looking for a way to dete ...

Is it possible for me to utilize the functionality of the "for" attribute in the label?

After searching extensively online with no luck, I found myself wondering if anyone is aware of a resource similar to the one provided by caniuse.com that outlines browser support for the for attribute within a label tag? I am specifically interested in t ...

What is the process for making a local storage item accessible to others on a network?

Can a local storage item be accessed from any computer on the network using the same Google Chrome extension that was previously set up? ...

Insert HTML code that is activated when the selection is modified

I have a simple dropdown menu in my HTML code. HTML <select name="breed" onchange="breedChanged()"> <?php while ($row = gdrcd_query($result, 'fetch')){ ?> <option value="<?php echo $row['id_breed']; ?& ...

Ways to adjust width and height for mobile devices on a website with HTML and CSS

My resume is currently uploaded to a website and looks great on desktop. However, when viewed on mobile, the width shrinks and the height extends, causing text overflow and an overall terrible appearance. I'm looking for a solution that will display m ...

What steps do I need to follow to change the background color to white for the <App/> component in solid-js?

In my solid-js application styled with tailwindcss, I established a gray color in the index.css for the background of various screens. However, I wanted the main <App/> component to have a white background instead. To achieve this, I created an App.c ...

When attempting to utilize drag and drop functionality in HTML5, the error message "DndSimulator is not

Currently, I am using selenium webDriver in C# on a Windows platform to automate tests involving HTML5 drag and drop. In my approach, I rely on the dragTo method from Selenium for achieving this functionality successfully in one test. However, when I attem ...