What strategies can be used to stop elements from reverting to their original state?

Hey there, I'm currently experimenting with animation and trying to create a cool two-step effect: 1. elements falling down and 2. pulsating on click. However, I've run into an issue where after the clicking action, the elements return to their original positions before the falling animation. Any ideas on what I might be missing? Have a look at this example on Codepen: https://codepen.io/ju-rat/pen/VwMbZVL?editors=1010

Let's take a peek at the code:

function pulsation() {
  var elem = document.getElementsByClassName("bulb");
  elem[0].style.animation = "pulse 0.5s linear infinite 0s alternate";
  elem[1].style.animation = "pulse 1s linear infinite 2s alternate";
  elem[2].style.animation = "pulse 0.5s linear infinite 0s alternate";
  elem[3].style.animation = "pulse 1s linear infinite 2s alternate";
}
html {
  background-color: rgba(171, 183, 183);
}

* {
  margin: 10px;
}

#container {
  width: 100%;
  display: flex;
}

#b1 {
  height: 50px;
  width: 50px;
  border-radius: 100%;
  background-color: red;
}

#b2 {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background-color: yellow;
  filter: none;
}

#b3 {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background-color: green;
  filter: none;
}

#b4 {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  background-color: blue;
  filter: none;
}

#b2 {
  animation: fall2 1s ease-out;
  animation-delay: 0s;
  animation-fill-mode: forwards;
}

@keyframes fall2 {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(400%);
  }
}

#b3 {
  animation: fall 2s ease-out;
  animation-delay: 1s;
  animation-fill-mode: forwards;
}

@keyframes fall {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(400%) translateX(100%);
  }
}

@keyframes pulse {
  0% {
    filter: none;
  }
  50% {
    filter: brightness(80%) drop-shadow(0px 1px 14px rgba(224, 231, 34));
  }
  100% {
    filter: brightness(140%) drop-shadow(0px 1px 14px rgba(251, 255, 255));
  }
}


}
<div id=c ontainer>
  <div class="bulb" id="b1" onclick="pulsation()"></div>
  <div class="bulb" id="b2"></div>
  <div class="bulb" id="b3"></div>
  <div class="bulb" id="b4"></div>
</div>
<p>Click on the red circle</p>

Answer №1

You replaced fall 2s ease-out with pulse 0.5s linear, resulting in the loss of anything associated with fall.

To my knowledge, it is not possible to merge both effects (pulse and fall) into a single CSS animation. It's either one or the other, though I could be mistaken. However, you can work around this by placing a child element inside each bulb and applying the pulse effect to that element instead of the bulb itself:

function pulsation() {
  var elem = document.getElementsByClassName("bulb");
  elem[0].children[0].style.animation = "pulse 0.5s linear infinite 0s alternate";
  elem[1].children[0].style.animation = "pulse 1s linear infinite 2s alternate";
  elem[2].children[0].style.animation = "pulse 0.5s linear infinite 0s alternate";
  elem[3].children[0].style.animation = "pulse 1s linear infinite 2s alternate";
}
html {
  background-color: rgba(171, 183, 183);
}

* {
  margin: 10px;
}

#container {
  width: 100%;
  display: flex;
}

.bulb {
  position: relative;
  height: 50px;
  width: 50px;
  border-radius: 50%;
}

#b1 {
  background-color: red;
}

#b2 {
  background-color: yellow;
  filter: none;
  animation: fall2 1s ease-out;
  animation-delay: 0s;
  animation-fill-mode: forwards;
}

#b3 {
  background-color: green;
  filter: none;
  animation: fall 2s ease-out;
  animation-delay: 1s;
  animation-fill-mode: forwards;
}

#b4 {
  background-color: blue;
  filter: none;
}

.inner {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: inherit;
  border-radius: 50%;
  margin: 0;
}

@keyframes fall2 {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(400%);
  }
}

@keyframes fall {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(400%) translateX(100%);
  }
}

@keyframes pulse {
  0% {
    filter: none;
  }
  50% {
    filter: brightness(80%) drop-shadow(0px 1px 14px rgba(224, 231, 34));
  }
  100% {
    filter: brightness(140%) drop-shadow(0px 1px 14px rgba(251, 255, 255));
  }
}
<div id=c ontainer>
  <div class="bulb" id="b1" onclick="pulsation()">
    <div class="inner"></div>
  </div>
  <div class="bulb" id="b2">
    <div class="inner"></div>
  </div>
  <div class="bulb" id="b3">
    <div class="inner"></div>
  </div>
  <div class="bulb" id="b4">
    <div class="inner"></div>
  </div>
</div>
<p>Click on the red circle</p>

(Please note: The CSS has been tidied up, repetitions have been minimized, etc)

Answer №2

Seems like the missing piece in the pulsing animation was

transform: translateY(400%) translateX(100%);
Don't worry, I've added it for you!

function pulseEffect() {
  var element = document.getElementsByClassName("bulb");
  element[0].style.animation = "pulse 0.5s linear infinite 0s alternate";
  element[1].style.animation = "pulse 1s linear infinite 2s alternate";
  element[2].style.animation = "pulse 0.5s linear infinite 0s alternate";
  element[3].style.animation = "pulse 1s linear infinite 2s alternate";
}
html {
  background-color: rgba(171, 183, 183);
}

* {
  margin: 10px;
}

#container {
  width: 100%;
  display: flex;
}

.bulb {
  height: 50px;
  width: 50px;
  border-radius: 100%;
  background-color: red;
}

#b2,
#b3,
#b4 {
  height: 50px;
  width: 50px;
  border-radius: 50%;
  filter: none;
}

#b2 {
  animation: fallDown2 1s ease-out;
  animation-delay: 0s;
  animation-fill-mode: forwards;
}

@keyframes fallDown2 {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(400%);
  }
}

#b3 {
  animation: fallDown 2s ease-out;
  animation-delay: 1s;
  animation-fill-mode: forwards;
}

@keyframes fallDown {
  0% {
    transform: translateY(0%);
  }
  100% {
    transform: translateY(400%) translateX(100%);
  }
}

@keyframes pulse {
  0% {
    filter: none;
    transform: translateY(400%) translateX(100%);
  }
  50% {
    filter: brightness(80%) drop-shadow(0px 1px 14px rgba(224, 231, 34));
    transform: translateY(400%) translateX(100%);
  }
  100% {
    filter: brightness(140%) drop-shadow(0px 1px 14px rgba(251, 255, 255));
    transform: translateY(400%) translateX(100%);
  }
}
<div id="container">
  <div class="bulb" id="b1" onclick="pulseEffect()"></div>
  <div class="bulb" id="b2"></div>
  <div class="bulb" id="b3"></div>
  <div class="bulb" id="b4"></div>
</div>
<p>Click on the red circle</p>

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

Establishing a Next.js API endpoint at the root level

I have a webpage located at URL root, which has been developed using React. Now, I am looking to create an API endpoint on the root as well. `http://localhost:3000/` > directs to the React page `http://localhost:3000/foo` > leads to the Next API end ...

How can I make the droppable elements only drop within draggable elements in jQuery UI?

After reading several similar articles and questions, I am still struggling to get this working properly when there are multiple droppable elements with the same named class. Is there a way to ensure that the dragged element can only be dropped in a speci ...

Encountering challenges with implementing debouncing functionality in React programming

import React,{useState} from 'react'; const app = () => { const [inputValue, setInputValue] = useState(); const handleChange = (e) => { setInputValue(e.target.value); console.log(inputValue); } const d ...

Using the max-width property with Semantic UI Dropdown in ReactJS

I'm struggling to determine how to adjust the max-width of the Dropdown element in ReactJS. I attempted the following: .Menu Dropdown { max-width: 5rem !important; } Unfortunately, this did not work as expected. The dropdowns are taking up too m ...

Validating Credit Card Numbers with Spaces

Currently, I am in the process of creating a credit card form that requires validation using checkValidity to match the specific card pattern that is dynamically added to the input field. For example, if a user enters a Mastercard number such as 545454545 ...

What is the method to conceal a certain element in jQuery based on the value of another element?

I am dealing with the following HTML structure: <button id="hideToggle">show/hide</button> <form id="item"> <div>Item 1 <input name="item1" type="number"/></div> <div>Item 2 <input name="item2" type="nu ...

Has the Soundcloud web widget API become obsolete?

I am currently working with the SoundCloud widget API (). My main goal is to retrieve information about the tracks within a set. Unfortunately, it seems like I am only able to utilize the methods widget.getSounds() and widget.getCurrentSound successfully ...

Loading Java Script files in real-time

Is there a method to dynamically load JS files before "$(document).ready" is triggered, while still having them available in the ready event handler? Is there a feature in jQuery that allows for this process? The challenge I am facing involves loading di ...

Adding Roles Using Discord.js - A Simple Guide

I'm currently working on a Discord bot using Discord.js, and I am trying to implement a feature where users can use a command to assign themselves a role. However, despite my best efforts, I am unable to get this functionality to work. In my bot' ...

How to achieve horizontal auto-scrolling in an image gallery with jQuery?

Hey there, I'm currently working on an Image Gallery project. I have arranged thumbnails horizontally in a div below the main images. Take a look at this snapshot img. My goal is to have the thumbnails scroll along with the main pictures as the user ...

Error: Attempting to access the property 'push' of an undefined variable has resulted in an unhandled TypeError

if (Math.random() <= .1) { let orgAdmin = User.find({email: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1234454665324721380c0d">[email protected]</a>'}); or ...

Node.js file successfully establishes a database connection, but script tags fail to do so

Having some trouble connecting to my MongoDB database (or MongoLab server). It works perfectly fine when the code is in the name.js file, but for some reason it fails when placed inside <script> tags within an HTML file. My folder contains only thes ...

Verify if the connection to MongoDB Atlas has been established for MongoDB

When working with MongoDB, I find myself switching between a local database during development and MongoDB Atlas in production. My goal is to implement an efficient text search method that utilizes $search when connected to MongoDB Atlas, and $text when co ...

Using JS regular expressions to only select anchor (a) tags with specific attributes

When trying to select a link tag (a) with a specific data-attr, I encountered an issue. I currently use the following regex pattern: /<a.*?data-extra-url=".*?<\/a>/g. However, I noticed that this selection goes wrong when there are no line br ...

showcasing real-time webcam information within an HTML web application

I have successfully integrated my webcam data live into my web application using the following code snippet. The live feed from my webcam is now visible on the webpage. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta ...

Guide to implementing dynamic conditional rendering in Vue.js loops (utilizing v-if within v-for)

I am currently working on a table component in a .vue file where I want to display icons based on the direction of the order clicked. For example: <th v-for="(column, index) in columns" :key="index" @click="sort( index )"> <span& ...

Displaying various Ajax html responses

The function $('.my-button').click(function(e) is designed to display the output of the MySQL query in display.php, presented in HTML format. While it functions correctly, since each button is looped for every post, the script only works for the ...

Search for Azure Time Series Insights (TSI) data insights

Is there a way to access real-time data from Azure TSI using the TSI query API? I am currently utilizing the TSI JavaScript Client library, which provides two wrappers for the Query API. However, these wrappers only allow me to retrieve aggregate data li ...

activating a jQuery function and sending a parameter

Looking for some help with JavaScript - I'm having trouble getting this code to work. I need to trigger a predefined function from one script within my AJAX script. Specifically, I want to activate the qtip functionality on the content of a div that i ...

The development chrome extension failed to load due to an invalid port or malformed URL pattern

I'm encountering an issue while trying to load my development chrome extension for debugging. The error message I am receiving is: Issue with 'content_scripts[0].matches[0]' value: Path cannot be empty. Manifest failed to load. This is th ...