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

generate code to automatically output the content of a website

I'm in the process of creating a program that automatically navigates to a website and prints a page. Unfortunately, I'm encountering difficulties in getting it to function properly. I've attempted using the selenium chrome driver, but with ...

Tips for extracting information from JSON data in the provided API example

{"Dina-Kar":{"com":"available"},"DinaKarPool":{"com":"available"},"DinaKarStore":{"com":"available"},"DinaKarOnline":{"com":"available"},"DinaKarParts":{"com":"available"},"DinaKarJack":{"com":"available"},"DinaKarTunes":{"com":"available"},"DinaKarSmart": ...

Is there a way to link multiple GET requests and combine their results into an array in JavaScript/Node?

The current code is a bit messy, and I'm still unsure about the then method. What each 'get' call returns: An array with some, but not all, results. What I need: To pass two different URIs, concatenate the results of both, and then expor ...

Leveraging Selenium for extracting data from a webpage containing JavaScript

I am trying to extract data from a Google Scholar page that has a 'show more' button. After researching, I found out that this page is not in HTML format but rather in JavaScript. There are different methods to scrape such pages and I attempted t ...

When the click event is triggered, the second function guess() does not execute

I am currently developing a number guessing application. It consists of two functions, the first one called startGame() works correctly (it receives the maximum number and then disappears by adding the hidden class). However, the second function that is ...

Encountering the identical error message while attempting to execute my application: "Uncaught TypeError: this.microservicesModule.register is not a function."

Despite my repeated attempts to run the application, I am consistently encountering the following error message: "UnhandledPromiseRejectionWarning: TypeError: this.microservicesModule.register is not a function at NestApplication.registerModules ...

Is there a way for me to retrieve the locator value of a webelement?

How can I retrieve the selector value used in a selenium webelement in javascript? Let's say I have the following object: var el = browser.driver.findElement(by.id('testEl')); I want to extract the text 'testEl' from this object ...

AngularJS does not support the use of $(this) syntax

I have encountered an issue while developing a Chrome extension using AngularJS. I would like to add buttons to my popup page, and I want the ancestor node to disappear when a button is clicked. Here is the code snippet: in popup.html <div class="dea ...

When I try to expand the width of a div, the display property set to inline

Visit my website At the moment, my website features the following code snippet: <div class="main-sale"> <div class="time-left">Sale Ends in: </div> <div class="sale-item"> //Highlighted Sale it ...

Tips for expanding the width of an image when employing position:relative

Below is the code snippet: <table border="1" cellpadding="2" cellspacing="2" height="250"> <tbody> <tr> <td> <div style="background: url('path to image'); width: 300px; height: 250px; position: ...

Unravel JSON using JavaScript on the web

I encountered an issue while running this code and trying to decode it: var data = JSON.parse({"forms":[{"url":"example.com/example","name":"example"}]}) document.getElementById("name").innerHTML=data.forms.name Instead of the expected value, the returne ...

Using Javascript to send key codes to an HTML <textarea> element

I'm having trouble figuring out how to initiate a keydown event on a textarea element. For example, I have two textarea elements and I want the second box to display whatever is typed in the first one. However, for some reason, I need to do this using ...

Having trouble getting Vue to properly focus on an input field

I am attempting to use this.$refs.cInput.focus() (cInput being a ref) but it seems to not be functioning as expected. I expect that when I press the 'g' key, the input field should appear and the cursor should immediately focus on it, allowing me ...

Trouble with Background Image Display in Internet Explorer 8

I have been attempting to set a background image for the . element, but it is not displaying correctly. The image shows up in Firefox and Chrome, but not in Internet Explorer. I have included a link to my website and relevant CSS code below. Can anyone pro ...

Struggling with the development of a crossfading image gallery using jQuery's .animate() function while ensuring compatibility with IE8

Seeking assistance in creating a crossfading image gallery using jQuery and the .animate() function. I'm struggling to solve the issue of smooth fadeIn for the next image while maintaining compatibility with IE8. https://jsfiddle.net/Vimpil/fqhc1e9m/ ...

Issue with JSONP success function not being triggered

Here is an example of an Ajax call being made to a different domain: <script> $.ajax({ url: 'url?callback=?', dataType: 'jsonp', success: function (data) { alert(data[0].DeviceName) ...

Ways to automatically display the date upon loading the view

I have integrated a plugin with Angular, which is essentially an extension. Upon loading the view, only an empty input is visible. Clicking on the input reveals a calendar with today's date displayed. My objective is to automatically load today&apos ...

The deployment on heroku encountered an error during the build process

I'm attempting to deploy my React application on Heroku, but I keep encountering the following errors: -----> Installing dependencies Installing node modules npm ERR! code ERESOLVE npm ERR! ERESOLVE could not resolve npm ...

Showing error messages in Angular when a form is submitted and found to be invalid

My form currently displays an error message under each field if left empty or invalid. However, I want to customize the behavior of the submit button when the form is invalid. <form #projectForm="ngForm" (ngSubmit)="onSubmit()"> ...

Error message: "Unable to locate HTML container when trying to create a child element in am

Whenever I navigate away from this page and then come back, the chart does not load. Instead, it displays an error message saying html container not found at createChild However, if I refresh the page, the chart will appear again. The issue seems to be i ...