Resetting CSS animations using animation-delay

My aim is to animate a series of images on my landing page using Next.js and SCSS. The issue arises when I try to add dynamic animations that reset after each cycle. The delay in the animation causes the reset to also be delayed, which disrupts the flow. Is there a way to include the delay within the running animation cycle, so that when it reaches the end, all elements reset simultaneously before adhering to the delay again? Below is the code snippet I am working with:

Carousel.js:

import Image from 'next/image';
import styles from '@/styles/components/LandingCarousel.module.scss';

export default function LandingCarousel() {

    const images = [
        { src: '/NetworkingSkeleton.png', delay: 6 },
        { src: '/NetworkingSkeleton.png', delay: 7 },
        { src: '/NetworkingPreview.png', delay: 8 },
        { src: '/NetworkingSkeleton.png', delay: 9 },
        { src: '/NetworkingSkeleton.png', delay: 10 },
        { src: '/NetworkingSkeleton.png', delay: 11 },
        { src: '/NetworkingSkeleton.png', delay: 12 },
        { src: '/NetworkingPreview.png', delay: 13 },
        { src: '/NetworkingSkeleton.png', delay: 14 },
        { src: '/NetworkingSkeleton.png', delay: 15 },
    ];

    const listingImages = [
        { src: '/ListingSkeleton.png', delay: 6 },
        { src: '/ListingSkeleton.png', delay: 7 },
        { src: '/ListingPreview.png', delay: 8 },
        { src: '/ListingSkeleton.png', delay: 9 },
        { src: '/ListingSkeleton.png', delay: 10 },
        { src: '/ListingSkeleton.png', delay: 11 },
        { src: '/ListingPreview.png', delay: 12 },
        { src: '/ListingSkeleton.png', delay: 13 },
    ];

    return (
        <div className={styles.section}>
            <div className={styles.container}>
                {images.map((image, index) => (
                    <img
                        key={index}
                        src={image.src}
                        className={styles.animated_image}
                        style={{ '--delay': image.delay }}
                    />
                ))}
            </div>
            <div className={styles.containerRight}>
                {listingImages.map((image, index) => (
                    <img
                        key={index}
                        src={image.src}
                        className={styles.animated_imageRight}
                        style={{ '--delay': image.delay }}
                    />
                ))}
            </div>
        </div>
    );
}

LandingCarousel.module.scss:

.section {
    display: flex;
    height: 87vh;
    overflow: hidden;
    align-items: center;

    .container {
        display: flex;
        flex-direction: column;
        height: calc(400px * 5);

        .animated_image {
            display: flex;
            position: relative;
            max-height: 400px;
            object-fit: cover;
            animation: scroll 8s ease-in-out infinite;
            -webkit-animation: scroll 8s ease-in-out infinite;
            // animation-delay: calc(var(--delay) * 0.05s);
            // -webkit-animation-delay: calc(var(--delay) * 0.05s);
        }
    }

    .containerRight {
        display: flex;
        flex-direction: column;
        height: calc(380px * 4);

        .animated_imageRight {
            display: flex;
            max-height: 380px;
            object-fit: cover;
            animation: scrollRight 8s ease-in-out infinite;
            -webkit-animation: scrollRight 8s ease-in-out infinite;
            // animation-delay: calc(var(--delay) * 0.05s);
            // -webkit-animation-delay: calc(var(--delay) * 0.05s);
        }
    }
}

@keyframes scroll {

    0% {
        -webkit-transform: translateY(0);
        transform: translateY(0);
    }

    50% {
        -webkit-transform: translateY(-400px * 5);
        transform: translateY(-400px * 5);
    }

    100% {
        -webkit-transform: translateY(-400px * 5);
        transform: translateY(-400px * 5);
    }
}

@keyframes scrollRight {

    0% {
        -webkit-transform: translateY(0);
        transform: translateY(0);
    }

    50% {
        -webkit-transform: translateY(-380px * 4);
        transform: translateY(-380px * 4);
    }

    100% {
        -webkit-transform: translateY(-380px * 4);
        transform: translateY(-380px * 4);
    }
}

Answer №1

My suggestion would be to manipulate animations using JavaScript, handling delays and repeat intervals, adding or removing classes from parent elements, and utilizing these classes to trigger animations.

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

Sneak beneath another element with a div's sliding movement

I'm attempting to make an absolutely positioned div element (#slideout) slide underneath another div element .menu using CSS, if possible. Check out my code here. Currently, when the red tab is clicked, #slideout covers .menu, but I want it to hide ...

Guide on accessing a local image while setting the background image using JavaScript

I am trying to set a background image from a local source on my computer. Below are two lines of code, one that works and one that does not: (the local one fails) _html.style.backgroundImage = 'url("urlsourceblahblahblah")'; _html.style.backgro ...

The issue of transform scale not functioning properly in conjunction with background clip and gradients in CSS

Looking to transform a div with the transform: scale(-1,1) property while using background-clip: text on the text within it. However, this causes the text to disappear. Here's what I've attempted: .reverse { transform: scale(-1, 1); } .gr ...

Refresh the current page with jQuery Mobile when it is clicked

I have a multi page template in jQuery Mobile. How can I refresh the current page when clicking on a hyperlink or button? I am using JQM version 1.4.5 Despite trying the code suggested in how to refresh(reload) page when click button in jQuery Mobile, it ...

Improving efficiency of basic image carousel in Angular 8

In my Angular 8 app, I am developing a basic carousel without relying on external libraries like jQuery or NgB. Instead, I opted to use pure JS for the task. However, the code seems quite cumbersome and I believe there must be a more efficient way to achie ...

The site is visually appealing in Firefox, but has a few issues on Chrome

I've encountered a common issue where my website displays perfectly in Firefox but looks terrible in Dreamweaver and Chrome. To fix it, I made live CSS edits in Firefox then copied them to Dreamweaver resulting in a mess. Here's how it should lo ...

Struggling with evenly positioning 6 elements in two rows using CSS

For positioning 6 different elements on a webpage, I've experimented with various methods: I initially tried stacking two unordered lists vertically but faced issues with scaling when stretching the page. Another attempt was using a table, but I stru ...

Select a specific element to apply a CSS selector for a button

One of the challenges I am facing involves a webpage with multiple submit buttons. My goal is to iterate through each button and click on them one by one. While I am aware that this can be achieved using xpath like this (//button[@class='submit' ...

The functionality of display flex is not functioning as expected outside of the CodePen

I attempted to create a responsive two-column layout on Codepen with success. The layout is quite simple, featuring a YouTube video and some text. However, when I tried to transfer this to my website, it failed to work... Here is the code – hoping someo ...

Tricks for refreshing cached web page assets in the year 2023

So far, here are some of the old Cache Buster techniques I've come across: Adding a query string in the link src: /mstylesheet.css?cache_buster=12345 Changing the filename each time: /mstylesheet-12345.css Using Apache with Cache-Control "must-revali ...

What is the best way to format each <li> element onto a separate line?

Creating a review section for an item on my website required me to organize the reviews and comments. I utilized the following code structure: <ul> <li> Comment code </li> <li> Comment code </li> </ul> Within the ...

Encountering an Internal Server Error 500 on Windows IIS while running a Next.js application

I am currently facing an issue while attempting to deploy my Next.js application on a VPS running Windows 10 with the Plesk control panel and Windows IIS. The problem arises after creating a domain name through the Plesk panel, as it works perfectly. Howe ...

Placing two tables in an HTML document

I am working on integrating two tables into my web application and I would like them to be positioned on the same row horizontally. <span><table><tr><td>A</td><td>B</td></tr></table></span> <s ...

Error in Prisma: Unable to retrieve data due to undefined properties (attempting to access 'findMany')

Recently, I've been working on a dashboard app using Prisma, Next.js, and supabase. Encountering an issue with the EventChart model in schema.prisma, I decided to create a new model called EventAreaChart. However, after migrating and attempting to ex ...

Solution for accessing the callee function in JavaScript slide down operation

While exploring a tutorial from CSS Tricks about animating section height, I came across a solution that I would like to implement in my Angular 2 application. Here is the function responsible for expanding sections in my app: expandSection(element) { / ...

Tips for adjusting div content to fit a fixed height on all devices

Looking to adjust the size of the #left-content div based on the height of the window, while ensuring that all content is visible within the fixed #left-bg. However, when viewing on mobile or tablet devices, the content appears hidden. .left-bg{ backgro ...

Highlighted text with CSS

For this exercise, I was provided with the following HTML code: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Selected text</title> </head> <body> <div class ...

Why does my Next.js server abruptly stop after I start it up?

After creating a new app with create-next-app@latest, I encountered an issue where the server would start and then close automatically when running npm run dev. The output displayed was as follows: > <a href="/cdn-cgi/l/email-protection" class="__cf ...

Safari is experiencing issues with overflow-x functionality after a device rotation

I'm currently in the process of developing a website that requires a specific div element to be horizontally scrollable if its content exceeds the screen size. This functionality works smoothly on most browsers, except for Safari: Scenario: When the ...

Having trouble getting the TailwindCSS container query to work with dynamically added elements

Trying to change Tailwind CSS queries into container queries where the className string is modified and placed back. The problem is that it's not being set to the component. Other styles work fine, just the container queries aren't working as exp ...