Incorporate a smooth infinite scroll feature in a CSS carousel that seamlessly loops without

Looking for a solution to the carousel jerk issue when it reaches the end of the loop? Is there a way to seamlessly loop start without any noticeable interruptions? Here is the code in question. Avoiding the use of JavaScript, is there a method to achieve this effect without making changes based on screen size but ensuring that .marquee-item remains at 580px?

.marquee-wrapper{
  background:#2F394C;
  text-align:center;
}
.marquee-wrapper .container{
  overflow:hidden;
}
.marquee-inner span{
  display:flex;
  gap:40px;
}
.marquee-wrapper .marquee-block{
  --total-marquee-items:5;
  height: 150px;
  width: calc(30% * (var(--total-marquee-items)));
  overflow: hidden;
  box-sizing: border-box;
  position: relative;
  margin: 20px auto;
  background:#1B2531;
  padding: 30px 0;
}
.marquee-inner{
  display: flex;
  width: 200%;
  position: absolute;
}
.marquee-item{
  width: 580px;
  height: auto;
  float: left;
  transition: all .2s ease-out;
  background:#00cc00;
}
.marquee-item:last-child {
  margin: 0 50px 0 20px;
}
.marquee-inner p{
  font-weight: 800;
  font-size: 30px;
  font-family: cursive;
}
.marquee-inner.to-left{
  animation: marqueeLeft 5s linear infinite;
}
.marquee-inner.to-right{
  animation: marqueeRight 5s linear infinite;
}
@keyframes marqueeLeft{
  0% {
    left: 0;
  }
  100% {
    left: -100%;
  }
}
@keyframes marqueeRight{
  0% { 
    left: -100%; 
  }
  100% {
   left: 0; 
  }
}
<!DOCTYPE html>
<html lang="en" >
<head>
  <meta charset="UTF-8">
  <title>CSS Carousel</title>

</head>
<body>

<div class="marquee-wrapper">
    <div class="container">
        <div class="marquee-block">
            <div class="marquee-inner to-left">
                <span>
                    <div class="marquee-item">
                        <p class="text-white">1</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">2</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">3</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">4</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">5</p>
                    </div>
                </span>
                <span>
                    <div class="marquee-item">
                        <p class="text-white">1</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">2</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">3</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">4</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">5</p>
                    </div>
                </span>
            </div>
        </div>
        <div class="marquee-block">
            <div class="marquee-inner to-right">
                <span>
                    <div class="marquee-item">
                        <p class="text-white">5</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">4</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">3</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">2</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">1</p>
                    </div>
                </span>
                <span>
                    <div class="marquee-item">
                        <p class="text-white">5</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">4</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">3</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">2</p>
                    </div>
                    <div class="marquee-item">
                        <p class="text-white">1</p>
                    </div>
                </span>
            </div>
        </div>
    </div>
</div>
  
</body>
</html>

Answer №1

There were 2 significant issues:

  • The problem with the width defined on .marquee-inner being in percentage was that it reflected the parent's percentage. In this case, since the parent had a smaller size than the content, this caused issues. To address this, I implemented a formula to calculate the width of each element, including their spacing, and multiplied it by the number of elements.
  • The animation utilized position:absolute along with left:-100%, which caused problems because percentages are relative to the parent sizes. In this scenario, you were moving left by 100% of the container size instead of the content size. I opted for using a transform for better performance (it's more efficient to use transforms rather than changing position) and accuracy since percentages directly relate to the current element, allowing -100% to function as expected.

As a result, there might be some slight flickering in the animation loop. This occurs when the first and last frames of the animation are identical, making it appear as though the element pauses momentarily. To correct this issue, you would need to subtract the percentage traveled in one frame from 100%. While there are techniques available to mitigate this, I'm not familiar with them.

.marquee-wrapper {
  background: #2F394C;
  text-align: center;
}

.marquee-wrapper .container {
  overflow: hidden;
}

.marquee-inner span {
  display: flex;
  gap: 40px;
}

/* Rest of the CSS code remains the same */

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8>
  <title>CSS Carousel</title>

</head>

<body>

  <!-- HTML markup remains the same -->
  

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

Guide to adding checkbox IDs and inserting them into a textarea

Hello, my name is Prasath and I am currently learning about AJAX. I am working on a project involving seat layouts and finding it challenging to retrieve both the seat fare and seat number simultaneously. Is there anyone who can assist me in resolving this ...

A div element featuring a border with transparent edges on the top right and bottom left corners

Does anyone know how to code the border for the upper right corner and lower left corner of the box (as shown in the image below)? I would really appreciate some help. Thank you in advance! This is the HTML <div class="carouselle"> <div clas ...

Text hidden within the image, each line concealing a separate message

I am having an issue where my image is overlaying the text. I would like the text to wrap around the image and for any hidden text to appear on a different line. How can this be achieved? Here is the CSS code I am using: div.relative { position: relati ...

Discover the magic of Bootstrap 3.0 Popovers and Tooltips

I'm struggling with implementing the popover and tooltip features in Bootstrap. While I have successfully implemented drop downs and modals, the tooltips are not styled or positioned correctly as shown in the Bootstrap examples, and the popover featur ...

Is the background image unable to fill up the entire screen?

I'm currently facing an issue with my webpage's background not covering the entire vertical space. Despite trying different solutions and referring to previous posts, I haven't been able to resolve it yet. Here is the relevant code: <div ...

What is the comparable javascript code for the <script src="something1.json"></script> tag?

Within my HTML code, I currently have the following line to read a JSON file: <script src="something1.json"></script> Inside this JSON file, there is a structure like so: var varName = { .... } This method of definition naturally sets up ...

Troubleshooting the Missing Border Issue in Tailwind Form Fieldset [Code Snippet Included]

I am developing a basic react application with the help of Tailwind css. Scenario 1: Functioning correctly without tailwind: https://codesandbox.io/s/bold-agnesi-y70ue In this example, the tailwind library is not utilized. The simple react app displays ...

Problem with CSS creating the Typewriter effect when the third line of text is added

I've been working on a website for my new company and I want to incorporate a typewriter effect on the main page. I came across a tutorial on Medium.com but I'm having trouble adding an additional span to the provided code. Take a look at the lin ...

Google Chrome extension with Autofocus functionality

I developed a user-friendly Chrome extension that allows users to search using a simple form. Upon opening the extension, I noticed that there is no default focus on the form, requiring an additional click from the user. The intended behavior is for the ...

Objective subject for an element within a :not operation

I have a function that specifically excludes a style on links by targeting their IDs. Each of my links has an ID, so I use that to exclude the style. a:not([id='hopscotch_logo'] { color: red; } Now, I also want to find links that are children o ...

Tips for applying unique CSS classes to individual templates in Joomla

I manage a website at www.kadamjay.kg using an RT template system where each language version has its own unique template. However, when I add a class to a specific element, it only changes on one template and there are no additional templates in the pat ...

Expansive Child Division with Ample Margins

I'm currently working with a nested set of divs and I need the inner div to occupy the full width without extending beyond the parent div's margins. Despite trying to use max-width:100%, it hasn't been successful so far. For this particular ...

AngularJs is being used to extract data from Firebase with the help of $value, $id, and

I have been working on retrieving data from Firebase in my HTML using AngularJS. Everything is functioning well, however, when I access the child node, the data is displayed in an unexpected format. Please refer to the images below for more details: This ...

Learn how to efficiently apply styles to multiple objects within an array using the material-ui library

Currently, I'm utilizing the functional component of React.js. Within this component, I have an array of objects that contain specific styles. const data = [ { id: 1, color: '#000' }, { ...

Is it possible for me to input a variable into the logical process of if(isset($_FILES['whatever']))?

I have recently started learning PHP and I'm in the process of creating a dynamic page that will update based on which form buttons are clicked. The functionality is working correctly, but my goal is to enable users to upload multiple files - either P ...

Animating scrollTop using JQuery

There are two buttons with hidden parts related to each. When a button is clicked, its associated part will be displayed and the document will scroll to the top. Issue: The displayed part does not move to the top as intended. Your assistance in resolving ...

Enhance User Experience - Automatically highlight the first element in Prime NG Menu when activated

I have been working on transitioning the focus from the PrimeNG menu to the first element in the list when the menu is toggled. Here is what I've come up with: In my template, I added: <p-menu appendTo="body" #menu [popup]="true&quo ...

How to correct header alignment in HTML for Google Table

Utilizing the google visualization table to generate a html table, I have successfully fixed the top section of the html using the stuckpart div. This ensures that regardless of how I scroll, the button remains in place. However, I now aim to fix the table ...

What is the method for saving background color and font size preferences in local storage using HTML?

I want to give visitors to my site the option to customize the background color and text size. Can their preferences be saved in local storage? If so, could you share some HTML code to make this possible? ...

Choose the first and last div element in the selection

Is there a way to target the first and fourth div elements using the :nth-child selector for styling purposes? .main{ width:460px; height:240px; } .box{ width:100px; height:100px; background:red; float:left; margin:10px; } /*I tried */ . ...