Is there a way to change the font size with a click in JavaScript or Angular?

Here is a breakdown of the 4 steps:

1.) Begin by clicking on a category

2.) The filtered products will be displayed

3.) Select the desired products from the filter

4.) Once selected, the products will appear in the rightmost part of the screen within the 3rd child component

The goal is to have an interactive experience where clicking on a product (in step 3) triggers its addition to the 'right' component and initiates a font size animation. For example, increasing the font size to 28 and then reducing it back to 18.

The service used for adding products to the 3rd component is shared among the child components, facilitating this functionality.

Thank you all for your help!

Answer №1

To start, insert a fresh guideline into the order-quantity-number category:

transition: font-size 1s;

Next, establish an additional selector in your stylesheet:

.order-quantity-number.selected {
   font-size: 48px;
}

Afterwards, simply assign the 'selected' class to the span element and watch the font size smoothly change. Upon completion of the animation after 1 second, remove the class from the element for the text to revert back. I trust this addresses your inquiry :)

UPDATE: Instructions for Implementation

Template:

Incorporate a reference to the span element for convenient access within the code.

<span class="order-quantity-number" #ref>{{receiptItem.quantity}}</span>

ts:

Add the following line to utilize 'ref' from the template within the class.

@ViewChild('ref') elRef: ElementRef;

Integrate a setTimeout() invocation inside the click handler to initiate the animation by removing the selected class after a duration of 1 second:

onClick() {
    ...
    // 1. apply 'selected' class to the span element
    this.elRef.nativeElement.classList.add('selected');

    // 2. eliminate it after 1 second
    setTimeout(() => {
      this.elRef.nativeElement.classList.remove('selected');
    }, 1000);
}

Answer №2

If you want to create a simple @Directive that implements the AfterViewInit interface, you can add a class with a larger font-size and then watch for the transitionend event to remove the class.

Here is an example of how to do this:

@Directive({
    selector: `[fontAnimation]`
})
export class FontAnimationDirective implements AfterViewInit {
    constructor(
        private hostElement: ElementRef
    ) { }

    ngAfterViewInit() {
        const el = this.hostElement.nativeElement as HTMLElement;
        el.classList.add('animate-font-size');
        el.addEventListener('animationend', (ev: TransitionEvent) => {
            if (ev.propertyName == 'font-size') {
                el.classList.remove('animation-font-size');
            }
        })
    }
}

Keep in mind that transitionend will trigger events for every property with a transition, so make sure to check if the propertyName is equal to font-size.

All you have to do now is create the appropriate css class and don't forget to import it into the correct NgModule.

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

What is the best way to implement a function on every item within a specific class?

I'm new to coding and I have a website that showcases job listings. I want to create a function that dynamically changes the logo of a company based on the first 50 characters of the company name mentioned. The current function only works for the firs ...

A method for displaying information in HTML by implementing ng-repeat in AngularJS

I have retrieved JSON data in my controller as a response and now I need to display it in the HTML. Here is what I have implemented: Inside my controller: .controller('DataImportControl', ['$scope','$http', '$location& ...

Insert an element into a JSON collection

I am currently working on a JavaScript function that utilizes an ajax call to retrieve data from an API in the form of a JSON array. Here is a snippet of the array structure that I receive: [ { "ErrorType": "Errors", "Explanations": [ { ...

Adjust the background color of the dropdown menu

I'm struggling to change the background color of the property address picker dropdown from transparent to white. I've been trying different things but can't seem to get it right. Any suggestions? Additionally, I want to change the cursor to ...

Add HTML content to an element within a directive and create a custom function to manipulate it locally

In my AngularJS application, I am faced with a variety of complex inputs scattered throughout. Some inputs utilize directives for autocompletion functionality, like those powered by Google Places or Twitter Bootstrap. I am currently exploring the concept ...

Issue encountered in node_modules/@ngrx/store/src/models.d.ts(58,58): TypeScript error TS2304 - Unable to locate identifier 'unknown'

I am currently exploring the implementation of the REDUX Pattern in my upcoming Angular project, but I am facing issues with importing the necessary libraries. ERROR in node_modules/@ngrx/store/src/models.d.ts(58,58): error TS2304: Cannot find name &apo ...

Experiencing difficulties with the alignment of Bootstrap columns

I'm encountering an issue with displaying 3 Bootstrap columns in the ContentArea. Even though I can locate them using Developer tools, they seem to be positioned at the bottom of the ContentArea, making them not visible. I've attempted adjusting ...

The attempt to create the property 'and_ff' on the string 'and_chr 89' has failed

Encountering an issue with a Lambda function, I receive an error that does not occur when running the same code within an Express app. I'm puzzled. Data returned by caniuse.getLatestStableBrowsers(); [ 'and_chr 89', 'and_ff 86& ...

I have encountered an issue while utilizing dynamic form functionality in Angular 7. The error message displayed is: "Error: Cannot find control with name: 'i'"

While working with Angular 7 dynamic forms, I encountered an error that I'm struggling to resolve. As a newcomer to Angular, this has been quite challenging for me. import { Component } from '@angular/core'; import {FormBuilder, FormArray} ...

Subheaders that stay in place while scrolling through a table with a stationary header

My goal is to design a table with a fixed header that allows the body to scroll under the header. While this is a common design, I am facing the challenge of implementing sticky subheaders. These subheaders should scroll along with the table until they rea ...

Issue with Jest mock function failing to trigger axios instance function causing it to return undefined

I initially found a solution on this StackOverflow thread However, I wanted to add my own helper function that generates a fresh Axios instance with the user's authentication token. Here is what I came up with: import axios from "axios"; c ...

Toggle the jQuery class off when a different element is chosen

I've created a function that toggles a class to change the style of an element when it is selected. However, I'm facing an issue where the new style remains even when another element is selected. Is there a way to make the styling revert back to ...

What is the method for dynamically updating and showcasing a JSON file upon the click of a button?

I'm currently developing an add-on that will display a panel with checkboxes and a save button when a toolbar button is clicked. The goal is to allow users to select checkboxes, then save the selected data in a JSON file that can be accessed and updat ...

Is it possible to define a variable within a JavaScript function and then access it outside of the function?

I have a Node.js application where I need to define a variable inside a function and access its value outside the function as well. Can someone provide guidance on how to achieve this in my code? var readline = require('readline'); var rl = read ...

Altering the status of a property in an object, at a specific position within a collection of objects, contained within another object?

Currently using React and having some trouble with the setState hook to update a value deep within an object structure ...

Troubleshooting issue with Highcharts 3D rendering after using setState()

When working on implementing a 3d pie chart in React using react highchart, I encountered an issue. Whenever I utilize this.setState() inside the lifecycle method componentDidMount(), the 3d chart shifts from its original position to the right diagonally. ...

Angular 5 - Error: Uncaught ReferenceError: req variable is not defined

After upgrading my Angular project from version 4.4.3 to 5, I made sure to update all angular packages to the latest Angular 5.0.0 as per the guidelines provided. Additionally, I updated the angular CLI to version 1.5.0. However, ever since this update, I ...

Dealing with a syntax error in JavaScript (Codecademy)

I have been working my way through the JavaScript course on Codeacademy and for the most part, I've been able to figure things out on my own. However, I've hit a roadblock with my code and can't seem to get it right. Here is the code I&apos ...

Timing issues with setInterval and setTimeout are causing them to execute at the incorrect moments

I am struggling with changing the background image using setInterval and setTimeout every x seconds. The issue I am facing is that the timer is not working as intended, causing images to change instantly instead. let images = ['background1.jpg&apo ...

Apache causes HTML download tag to malfunction

I have an HTML file that includes the following code: <a href="/Library/WebServer/Documents/file.zip" download="file.zip"> Download here </a> When I test this HTML page on Chrome, it successfully allows me to download the file. However, when ...