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

Issue with submitting form using Jquery on select action

There seems to be an issue with this jquery code that is supposed to trigger a form submit when a drop down select is changed. It works fine on our test site, but once we move it to the live website, it breaks. Can someone help us troubleshoot and identify ...

Is it possible to horizontally center an auto-fill grid using only CSS?

I would like to fill my page horizontally with as many blocks as possible and center the result. My goal is to have a grid that can resize when the window size changes: wide window xxx small window xx x Is it possible to achieve this without using Java ...

Is it possible to display a Twitter feed within a Bootstrap popover?

Is it feasible to showcase a Twitter feed within a Bootstrap popover? For instance, when the Twitter button is clicked, I aim to have my website's Twitter feed appear inside a Bootstrap popover. I have attempted the code below without success. Any a ...

What causes a slow disappearance of both the form element and its child elements when the visibility attribute is set to hidden on the

Have you ever noticed that the child elements of an element form hide slowly when using visibility:hidden, but hide quickly when using display:none? This slow hiding can negatively impact user experience. I tried to find information on this issue, but eve ...

What steps can I take to stop search engines from crawling and indexing one specific page on my website?

I'm looking for a way to prevent search engines from indexing my imprint page. Is there a method to achieve this? ...

Modifying the appearance of select components in React MUI using CSS

Can someone help me modify the appearance of the react material UI select component to match this design: I have already applied the following CSS styling: const styles = { width:'250px', border:'1px solid gray', borderBot ...

turning every input field's border to red if none of them were filled out at least once

I struggle with javascript and need some help. I have a form with multiple input fields, and I want to ensure that the user fills in at least one of them. I found code that triggers an alert message if the user does not fill in any fields, but I would pref ...

Getting data in a ng-Dialog Modal with AngularJS is a breeze!

Hi there, I'm new to Angular JS and facing an issue with displaying data from my MySQL database in a table as well as in a modal for more detailed information: I've included the HTML file named _detail_modal.html at the bottom of the page. ...

Assistance with HTML and CSS to position an image in the corner

I need assistance with positioning a corner image on top of another image. I want the span, which has a small background image, to be in the top left corner of the main image. Below is my code: <div id="wrapkon"> <span style="width: 4px; height: ...

What is the best way to position text next to an image?

My dilemma involves aligning images from left to right with text beside each image. Here is a snippet of the code I used: <div> <p style="float: left;"><img src="images/pic1.jpg" height="141px" width="212px" border="0px"></p> & ...

pass the HTML output back to Python

I am currently working on a local website using Python to create a button that will open the door to my room from my phone through a Raspberry Pi. I have already created a Python program that successfully opens the door, but now I am trying to implement an ...

I require limitless onclick functionality, but unfortunately, transitions are not functioning as expected

I'm currently working on creating a dynamic photo gallery, but I've encountered a couple of issues that need to be addressed... The "onclick" function in my JavaScript only allows for a single click, whereas I need it to be able to handle mul ...

The sticky navigation bar allows for the overlapping of additional div elements

Here is the HTML AND SCSS(SASS) code for a NAVBAR: <div id="navbar"> <ul> <li><a href="#home">Home</a></li> <li><a href="#main">Main</a></li> ...

In this guide, learn the best way to dynamically adjust image height to match the height of any device

I am trying to make sure that the image on the front page of my website fits the height of the screen or device, and resizes responsively without cropping when the device height changes. How can I achieve this? If you need a visual example of what I mean, ...

Enhancing D3 visualization with centralized alignment and mobile-friendly responsiveness

I am still quite new to JavaScript and development in general, so I may be overlooking something obvious. Although I have successfully created a chart using d3, I am struggling with positioning. No matter how much I manipulate it with CSS, the chart just d ...

I've been attempting to upload application/pdf files, but I'm having trouble. Can you provide me with instructions

'<?php $allowedExts = array("jpg", "jpeg", "gif", "png"); $extension = end(explode(".", $_FILES["file"]["name"])); if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/ ...

What is the reason CURL is unable to retrieve the HTML content of a page?

Check out the code snippet below: <?php $url = "https://www.facebook.com/login/identify?ctx=recover&lwv=110"; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, C ...

The div background image in Vue.js is displaying a 404 not found error

I've encountered an issue while trying to set a background for a div. Strangely, when using the same relative path with an img tag everything works fine, but as soon as I try to achieve the same result with a div, I receive a 404 error. This img tag ...

What is the best way to use PHP in order to retrieve the element containing the visit ID from the specific row within an HTML table?

I am working on a project that involves populating an HTML table with data from a database. The table includes an approval button for administrators to approve visits and change their status to APPROVED. I am trying to figure out how to select the element ...

Issues with Laravel 8's datatable scripts causing dysfunction

In my practice with the AdminBSB template, I am facing an issue where the Jquery datatable plugin JS is not functioning properly in my blade template. I aim to achieve a datatable layout similar to this example: https://i.sstatic.net/krfPl.jpg However, t ...