Tips for creating CSS3 animations in Angular triggered by mouse clicks

I'm working with a span that utilizes a Bootstrap icon. My goal is to fade out and fade in the same element (span) on click, while toggling the class (icon).

There's a boolean variable called showLegend which determines whether or not to animate the span. I've managed to make it work when the element initializes by assigning it a class. However, I want the animation to be triggered on every click (i.e., whenever the toggleTrendLegend() function is called, as seen in the Plunker). Can someone please advise me on how to achieve this?

I specifically want to activate the animate-fade class with each click.

As a side note, I'd like to avoid using Angular Animation due to its heavy load for simple animations.

You can find the Plunker reference code here

This snippet demonstrates the setup:

<div class="show-hide">

   <span (click)="toggleTrendLegend()" [class]="showLegend == true ? 'animate-fade glyphicon adjust-eye glyphicon-eye-close' : 'glyphicon adjust-eye glyphicon-eye-open'"></span>

</div>

Answer №1

There are several methods to accomplish this task:

One way is by using class.yourClass

<span (click)="showLegend = !showLegend" [class.animate-fade.glyphicon-adjust-eye.glyphicon-eye-close]="showLegend" [class.animate-fade.glyphicon-adjust-eye.glyphicon-eye-close]="!showLegend"></span>

Another approach is utilizing ngClass

<span (click)="showLegend = !showLegend" [ngClass]="{'animate-fade glyphicon adjust-eye glyphicon-eye-close' : showLegend, 'glyphicon adjust-eye glyphicon-eye-open': !showLegend}"></span>

If you wish to implement animations, please consult the official documentation

Remember to include the necessary animation!

transition: all 275ms ease-in-out;

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

Customize the default email validator in AngularJS

I am looking to customize the email validator in AngularJS by using a custom string for validating email addresses. However, when I tried implementing the code provided in the documentation, it doesn't seem to work as expected. Here is the code snippe ...

Changing the order of element names depending on their location within the parent element using jQuery

<div class="content"> <div> <input type="text" name="newname[name]0"/> <div> <input type="text" name="newname[utility]0"/> <div> <textarea name="newname[text]0 ...

Chrome may clip the gradient radius when setting the focal point outside of the SVG gradient

I just joined this community and I might have some questions that could be considered too basic. Recently, I've been trying to understand SVG and came across something that left me puzzled. Take a look at this: <svg xmlns="http://www.w3.org/20 ...

When attempting to click the submit button, the action of submitting a form using jQuery/AJAX is not functioning as expected

When attempting to submit a form using jquery/AJAX, my function does not get called when clicking on the submit button. The structure of my website is as follows: CarMenu.php <html lang="en"> <html> <head> <meta charset="ISO-8859- ...

How can I adjust padding values specifically for mobile devices within the Bulma CSS framework?

After constructing a website with the Bulma CSS framework, I encountered an issue with padding on mobile devices. To optimize the user experience, I need to reduce the padding on smaller screens to 5% or less instead of the initial 10%. However, I am unsur ...

Is there a way to customize the dot in a JavaFx RadioButton?

Hello fellow coding enthusiasts, I am looking to find out how I can customize the color of a RadioButton dot. I want to have 3 RadioButtons, each with a different color to represent a state. Do I need to use CSS for this task? If so, could someone please ...

Angular 4: Retrieving the selected element's object from a collection of elements

In my x.component.html file, I have a list of objects that are being displayed in Bootstrap cards like this: <div *ngFor="let item of items | async"> <div class="row"> <div class="col-lg-6"> <div class="card ...

Adjusting div height as you scroll down the page

Hello, I am new to CSS and I have encountered an issue with setting the height of a div element to 100%. When the list on my page exceeds the size of the page, the div is no longer visible when scrolling. Do you have any ideas on how to fix this? Here is t ...

When resizing the browser window, this single horizontal page website does not re-center

I am in the process of creating my very first one-page horizontal website using jQuery and the scrollTo plugin. This site consists of 3 screens and initially starts in the center (displaying the middle screen). To achieve this, I decided to set the width o ...

Discovering the process of extracting information from an individual object within a local JSON file on a detailed page using Angular 9

Here is a JSON object I am working with: [ { "products": [ { "id": "1", "name": "Apple", "price": "free", "imageURL": "assets/apples.jpg", "category": "Fruits" }, an ...

Transition animation using jQuery

I am seeking assistance with jQuery effects. Specifically, I have set images to a certain position using style, and I want users to be able to drag these images around once the HTML page is published. Is there a plugin or command in jQuery that will allow ...

What is the best way to showcase data from input fields within a bootstrap modal dialog?

After the user has entered their details and clicks submit, I would like to present the information in a Bootstrap modal with a confirmation button below. This serves as a preview of the data before it is saved to the database. Here's what I have so ...

Angular fails to detect newly added component

Just dipping my toes into the world of angular and decided to create my first app tour of heroes. However, I encountered a problem where angular wasn't recognizing a new component. Instead of throwing an error, it simply refused to render in the brows ...

Adjust the height of the image using CSS while maintaining its aspect ratio within the Gall-Peters projection

I've implemented a Gall-Peters projection on my website and I'm looking for the right CSS code to maintain its aspect ratio. I want the image to take up 100% of the available width without elongating the countries' shapes. Can anyone help wi ...

Animated CSS sidemenu (utilized as a filtering panel for a table)

Hi there, I'm having some trouble with CSS Animation. I recently started developing websites and am using Bootstrap 4 along with Animate.css for animations. My goal is to have an icon button expand sideways to reveal a div containing select elements f ...

What is the most effective way to upload HTML and CSS files on Google's servers?

Our website is basic with HTML, CSS, and JavaScript and we're looking to host it on Google's servers. Should we use App Engine or set up a web server using Compute Engine? If we opt for App Engine, we'll have to choose a programming langua ...

When I click on my Material UI avatar image, it seems to override the button I had in

Every time I try to create a basic category filter with images, I encounter a frustrating bug. After examining the console log, it seems that when I click on the button, my image is being clicked and behaving like an image. However, when I click around the ...

switching the content of a button when it is clicked

I am currently using Angular 7 and I am trying to achieve a functionality where the text on a button changes every time it is clicked, toggling between 'login' and 'logout'. Below is the code snippet I have been working on: typescript ...

Is there a way to update the color of a button once the correct answer is clicked? I'm specifically looking to implement this feature in PHP using CodeIgniter

Within my interface, I have multiple rows containing unique buttons. Upon clicking a button, the system verifies if it corresponds to the correct answer in that specific row. The functionality of validating the responses is already functional. However, I a ...

Can you modify the color of the dots within the letters "i"?

Is there a method to generate text like the one shown in the image using only css/html (or any other technique)? The dots in any "i's" should have a distinct color. Ideally, I'd like this to be inserted via a Wordpress WYSIWYG editor (since the ...