Creating two interactive animations utilizing a combination of CSS and Javascript, triggered by the click event on

Looking to incorporate two unique CSS animations into my project. Utilizing two buttons and one object, I want the first animation to trigger upon clicking the first button.

I understand how to handle one button and one animation, but am unsure how to manage two distinct animations on the same object.

Update: The issue has been identified!

Answer №1

Do you find this solution to be satisfactory?

let btn_a = document.getElementById('a')
let btn_b = document.getElementById('b')
let btn_c = document.getElementById('c')
let div = document.getElementById('obj')
btn_a.addEventListener('click', function() {
  div.className = 'anim-a'
})
btn_b.addEventListener('click', function() {
  div.className = 'anim-b'
})
btn_c.addEventListener('click', function() {
  $('#obj').animate({}, function(){
    $('#obj').css({transform: 'rotate(30deg) translate(200px)'})
  })
})
div {
  background-color: pink;
  width: 100px;
  transition: all linear 0.5s;
}

.anim-a {
  background-color: yellowgreen;
  width: 150px;
}

.anim-b {
  background-color: grey;
  width: 200px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id='obj'>animated</div>
<button id='a'>anim-a</button>
<button id='b'>anim-b</button>
<button id='c'>anim-c</button>

UPDATE

If you are using jQuery without CSS properties, the .animate() method can be utilized. However, keep in mind that .animate() does not directly support the transform property. This action can be handled within the callback like so:

btn_c.addEventListener('click',function() {
  $('#obj').animate({}, function(){
    $('#obj').css({transform: 'rotate(30deg)'})
  })
})

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

Error: Angular encountered an unexpected token in the syntax

I am encountering an issue while trying to incorporate a user's profile updater on my website. Whenever I attempt to access a user's profile on the site, I run into this Angular error: main.ts:6 ERROR SyntaxError: Unexpected token 'e', ...

Leveraging the @font-face feature along with fonts sourced from fontsquirrel

As I embark on creating my very first website, I find myself delving into the world of fonts from Font Squirrel. However, a challenge arises as I realize that not all the fonts I downloaded from the site are easy to use. The real struggle comes when deal ...

The issue arises when the mat-panel-description is not properly aligned with the shrinking panel

Within my HTML, I have a mat-panel with a description that is populated by a variable from the TS file. However, I am encountering an issue where if I shrink the panel and the description is too long, the text is cut off at the top and bottom. Is there a w ...

Change a PNG file into a three.js plane by converting it into height

I have a visualization challenge using three.js. I am working with an image linked here: https://i.sstatic.net/EzfcS.png. I am using a plane geometry and I aim to adjust the height of the plane according to this other image: https://i.sstatic.net/hMj91.png ...

Having trouble with jQuery events not triggering properly after dynamically inserting elements using an ajax request?

It's strange that all my jQuery events become unresponsive after an AJAX call. When I use a load function, once the JSP reloads, none of the events seem to work properly. Any suggestions? Below is the code that triggers the function call: $('#p ...

Custom Sizing for Bootstrap Modals

I need assistance with creating a dynamic bootstrap modal that adjusts its width based on the content and includes a vertical scrollbar when needed. Although the vertical scrollbar is functioning correctly, the horizontal width appears to be static. C ...

Arrange the divs in a horizontal line without stacking them vertically, specifically in Internet Explorer 7

I have a fixed container that contains another container with multiple DIVs based on user choices. I want these DIVs to align horizontally to allow for horizontal scrolling, without any vertical scrolling. For example: [x] [x] [x] Here is a simplified ve ...

Concealing other items in an array on selecting one item in React Native- A step-by-step guide

Currently, I have set up my view to display an array of items (Circle components) as shown in the image below: However, I am facing a challenge in hiding all other Circle components when I click on one of them. The onPress event is configured to zoom in a ...

Flexbox presenting a specific alignment issue

I'm facing a challenge using Flexbox to achieve the desired result shown below https://i.sstatic.net/8vJGQ.png When trying to position my 2 blocks on the right, one of them ends up crossing the line and I struggle to bring it back up. I want this l ...

Looking for guidance on where to find a functional code sample or comprehensive tutorial on working with ViewMetadata in Angular2

I am currently trying to understand the relationship between viewmetadata and the fundamental use of encapsulation: ViewEncapsulation, including ViewEncapsulation.Emulated and ViewEncapsulation.None. Here is a link for further information: https://angula ...

Tips for resizing the dimensions of two div elements to fit the screen dimensions

Take a look at this template. It's interesting how the left content div and right sidebar divs adjust themselves based on the browser window size, zoom level, or viewing device such as a mobile phone. When viewed on a mobile, only the content div is d ...

"Sliding through pictures with Bootstrap carousel placed beneath the

Currently, I am working on a website that requires a background image, although I personally do not prefer it. The client's preference is to have the navbar transparent so that the background image shows through it. Now, I would like to incorporate a ...

Adjust the carousel width on slide in Bootstrap 4

Having an issue with my Bootstrap Carousel where the width changes as it slides. The carousel starts off fine, But when it slides, I want the carousel to stay centered and maintain a fixed width. Setting a fixed width for carousel-inner works, but it n ...

Tips for ensuring consistent alignment of the final navigation item

I'm having trouble understanding why the layout collapses when I resize the window horizontally. I've attempted to adjust the width of the div and tried using display:block. My website is built with Bootstrap 3, although I don't believe that ...

Here's a guide on how to package and send values in ReactJs bundles

I'm currently involved in a ReactJs project that does not rely on any API for data management. For bundling the React APP, we are using Webpack in the project. The challenge now is to make the React APP usable on any website by simply including the ...

Create a layout using jquery-tokeninput

Currently, my project incorporates jQuery-tokenInput and everything is functioning properly. However, I now have the desire to customize the design of the results that are displayed. For example, I want to adjust the width or make any other design changes. ...

Updating information on <element>

Having trouble with the <object> element. I'm trying to display different external pages, but it's not functioning properly. It seems like once I set the data for the first time, any subsequent changes have no effect. Do I need to refresh i ...

When utilizing GET in PHP to handle data created using jQuery's $.param(data) function, you will come across %5B and %5D symbols being used

I am working with JSON data named rowdata that I utilize in an ajax call. When I use the following code: JSON.stringify(rowdata) The output looks like this: {"Description":"qwerty","Code":"12345","Size":"11","Colour":"green"} I am trying to send it to ...

Is it feasible to package shared modules into individual files using Browserify?

In my web app, I am using Browserify, Babel, and Gulp to bundle my scripts into a single file. However, when I checked the file size, it was over 3MB which seems excessive to me. Although I'm not entirely sure how Babel and Browserify modify my sourc ...

Combining two containers in CSS

I am currently working on building a website design, and I have a menu positioned on the right side of the layout. Below is the code for the menu along with an explanation. #rightmenu { border-top: 1px solid #000000; border-right: 1px solid #00000 ...