What's the best way to prolong the duration of a CSS animation?

I have a code snippet here that is designed to style a button when it is clicked and then maintain the style even after the click for a more aesthetically pleasing look. I'm wondering if there might be another approach to achieve this effect? Thanks!

// Clicked button receives enhanced styling
document.querySelectorAll('button[type="button"]').forEach(button => {
  button.addEventListener("click",event => {
    // Apply styling
    event.target.style["boxShadow"] = "0 0 2px 3px rgba(250,189,22,0.95), inset 0 0 4px 0 rgba(250,195,35,1)";
    event.target.style.borderRadius = "0.4rem";
    event.target.style.transitionDuration = "45ms";
    event.target.style.transform = "scale(0.98)";
    setTimeout(() => {
    // Remove the clicked styling 
    event.target.style.removeProperty("box-shadow");
    event.target.style.removeProperty('border-radius');
    event.target.style.removeProperty('transition-duration');
    event.target.style.removeProperty('transform');
    }, 110);
  });
});

Answer №1

Perfection is key in your approach. I would suggest moving the click-specific styling to a CSS class, then adding and removing the class from the button after a certain time interval.

document.querySelectorAll('button[type="button"]').forEach(button => {
  button.addEventListener("click",event => {
    event.target.classList.add("beautiful")
    setTimeout(() => {
      event.target.classList.remove("beautiful")
    }, 110);
  });
});

In your CSS file, you can define the style for the "beautiful" class like this:

button {
  transition-duration: 45ms; // This should be included in the standard button style
}

.beautiful {
  box-shadow: 0 0 2px 3px rgba(250,189,22,0.95), inset 0 0 4px 0 rgba(250,195,35,1);
  border-radius: 0.4rem;
  transform: scale(.98);
}

Answer №2

Apply this style to every button element

.customButton{
  // default button styling 
  transition: transform 0.5s;  // specify effect duration
}

.customButton:active {
  // Styling for when the button is clicked
  transition:  0s;
}

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

Vuetify 3 now displays full text in v-autocomplete without an ellipsis

Trying to truncate long text in a v-autocomplete component using Vuetify 3 and text-overflow: ellipsis, but it's not working. Check out the code below: <div id="app"> <v-app id="inspire"> <v-row align="cen ...

What is the process for updating the names and IDs of HTML elements?

I need help with updating the index of elements in a dynamic form when an item is deleted. For instance, if I have items named items1, items2, items3, items4, and items5, and I delete items3, I want the remaining items to be re-indexed to items1, items2, i ...

Using AngularJS to maintain a 'Token' throughout the duration of the application

Currently utilizing AngularJS with Restangular to authenticate a user during the login process. Once authenticated, a 'Token' is returned which must be utilized for all subsequent requests. My inquiry pertains to the optimal method of storing th ...

Utilize JavaScript to identify the orientation of an iPad device

I have implemented the code below to monitor the orientation of the iPad. However, it seems that this method is only triggered when I physically rotate the device or change its orientation. If the app is launched in landscape mode and I navigate to a dif ...

Tips for transferring information between two distinct ports within a single application

I have been utilizing the expressjs library for my project. It functions as a server on localhost:8000 and I am displaying static pages through it. However, my application operates on localhost:4200. Despite attempting to share the same cookie or localSt ...

Display a pop-up alert message when the session expires without the need to manually refresh the page

I have a query regarding the automatic display of an alert message. Even though I have set the time limit to 10 seconds, I still need to manually refresh the page for the alert message to appear. The alert message should notify the user that the session ...

Design a circular progress bar with a cut-off at the bottom

Does anyone have suggestions on how to create a circular progress bar with cropping at the bottom, similar to this example: PROGRESS BAR? I've been searching for a component like this but haven't had any luck. Any ideas, especially utilizing Mate ...

I am looking to load an alert message simultaneously with loading an HTML link

My request is for a pop-up sweet alert to appear when the "accept your promotion" button is clicked, along with calling a relevant link at the same time. This link will update a data column in a database based on Boolean values. <script src="https://cd ...

I need to retrieve data from two different databases. What is the best way to combine the code into a single function?

I am working on fetching data from 2 database sources and combining them to send the merged data to result.html function showResult(req, res){ var n = req.query.query mysql_conn.query('SELECT query_text FROM catalogsearch_query WHERE ...

Highlight text when the user is scrolling the page - using JQUERY

Can anyone suggest a way to dynamically underline text as the user scrolls down a webpage? The underline should only be visible while the user is scrolling. I've experimented with animate.css and other plugins, but haven't been able to achieve th ...

Restrict the maximum and minimum time that can be entered in an HTML input

I've implemented a basic code feature that allows users to input minutes and seconds on my React website. <div> <span> <input defaultValue="0" maxlength="2"/> </span> <span>m</span> <spa ...

Looking to retrieve data using Cheerio? If you're finding that the data appears empty in the page source but is visible when inspecting the elements, here's how you can go

I am encountering difficulties while trying to scrape data from another website. In my case, I notice that the data appears empty when viewing the page source, but it is visible when inspecting the elements. If you're confused, please refer to the fol ...

The Google Chrome console is failing to display the accurate line numbers for JavaScript errors

Currently, I find myself grappling with debugging in an angular project built with ionic framework. Utilizing ion-router-outlet, I attempt to troubleshoot using the Google Chrome console. Unfortunately, the console is displaying inaccurate line numbers mak ...

Removing the empty option in a select element with ng-model

There's a situation I'm dealing with that involves a select dropdown along with a refresh button and a plus button. The issue arises when clicking the refresh button sets the model to null, while clicking the plus button displays the dropdown wit ...

An endless expanse of flooring within a THREE.JS environment

What is the most effective method for creating a floor that extends infinitely in all directions in my canvas three.js scene? Would it be more efficient to link a THREE.PlaneGeometry to the camera's position so that it moves along with the camera? A ...

Centering an Image with CSS

Looking for assistance in centering my code vertically and horizontally, while also dealing with images of various sizes. CSS: body { margin: 0px; } .wrapper { text-align:center; width: 100%; height: 100%; position: relative; z-index: ...

The onClick event is not functioning properly with React's select and option elements

Looking for a way to get the option value from each region? const region = ['Africa','America','Asia','Europe','Oceania']; <div className="options"> <select> ...

Troubleshooting: React Testing Library Issue with Updating Material UI DatePicker Input Content

I'm attempting to update the value of the Material UI Datepicker Input using React Testing Library. Unfortunately, I have not been successful with the fireEvent.change() method. import React from "react"; import { render, screen, waitFor, fi ...

Add an image to a div element and determine its height, then apply the height to the CSS property

Here is my JavaScript code that utilizes jQuery: $(".article_big_photo").click(function() { $('#screen').css({ opacity: 0.5, 'width':$(document).width(),'height':$(document).height()}); $('#screen').show() ...

The persistent redirection issue in Node.js - Middleware

Currently, I am in the process of developing a middleware using node js and express. The main objective of this middleware is to redirect users to a login page if they are not authenticated. Although the redirection to the login page works as intended, th ...