Tips for integrating CSS keyframes in MUI v5 (using emotion)

Hey there! I'm currently working on adding some spinning text, similar to a carousel, using keyframes in my React app. The setup involves MUI v5 with @emotion. Basically, I want a "box" element to show different words every few seconds with a rotating animation effect. Check out the code snippet below for the keyframes of my spin animation:

import { css, keyframes } from '@emotion/react';
export const Home = () => {
    const spin = keyframes`
        0%, 20%:{
            transform: translateY(0)
        },
        25%, 45%:{
            transform: translateY(-208px)
        },
        50%, 70%:{
            transform: translateY(-416px)
        },
        75%, 95%:{
            transform: translateY(-624px)
        },
        100%:{
            transform: translateY(-832px)
        }
    `;
    const animated = css`
        animation: ${spin} 7s ease-in-out infinite;
    `

Here's what the components look like once returned:

return (<>
        ...
        <div css={animated}>
            <div>restaurants</div>
            <div>musuems</div>
            <div>landmarks</div>
            <div>parks</div>
        </div>
        ...
</>)

My goal is to eventually swap out the div elements with MUI's Box and Typography components. This is my first time working with keyframes, especially when it comes to integrating them with MUI. Any guidance or tips would be highly appreciated :)

Answer №1

Give this code a shot using emotion/styled

import styled from "@emotion/styled";
import { keyframes } from "@emotion/react";

export const Home = () => {
    const spin = keyframes`
        0%, 20%:{
            transform: translateY(0)
        },
        25%, 45%:{
            transform: translateY(-208px)
        },
        50%, 70%:{
            transform: translateY(-416px)
        },
        75%, 95%:{
            transform: translateY(-624px)
        },
        100%:{
            transform: translateY(-832px)
        }
    `;

 const StyledDiv = styled.div`animation: ${spin} 7s ease-in-out infinite`; 
 return (<>
        ...
        <StyledDiv>
            <div>restaurants</div>
            <div>musuems</div>
            <div>landmarks</div>
            <div>parks</div>
        </StyledDiv>
        ...
</>)

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

"Troubleshooting issues with data loading using React's useEffect function

While working on my project, I encountered a strange issue where the isLoading state is being set to false before the data fetching process is completed. I am using the useEffect hook to show a loading spinner while fetching data from an API, and then disp ...

Is there a method available to incorporate a scroller into an nvd3 chart?

I am encountering an issue with my nvd3 chart. When I have a large amount of data that exceeds the width of the chart container, there is no scroll bar present and I'm struggling to figure out how to add one. I attempted to include overflow:scroll wi ...

Adjustable slider height in WordPress

Struggling with the height of the slider on my website. I've tried various CSS adjustments and even tweaked the Jquery, but it still doesn't display properly on mobile devices. For instance, setting a height of 300px looks perfect on desktop brow ...

Month and year selection feature in ExtJS 4 catalog

I recently came across an interesting Fiddle that featured a Month and Year picker for apps. After finding this Fiddle here, I noticed that it was built using Extjs 5.0, and it worked perfectly. However, when attempting to switch it to version 4.2.1, the l ...

Creating a clone of JSON for use as a template

I am working with a json template that I fill with product data. Here is an example of the json structure: // product template $scope.productAttributes = { "Code": null, 'Attributes': {} }; When a user inputs produ ...

Is it possible to manipulate a parent component's state with a child component?

I have been experimenting with Material UI's react modal to create a modal as my child component. In the parent component, I have set up a state that controls the visibility of the modal and added a button inside the modal to close it. However, this s ...

Is there a way to transform an HTML table from a horizontal layout to a vertical

I have reorganized the bus seating layout horizontally using a table. However, I now need to transform it vertically for mobile phones without disrupting my existing code. My attempt at using transform(rotate90deg) achieved the desired result, but the tab ...

Adjust rankings based on the number of upvotes received by a project

I'm facing a challenge with ranking projects based on the number of votes they receive. No matter the vote count, the project always ends up getting ranked as 1. To address this issue, I developed a function to manage the rank count and a return hand ...

Accessing information from a JSON array

I am encountering difficulties in fetching data from a JSON file. Whenever I click the button, it triggers the call function which then retrieves data from the json.jsp file containing an array. However, I am unable to retrieve the data successfully. Below ...

Utilizing flot.js to showcase a funnel chart with an external JSON file as the data source

Trying to create a funnel chart using the Flot.js library with an external JSON file as input. The JSON file is being fetched successfully, but the chart is not being plotted. [ { "data": 10, "label": "a" }, { "data": 81, "label": "b ...

Do you need assistance with downloading files and disconnecting from clients?

When looking at the code snippet below: async function (req, res, next) { const fd = await fs.open("myfile.txt") fs.createReadStream(null, { fd, autoClose: false }) .on('error', next) .on('end', () => fs.close(fd)) . ...

Show product name when hovering over the image

Trying to achieve a hover effect where product titles appear in a contained box when hovering over the product image on the bottom overlay, instead of below the image itself. Tried CSS code recommended by Shopify, but it had no effect: <noscript> ...

Discover how to move around in next.js through the use of HOC and the Router feature in

Currently, I am in the process of creating an HOC (Higher Order Component) to add authentication protection to my project. The main purpose of this HOC is to prevent unauthorized access to certain pages by redirecting users to the login page when necessary ...

What could potentially be the reason behind the incapability of the next.js Image component to transform the svg into a

Unique Context I recently developed a minimalist Hero + Navbar using Next.js. The site utilizes the powerful next.js Image component to display images. Surprisingly, all three images on the website, which are in .webp format, load instantly with a size of ...

Is it possible to dynamically create input fields in AngularJS by clicking a button?

I'm currently working on a project that involves adding an input field and a button each time a user clicks on the button. Is there a way to retrieve the text from the input field when the new button, which is displayed alongside the input field, is ...

When using npm bootstrap in a React project, the JavaScript features may not function properly

I am currently working on a React project and I needed to add a navbar. To achieve this, I installed Bootstrap using the following command: npm install bootstrap After installation, I imported Bootstrap into my index.js file like so: import 'bootstra ...

Styling HTML elements using CSS within PHP echo statements

I am a beginner when it comes to PHP and I'm trying to figure out how to style this table. I tried creating a class, but changing the styles in a separate CSS file didn't work for me. Should I use style tags? How should I go about styling this? ...

Having trouble with your contact form and not getting it to work properly with Javascript, Ajax, or

I've been struggling to get a contact form working for an entire day. I included the following script at the top of my page: <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> This is the structure ...

Create a dynamic select2 field based on the value selected in another dropdown menu

Starting with an initial dropdown where a value needs to be selected: <select id="indexID" name="indexID" class="form-control" onChange="updateSector();" style="width:300px;"> <option value='' selected>Choose an Index</option> ...

Modify the default background color for the chosen element in the tree structure

Could someone assist me in changing the default background color of a selected element in the tree? Below is the XHTML code: <style type="text/css"> .ui-state-highlight,.ui-widget-content .ui-state-highlight,.ui-widget-header .ui-state- ...