Changing the style.backgroundImage property overrides any image transition effects

I am currently working on creating a button for a player. You can check out the video (here)

The button works, but in order to give it a "pressed" effect, I have to change its background-image on click. However, this action seems to override the transition that was already in place. The rotation part still functions properly, unlike the image swapping.

Not changing the image on pause or reverting it to the initial image does not yield the desired result.

I believe the solution may involve using @keyframes and handling everything manually, but I am searching for a cleaner approach.

Here is a demo: (I want to ensure that when clicked twice, the light-blue square transitions back to orange)

document.addEventListener("DOMContentLoaded", function () {
    let state = "pause";

    const playButton = document.getElementById("playIcon")

    function playButtonClick() {
        if (state === 'play') {
            state = 'pause';
            playButton.classList.remove("transitionBane");
            playButton.style.backgroundImage = "url('https://placehold.co/600x400/aliceblue/white')";
        } else {
            state = 'play';
            playButton.classList.add("transitionBane");
            playButton.style.backgroundImage = "url('https://placehold.co/600x400/gray/white')";
        }
    }

    addEventListener("click", playButtonClick)
});
#playIcon {
  top: 35%;
  left: 40%;
  position: absolute;
  background-color: transparent;
  background-repeat: no-repeat;
  background-image: url("https://placehold.co/600x400/orange/white");
  button:focus {outline:0;}
  background-size: 100% 100%;
  border: 0;
  width: 18%;
  height: 31.6%;
  z-index: 52;
  transform: rotate(0);
  //animation: playButtonSwap1 2s alternate infinite ease-in-out;
  transition: all 0.75s cubic-bezier(.71,0,.33,1.56) 0ms;
  cursor: pointer;
}

#playIcon:hover {
  transform: rotate(360deg);
  background-image: url("https://placehold.co/600x400/aliceblue/white");
}

.transitionBane {
  transition: none !important;
}
<!DOCTYPE html>
<html lang="en>
<head>
    <meta charset="UTF-8>
    <title>Title>
</head>
<body>
<button id="playIcon></button>
</body>
</html>

Answer №1

I tested the double click feature and eliminated the background image in the hover selector based on my interpretation of your query. Please review and inform me if this is not satisfactory for you.

document.addEventListener("DOMContentLoaded", function () {
    let state = "pause";

    const playButton = document.getElementById("playIcon")

    function playButtonClick(event) {
        if (state === 'play') {
            state = 'pause';
            playButton.classList.remove("transitionBane");
            
            if(event.detail==1){
               playButton.style.backgroundImage = "url('https://placehold.co/600x400/aliceblue/white')";
            }
              if(event.detail==2){
               playButton.style.backgroundImage = "url('https://placehold.co/600x400/orange/white')";
            }
         
        } else {
            state = 'play';
            playButton.classList.add("transitionBane");
            playButton.style.backgroundImage = "url('https://placehold.co/600x400/gray/white')";
        }
    }

    addEventListener("click", playButtonClick)
});
#playIcon {
  top: 35%;
  left: 40%;
  position: absolute;
  background-color: transparent;
  background-repeat: no-repeat;
  background-image: url("https://placehold.co/600x400/orange/white");
  button:focus {outline:0;}
  background-size: 100% 100%;
  border: 0;
  width: 18%;
  height: 31.6%;
  z-index: 52;
  transform: rotate(0);
  //animation: playButtonSwap1 2s alternate infinite ease-in-out;
  transition: all 0.75s cubic-bezier(.71,0,.33,1.56) 0ms;
  cursor: pointer;
}

#playIcon:hover {
  transform: rotate(360deg);

}

.transitionBane {
  transition: none !important;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <title>Title</title>
</head>
<body>
<button id="playIcon"></button>
</body>
</html>

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

When transformed into clickable links, the list items start piling up on

Here are two groups of code along with a straightforward question that most people can answer easily, but still worth asking. Below is the initial piece of code with working CSS implementation: <div class="subTopHolder"> <ul class="language ...

Best practices for incorporating and leveraging node packages with Laravel Mix

As I embark on my Laravel (v 8.x) Mix project, I am encountering challenges when it comes to incorporating JavaScript from node modules. To kick things off, here is a snippet from my webpack.mix.js: mix.js('node_modules/mxgraph/javascript/mxClient.mi ...

Is the post secure if a HTTP web page sends an ajax request to a HTTPS url?

If I developed an HTML/jQuery widget to be embedded on third-party websites with users of very limited technical knowledge and potentially missing SSL certificates, would the information posted securely through AJAX Post to a secure URL remain protected? ...

What is the process for obtaining the indirect link of a Google search result?

I am looking to obtain the indirect link of a Google search result. After performing a search on Google, if I right-click on the result link, it changes to something like this: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&am ...

Ways to include "working hours" if statement a particular holiday dates are specified

Trying to update the code for an "if statement" to include specific dates that are official holidays when the business is not operational. The current code works if the day falls on Mon, Tue, Wed, Thu, or Fri and the time is between 09:00 and 15:00 in a 24 ...

How to manage form submissions in Vue.js using inputs within child components

I'm working on a parent component that acts as a form. This form consists of multiple child components, each containing input fields. <template> <div class="form"> <generalData v-model="input" /> <textAreas v- ...

Utilize the atan2() function to rotate a div so that it follows the movement of the mouse

I am trying to create an effect where the mouse aligns with the top of a div and the div rotates as the mouse moves. The rotation should happen when the top part of the div is in line with the mouse cursor. My goal is to achieve this using the atan2 functi ...

Show a separate div in a block format if the data field contains a value

<span>{{pstCtrl.doctorProfile.boardCertification ? === 'Yes' : $yes.display-block}}</span> <span class="yes">Yes</span> span.yes { display:none; } In my Angular.JS project, the code snippet above is demonstra ...

Is there a way to invoke a different function within a class from a callback function in an HTTP request?

Having an issue with my HTTP GET request function in the "CheckPrice" class. When trying to call another function within the class callback, it's showing as undefined. Any suggestions? const got = require("got") class PriceCheck { constructor() { ...

Updating parent array values within child components in React

Currently, I am working on a React application where I need to save the handlers for all windows opened from the app. Previously, before using React, I stored these windows in a global array attached to the parent window, although I understand that using J ...

After a duration of 4 minutes, an ERR_EMPTY_RESPONSE is thrown in response to the

My angular 5 node app involves sending a request to the server and waiting for a response. There are instances where the response takes around 5-6 minutes to arrive. However, an ERR_EMPTY_RESPONSE error is triggered by the http method after just 4 minutes ...

Ensure that the context is used to effectively clear any existing data from the previous bar chart

I recently came across a cool codepen demo on this link. Upon clicking the first button followed by the second, the data transitions smoothly. However, there seems to be an issue where when hovering randomly over the bar charts at this source, the value ...

Multiple images overlapping each other in a dynamic parallax effect

Despite my fear of receiving down votes, I have been unsuccessful in finding the answer to my question so I will proceed with asking it. I am attempting to replicate a parallax effect similar to the one showcased on this website, particularly the image of ...

Horizontal scroll box content is being truncated

I've been trying to insert code into my HTML using JavaScript, but I'm facing a problem where the code is getting truncated or cut off. Here's the snippet of code causing the issue: function feedbackDiv(feedback_id, feedback_title, feedb ...

What is the best way to replace testcaferc.json browsers using the command line interface (CLI

Scenario: I am facing a situation where I aim to execute Testcafe in docker within a remote environment that necessitates running Testcafe through its command-line interface. I intend to utilize the .testcaferc file that I use for local testing to avoid m ...

Changing the screen resolution can cause the LI elements to become disorganized and appear out of

I have a menu that displays multiple links using a styled UL. Everything looks great at 100% resolution, but when I adjust the screen resolution, the links no longer fit within the menu and some end up on another line, causing issues. My concern is this - ...

Adding a background color with a background image layered underneath it in CSS - here's how!

Looking to set a background color for a div, with a background image on the bottom of the same div. However, I need the background color to stop once the background image begins. Check out the example image below: ...

How come the dropdown menu consistently disrupts my CSS design?

Whenever I add a dropdown menu, my CSS layout gets all messed up. Can someone please explain why this happens and how to fix it? Below is the code snippet: <asp:Label ID="projnamelbl" runat="server" CssClass="editlabels" Text="Project Name"></as ...

The `overflow:auto` property is causing the flex container to overflow due to the size of its fixed width children

Here's a unique CSS puzzle for you all: Imagine the following hierarchy: app container flex container item1 - fixed width(80px) item2 - responsive width(width:100%). MUI Stepper - overflow:scroll, width:100% This structure would look like this: ...

Retrieving a dynamic JSON object for the MusicBrainz application using AngularJS

I want to incorporate a search form into my application that sends the form result to the specified link. I am retrieving artist names from the musicbrainz JSON database using the following request: "NAME OF AN ARTIST"%20e*&fmt=json The "NAME OF AN AR ...