Execute the CountUp function when the element becomes visible

Currently, I am implementing the following library:

https://github.com/inorganik/ngx-countUp

Is there a way to activate the counting animation only when the section of numbers is reached?

In other words, can the count be triggered (

<h1 [countUp]="345">0</h1>
) only when the element is scrolled into view?

Is this feature possible to implement?

Answer №1

Check out this Demo. Utilizing ViewChild is a great way to achieve this.

When working in HTML, it's important to make the value parametric.

<h1 #counter [countUp]="param">0</h1>

Within the component:

@ViewChild('counter') counter: ElementRef;
param=0;

By using HostListener, we can listen for scroll events and update the count when necessary (e.g., changing the count to 365 when the scroll reaches a certain point).

@HostListener('window:scroll', ['$event']) // for window scroll events
onWindowScroll(event) {
   var rect = this.counter.nativeElement.getBoundingClientRect();
   var elemTop = rect.top; 
   var elemBottom = rect.bottom;
   (elemTop >= 0) && (elemBottom <= window.innerHeight) ? this.param=365:null
}

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 experiment with transclusion (ng-content) in Angular2?

I have a specific component that I need to test, here is the code: import {Component, OnInit, Input} from "@angular/core"; @Component({ selector: 'column', template: '<ng-content></ng-content>', host: { ...

GitHub Actions causing build failure in Angular project exclusively

I've encountered an issue where the GitHub Action workflow fails to compile an Angular project, even though it works fine on my local machine and that of my colleagues. It's worth noting that I'm using npm ci instead of npm install. The err ...

Using DevExtreme Angular to establish an initial filter value for a DxDataGrid custom headerFilter

What is the best way to set a starting headerFilter value with a custom expression like this? Using filterValues="xxx > 0" does not seem to work. <dxi-column caption="" dataField="xxx" [headerFilter]="headerFilter ...

Combine the information from 3 separate subscriptions into a single object using RxJS in Angular 9

I am seeking assistance with merging data from 3 different sensors into one object. I am using Cordova plugins to retrieve accelerometer, gyroscope, and magnetometer data. However, I am facing an issue in subscribing to all three observables simultaneously ...

Retrieve functions with varying signatures from another function with precise typing requirements

My code includes a dispatch function that takes a parameter and then returns a different function based on the parameter value. Here is a simplified version: type Choice = 'start' | 'end'; function dispatch(choice: Choice) { switch ...

How can I extract an object from an array by using a string key in either Observable or lodash?

How can I retrieve a specific object (show) from Shows based on its id being a string in a given sample? I am transforming the result into an RXJS Observable, so using functionalities from RXJS or lodash would be greatly appreciated. //JSON RETURNED with ...

We were unable to locate the module '@reactflow/core' or its associated type declarations

After forking reactflow, I attempted to make some modifications but encountered a type error even without making any changes. My next step was to try "pnpm i @types/reactflow," but it did not resolve the issue. ...

Neglect certain concealed fields on AG Grid for now

Currently, I am working with Angular and AG-Grid to create a table below. By default, the table looks like this: However, when a user hovers over a row, two hidden buttons will appear as shown here: These buttons are actually associated with the two hid ...

TypeScript conditional return type: effective for single condition but not for multiple conditions

In my code, I have implemented a factory function that generates shapes based on a discriminated union of shape arguments. Here is an example: interface CircleArgs { type: "circle", radius: number }; interface SquareArgs { type: "square" ...

Using Angular's asterisk wildcard to correctly match nested routes

Struggling to locate subroutes using a component const routes: Routes = [ { path: '', component: CatalogueComponent, }, { path: 'search/**', component: CatalogueComponent, }, ... { path: '**', ...

Utilize TypeScript to retrieve the enumeration values as a parameter through a method that employs a generic enum type

Is there a way to retrieve all values of an Enum (specified as a parameter or generic) and return them in a list? Additionally, if the user has a specific role, I only need to retrieve certain Enum values provided as a parameter. I had the idea of groupin ...

Enhancing Responses in NestJS with External API Data

I'm a beginner in NestJs, Graphql, and typescript. I am trying to make an external API call that is essentially a Graphql query itself. The goal is to modify the response, if necessary, and then return it for the original request or query, in this ca ...

Oops! The type error is indicating that you tried to pass 'undefined' where a stream was required. Make sure to provide an Observable, Promise, Array, or Iterable when working with Angular Services

I've developed various services to interact with different APIs. The post services seem to be functioning, but an error keeps popping up: ERROR TypeError: You provided 'undefined' where a stream was expected. Options include Observable, ...

Tips for including an element at the start while creating a map()

enum StatusEnum { accepted = "AC", rejected = "RJ", } const select = (Object.keys(StatusEnum) as Array<keyof typeof StatusEnum>).map((x) => ({ value: x, name: x + "_random", })) /** * Console.log(select) * [ ...

Deploying a nodejs application with angular as the user interface framework using Firebase

Is there a way to set up an express.js application with angular as the front-end framework, multiple route files, and communication between the server and angular via service level API calls, in order to deploy it on firebase using Firebase Hosting? Curre ...

What is the most effective way to receive all values sent to an Observer upon a new subscription?

I have an observer that receives various values emitted to it at different times. For instance sub = new Subject<any>(); sub.next(1); sub.next(2); sub.next(3); #hack 1 sub.next(4); sub.next(5); sub.next(6); #hack 2 If there is a ...

Exploring a Component's props and their data types

As a newcomer to React and Typescript, I have a straightforward question that I can't seem to find an answer to. I'm attempting to construct a tab layout using Typescript with headless UI following the documentation here I am encountering issue ...

Is there a one-click Angular Material UI datepicker that supports date ranges?

After doing some research on Material UI Datepicker with range support, I stumbled upon saturn-datepicker. Saturn-datepicker is exactly what I was looking for. My goal is to have the "end date" automatically set (1 week later than the "begin date") when I ...

What causes the "Error: method not allowed" message to appear when attempting to send a "DELETE" request from a Next Js component? (The POST method is

This is my first time on this platform, and I'm currently following a tutorial from Javascript Mastery to create a clone of a thread application. After watching the entire video and building the basic functionality based on it, I decided to enhance th ...

Retrieve the dimensions of an image once rendering is complete, using Angular

I'm currently working on obtaining the rendered size of an image within a component. By utilizing the (load) event, I can capture the size of the image as it appears at that particular moment (pic1), as well as its "final" size after the page has fini ...