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

HTML: Ensure that a user fills out a minimum of one text box before submission

<tr><td> First Name: </td><td><input type="text" name="FirstName" required></td> </tr> <tr><td> Last Name: </td><td><input type="text" name="LastName" required> </td></tr> ...

"I'm experiencing an issue where my JSON data is not displaying in the browser when I run the code

I am having trouble displaying my json data in the browser. This is my first time working with json and I can't seem to identify the issue. I tried researching online and found that it could be related to mime types, but I still can't solve it. B ...

Overlay a semi-transparent gray layer onto the background color

My Table has various TDs with different background colors, such as yellow, red, green, etc., but these colors are not known beforehand. I am looking to overlay a semi-transparent gray layer on certain TDs so that... The TD with a yellow background will t ...

Adjust the content size to 100% based on the quantity of items in a row

I've been having a hard time creating an image gallery that adjusts its size automatically (width: 100%) based on the number of items it contains. For example, take a look at this gallery: http://codepen.io/anon/pen/eNMOEz .item { width: 75px; h ...

Jquery submenu accordion toggles open and closed with each click

My website has a hamburger menu with an accordion-style submenu implemented using jQuery for devices 1084px and smaller. For larger devices, the menu utilizes a hover-style submenu on desktop. However, I encountered issues when trying to use both the hove ...

Ways to find the image source using JavaScript/Jquery upon page loading?

I've scoured countless forums, yet a viable solution still eludes me. My objective is simple - upon page load, I want to gather all elements with the ".home" class and store them in an array called arr. Subsequently, the script should iterate through ...

What is the best way to ensure that an image functions as a clickable hyperlink upon hovering over it?

I am facing a challenge in making an image function as a link. Unfortunately, I have identified the problem, but finding a solution is proving to be quite elusive. When the cursor hovers over the image, it undergoes a slight darkening effect and some word ...

Transforming HTML content in real-time using Express.js

I'm a little uncertain if I understand the concept of Express MVC correctly: If my goal is to create a single page application and dynamically modify the HTML, can Express assist me with this? Or will I only be able to work with static pages that req ...

Adding an image within the body of text in a Django model, where both the text and image coexist

I am currently seeking a method to seamlessly insert an image within the text of my Django-powered blog. My goal is to achieve a layout similar to the one showcased in this example: https://i.stack.imgur.com/cFKgG.png The desired layout consists of two c ...

Arrange Raphael Objects in Their Relative Positions

I've been experimenting with Raphael.js recently and I've encountered an issue related to the positioning of each Raphael object. My goal is to create multiple 'canvases' without having them overlap within a predefined div on the page. ...

Issue with Bootstrap checkbox buttons not rendering properly

I'm attempting to utilize the checkbox button feature outlined on the bootstrap webpage here in the "checkbox" subsection. I copied and pasted the html code (displayed below) from that page into a jsfiddle, and checkboxes are unexpectedly appearing in ...

Error arising from CSS positioning with dynamically generated images

My challenge is displaying dynamic images from a database alongside details like color, name, and size, with a hover effect that shows a border. Whenever I hover over one image, the other images shift to the right or left. I want them to stay in place. T ...

Having trouble getting your Raspberry Pi web server to display the background image?

Recently, I successfully set up a LAMP server on my raspberrypi which was quite exciting. One of the first things I did was configure an FTP server to transfer webpage files to the Pi. I managed to create a basic form of the final webpage and transferred i ...

Trouble displaying certain HTML code and its output on the browser?

I am in the process of developing a user profile page. I have designed a form with various fields, but the code within the section is not displaying correctly in the browser. I attempted to inspect the elements, but the code within that section remains inv ...

The swf file doesn't stretch to fit the window when the height is set to 100%

Struggling with creating a flash recorder controlled by JavaScript? Trying to get the flash to fill the browser window but disappearing when setting height or width to 100%? Where am I going wrong? <div id="flashrecorder"> <object wmode="trans ...

Instructions for creating a background within a container and positioning text in the center of the image utilizing HTML and CSS

I am currently learning HTML, CSS, and JavaScript. I am in the process of building my very first website, but I have encountered a problem. I am struggling to change the background color within the container beneath the images to a specific color (#82b5cf) ...

Utilizing Django to recycle an HTML block for multiple for loops within the same template

I have three specific states for an object - 1. to start, 2. started, and 3. finished. Upon filtering the objects in view, I pass three variables to the template - tostart_objects, started_objects, and finished_objects. My current approach involves loop ...

Timepicker Bootstrapping

I've been searching for a time picker widget that works well with Bootstrap styling. The jdewit widget has a great style, but unfortunately it comes with a lot of bugs. I'm on a tight deadline for my project and don't have the time to deal w ...

I am having trouble with the Primeng password template suggestions not appearing as expected

The demonstration available at this link is not functioning correctly, unlike the example provided in the documentation at this website. In the StackBlitz demo, the suggestions are not being displayed as expected for the password template. Take a look at ...

Is it possible to deselect a checkbox if a radio button is selected, and vice versa?

I'm presenting these two options: <div class="pricing-details pricing-details-downloads"> <h4>Single purchase (60 lessons)</h4> <h4>Bulk Purchase: Lesson</h4> <div class="pricing-details-separator">&l ...