Infinite Scrolling Animation CSS

Infinite Scrolling Animation CSS
Code Snippet:Progressively enhanced infinite horizontal scroll
Author: Kevin
Published: January 20, 2024
Last Updated: January 22, 2024
Downloads: 2,357
License: MIT
Edit Code online: View on CodePen
Read More

This code implements an Infinite Scrolling Animation using CSS. It checks if the user has reduced motion preferences and adjusts accordingly. If not, it duplicates content for animation. It offers control over animation speed and direction, enhancing the user experience.

You can use this code for websites or web applications to create visually appealing, infinite scrolling animations. It enhances user engagement and provides a dynamic look to your content, improving the overall user experience. It offers customization options for animation speed and direction, making it versatile for various design needs.

How to Create Infinite Scrolling Animation CSS

1. First, load the following CDN link into the head tag of your HTML document.

<link rel='stylesheet' href='https://codepen.io/kevinpowell/pen/BavZQjN/9451cb2bddcecda34df2a0f718703cbc.css'>

2. Copy and paste the HTML code into your web page. This code sets up the structure for the scrolling animation and contains the content you want to scroll.

If you want to customize the animation, you can adjust the data-speed and data-direction attributes in the HTML. data-speed="fast" or data-speed="slow" controls the animation speed, while data-direction="right" changes the animation direction.

<!-- 
   PROGRESSIVELY ENHANCED
   If a user has `prefers-reduced-motion: reduced`, there will be no animation
   and the items will wrap, instead of being hidden.
   If they have not opted for reduced motion, the items will be duplicated with JS
   and the duplicated content will have `aria-hidden="true"` to prevent duplicate content
   for screen readers.
   If a user has JS disabled or it fails for whatever reason, they will get the same 
   experience as a user with `prefers-reduced-motion: reduced`, so no content is hidden,
   and there is no animation.
   
   === OPTIONS ===
   CONTROL SPEED 
   If you don't assign anything, it will use a default speed.
   To change the speed, on the `.scroller`
   you can use `data-speed="fast"` or `data-speed="slow"

   CONTROL DIRECTION 
   By default, it will scroll from right to left.
   To change the direction, on the `.scroller`
   you can use `data-direction="right"` (`data-direction="left" also works, but it is the default) 
-->

<h1 style="text-align: center">Infinite Scroll Animation</h1>

<div class="scroller" data-speed="fast">
  <ul class="tag-list scroller__inner">
    <li>HTML</li>
    <li>CSS</li>
    <li>JS</li>
    <li>SSG</li>
    <li>webdev</li>
    <li>animation</li>
    <li>UI/UX</li>
  </ul>
</div>

<div class="scroller" data-direction="right" data-speed="slow">
  <div class="scroller__inner">
    <img src="https://i.pravatar.cc/150?img=1" alt="" />
    <img src="https://i.pravatar.cc/150?img=2" alt="" />
    <img src="https://i.pravatar.cc/150?img=3" alt="" />
    <img src="https://i.pravatar.cc/150?img=4" alt="" />
    <img src="https://i.pravatar.cc/150?img=5" alt="" />
    <img src="https://i.pravatar.cc/150?img=6" alt="" />
  </div>
</div>

<a class="yt" href="https://youtu.be/pKHKQwAsZLI">
  Watch the tutorial
</a>

3. Now, add the following CSS code to your project. It is essential for styling and controlling the animation. You can customize the colors, fonts, and other styles to match your website’s design.

.scroller {
  max-width: 600px;
  margin: 20px auto
}

.scroller__inner {
  padding-block: 1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
}

.scroller[data-animated="true"] {
  overflow: hidden;
  -webkit-mask: linear-gradient(
    90deg,
    transparent,
    white 20%,
    white 80%,
    transparent
  );
  mask: linear-gradient(90deg, transparent, white 20%, white 80%, transparent);
}

.scroller[data-animated="true"] .scroller__inner {
  width: -webkit-max-content;
  width: -moz-max-content;
  width: max-content;
  flex-wrap: nowrap;
  -webkit-animation: scroll var(--_animation-duration, 40s)
    var(--_animation-direction, forwards) linear infinite;
          animation: scroll var(--_animation-duration, 40s)
    var(--_animation-direction, forwards) linear infinite;
}

.scroller[data-direction="right"] {
  --_animation-direction: reverse;
}

.scroller[data-direction="left"] {
  --_animation-direction: forwards;
}

.scroller[data-speed="fast"] {
  --_animation-duration: 20s;
}

.scroller[data-speed="slow"] {
  --_animation-duration: 60s;
}

@-webkit-keyframes scroll {
  to {
    transform: translate(calc(-50% - 0.5rem));
  }
}

@keyframes scroll {
  to {
    transform: translate(calc(-50% - 0.5rem));
  }
}

/* general styles */

:root {
  --clr-neutral-100: hsl(0, 0%, 100%);
  --clr-primary-100: hsl(205, 15%, 58%);
  --clr-primary-400: hsl(215, 25%, 27%);
  --clr-primary-800: hsl(217, 33%, 17%);
  --clr-primary-900: hsl(218, 33%, 9%);
}

html {
  color-scheme: dark;
}

body {
  display: grid;
  min-block-size: 100vh;
  place-content: center;
  font-family: system-ui;
  font-size: 1.125rem;
  background-color: var(--clr-primary-800);
}

.tag-list {
  margin: 0 auto;
  padding-inline: 0;
  list-style: none;
}

.tag-list li {
  padding: 1rem;
  background: var(--clr-primary-400);
  border-radius: 0.5rem;
  box-shadow: 0 0.5rem 1rem -0.25rem var(--clr-primary-900);
}

/* for testing purposed to ensure the animation lined up correctly */
.test {
  background: red !important;
}

4. The JavaScript code ensures the animation runs smoothly. If you have users who prefer reduced motion, it won’t add animation, but if not, it duplicates the content for the scrolling effect.

const scrollers = document.querySelectorAll(".scroller");

// If a user hasn't opted in for recuded motion, then we add the animation
if (!window.matchMedia("(prefers-reduced-motion: reduce)").matches) {
  addAnimation();
}

function addAnimation() {
  scrollers.forEach((scroller) => {
    // add data-animated="true" to every `.scroller` on the page
    scroller.setAttribute("data-animated", true);

    // Make an array from the elements within `.scroller-inner`
    const scrollerInner = scroller.querySelector(".scroller__inner");
    const scrollerContent = Array.from(scrollerInner.children);

    // For each item in the array, clone it
    // add aria-hidden to it
    // add it into the `.scroller-inner`
    scrollerContent.forEach((item) => {
      const duplicatedItem = item.cloneNode(true);
      duplicatedItem.setAttribute("aria-hidden", true);
      scrollerInner.appendChild(duplicatedItem);
    });
  });
}

Feel free to modify the HTML content inside the .scroller__inner elements to display your own text, images, or other elements. You can also tweak the animation duration and styles to match your website’s aesthetics.

That’s all! hopefully, you have successfully created an Infinite Scrolling Animation HTML, CSS, and JavaScript. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X