What is the best way to create an animation where every letter in a word transitions to a different

Is there a way to animate a word so that each letter changes color within a range of 7 colors, with all letters displaying different colors simultaneously in an infinite loop?

<div class="box">
  <h1 class="logo animate__animated animate__bounceInDown">
    <span class="cake d">D</span>
    <span class="cake y">y</span>
    <span class="cake n">n</span>
    <span class="cake a">a</span>
    <span class="cake m">m</span>
    <span class="cake i">i</span>
    <span class="cake x">x</span>
  </h1>
</div>

The HTML code above separates the word using span tags and classes for each letter.

:root{
  --primary: #22D2A0;
  --secondary: #192824;
  --back: #192824;
  --green: #1FC11B;
  --yellow: #FFD913;
  --orange: #FF9C55;
  --red: #FF5555;
  --aqua: #00ffff;
  --color: #fbff00;
  --blue: #4d1461;
  text-align: center;
} 

I am looking for a solution to cycle through each letter of the word using a different color every millisecond with minimal code. I have attempted to animate each letter individually, but it resulted in bulky code. Additionally, if there is a way to pause the animation on hover using JavaScript, that would be ideal.

Answer №1

To achieve this effect, follow these steps:


.h1{
    height: 200px;
    width: 400px;
    border: 1px solid #000;
    -webkit-animation: animate_bg 5s;
    animation: animate_bg 5s;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
}

@keyframes animate_bg {
    0%   {background:#22D2A0;}
    10%  {background:#192824;}
    20% {background:#192824;}
    30% {background:#1FC11B;}
    40% {background:#1FC11B;}
    50% {background:#FFD913;}
    60% {background:#FF9C55;}
    70% {background:#FF5555;}
    80% {background:#00ffff;}
    90% {background:#fbff00;}
    100% {background:#4d1461}
}

@-webkit-keyframes animate_bg {
    0%   {background:#22D2A0;}
    10%  {background:#192824;}
    20% {background:#192824;}
    30% {background:#1FC11B;}
    40% {background:#1FC11B;}
    50% {background:#FFD913;}
    60% {background:#FF9C55;}
    70% {background:#FF5555;}
    80% {background:#00ffff;}
    90% {background:#fbff00;}
    100% {background:#4d1461}
}

Alternatively, you can modify the code by changing background to color

.h1{
    -webkit-animation: animate_bg 5s;
    animation: animate_bg 5s;
    -webkit-animation-iteration-count: infinite;
    animation-iteration-count: infinite;
}

@keyframes animate_bg {
    0%  {color:#22D2A0;}
    10% {color:#192824;}
    20% {color:#192824;}
    30% {color:#1FC11B;}
    40% {color:#1FC11B;}
    50% {color:#FFD913;}
    60% {color:#FF9C55;}
    70% {color:#FF5555;}
    80% {color:#00ffff;}
    90% {color:#fbff00;}
    100% {color:#4d1461}
}

@-webkit-keyframes animate_bg {
    0%  {color:#22D2A0;}
    10% {color:#192824;}
    20% {color:#192824;}
    30% {color:#1FC11B;}
    40% {color:#1FC11B;}
    50% {color:#FFD913;}
    60% {color:#FF9C55;}
    70% {color:#FF5555;}
    80% {color:#00ffff;}
    90% {color:#fbff00;}
    100% {color:#4d1461}
}

Answer №2

Utilizing JavaScript

  1. Start by creating an array containing various colors
  2. Iterate through each letter in a word and assign a color from the array based on its index position
  3. Repeat this process every 100ms, shifting the starting color index (indexOffset) to apply different colors to each letter in every iteration

let colors = [
 '#1fc11b',
 '#ffd913',
 '#ff9c55',
 '#ff5555',
 '#00ffff',
 '#fbff00',
 '#4d1461',
]

let indexOffset = 0

setInterval(() => {
  document.querySelectorAll('h1 span').forEach((letter, index) => {
    letter.style.color = colors[(index + indexOffset) % 7]
    // '% 7' ensures that the index stays within the bounds of the color array
  })
  indexOffset++
}, 100)
<h1>
  <span>Q</span>
  <span>u</span>
  <span>i</span>
  <span>r</span>
  <span>k</span>
  <span>y</span>
  <span></span>
</h1>

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

Is there a method in React Native to include the "share" icon in my drawer instead of a button?

How can I add a "share" icon to my drawer? I attempted to insert an icon using <Ionicons name="md-share" size={24} color="black" /> instead of a Button. However, when I do this, the icon is displayed without any title, unlike what I see in the sett ...

Tips for handling errors in ajax calls with alternative promises

While working on an application that offers weather data based on user location coordinates, I created the code provided below (also accessible in this codepen http://codepen.io/PiotrBerebecki/pen/QNVEbP). The user's location information will be retr ...

Uncertain about the distinction between reducers and dispatchers when it comes to handling actions

I'm feeling a bit confused regarding reducers and dispatchers. While both receive actions as parameters, it doesn't necessarily mean that the actions I use in my dispatchers are the same as those used in my reducers, correct? For example, if I h ...

Every time I refresh the app, I am consistently redirected back to the home route "//"

I'm facing an issue where, after logging in successfully, I get redirected to the homepage. However, when I refresh the page from any route other than the homepage, such as "/products", I always end up getting redirected back to "/". This is what my ...

Can JavaScript be utilized to retrieve the MAC address?

Is it possible to retrieve the Mac addresses of a system for login using JavaScript? ...

Why is the 3D CSS transform (translateZ) feature malfunctioning on Android 4.0.3?

I have this HTML code that is functioning well on Chrome, Safari, and Mobile Safari. However, when testing it on Android 4.0.3, the translateZ property does not seem to have any desired effect on the div element. While everything else works as expected, ...

How can you efficiently identify and resolve coding errors or typos in your code?

While practicing node.js and express.js, I encountered an issue with finding typos. One such instance was when I mistakenly typed: const decoded = jwt.veryfy(token, config.get('jwtSecret')); instead of jwt.verify. Even though I eventually disc ...

A guide to smoothly transition box-shadow with jQuery's addClass() function

UPDATED I have a div with the class .shadow-circle-lg: <div class="shadow-circle-lg"></div> To add a different border styling to the div, I am using another class called .circle-highlight: .circle-highlight{ box-shadow: 0 0 0 1px rgba(0,0, ...

I'm having trouble getting jQuery to work on a page loaded on an embedded device's browser. How can I troubleshoot this issue?

In the process of creating a web application that will only run on a specific embedded device with its own web browser. According to the documentation, this device's browser is similar to Firefox but not an exact match. The plan was to utilize the j ...

Using a jQuery modal to submit a button and update data in the window

In my current project, I have set up a jQuery modal that appears when the user clicks submit on a form. This modal serves as a confirmation window displaying all the data entered in the form. It contains two buttons - "Go" and "Cancel". If the user clicks ...

"The useTheme hook is not functioning as expected when used within a className

I've defined a custom theme in my App.js file like this: let theme = createTheme({ palette: { bgCells: { textAlign: 'right', color: '#8ccadf', backgroundColor: '#eef7ff' } } }); In another c ...

Animate the React Bootstrap modal only when it is shown or hidden

I am experimenting with the idea of transitioning smoothly from one modal to another in bootstrap Modals for react (using react-bootstrap). However, I am facing challenges due to the fade CSS class causing flickers and unwanted animations. My goal is to h ...

iPhone Safari Mobile Browser experiencing issues with sms: and mailto: functionalities

Issue: Encountering a problem with web pages containing sms: and mailto: links not functioning on iOS mobile Safari browser. Clicking on the link redirects to: Safari cannot open the page because it cannot redirect to locations starting with "sms:& ...

Full image with stylish CSS text overlay

I'm having trouble displaying an overlay text that should look like: This is what I've tried: <div class="row"> <div class="col-md-3 program-cat"> <img src="<?php echo EF_THEME_BASE_URL; ?>/image ...

Angular protection filter

Every time I used Angular CLI to generate a new component, it would create the component with standard parameters: @Component({ selector: 'app-test1', templateUrl: './test1.component.html', styleUrls: ['./test1.component.css ...

Tips for selecting specific elements from an array with jquery

Is there a way to efficiently fetch specific elements from an array using jQuery? For example, if we have an array like this: var arr = [Obj1, Obj2, Obj3, Obj4, Obj5, Obj6] And we want to select objects with index 3 and 5. Is there a function or method t ...

Is there a way to restrict access to my website to only be opened in the Chrome browser?

Is there a way to prevent my web application from loading when the link is opened in browsers other than Chrome? Can this be achieved using Javascript or Java? I want to restrict the usage of my web application to only Chrome. Any assistance would be appre ...

unable to display images from a folder using v-for in Vue.js

Just getting started with Vuejs and I have two pictures stored on my website. The v-for loop is correctly populating the information inside databaseListItem. The path is /opt/lampp/htdocs/products_db/stored_images/cms.png https://i.stack.imgur.com/969U7.p ...

Vanilla JavaScript code that utilizes regex to transform JSON data into an array of blocks, while disregarding any

As I searched through various resources on converting JSON into arrays using JavaScript, none of the results matched my specific requirements (outlined below). I am in need of a RegEx that can transform JSON into an array containing all characters such as ...

Customer Notification System Malfunctioning on Front End

I am currently experimenting with MeteorJS technology and attempting to use alerts for success or failure notifications when making a meteor call. However, I've encountered an issue where the alerts are not functioning as expected. T ...