angular - 当元素进入 View 时触发 CountUp 功能

标签 angular typescript

我正在使用这个库

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

有没有办法只在到达数字部分时激活计数动画

换句话说,当元素滚动到 View 中时触发它( <h1 [countUp]="345">0</h1> )

那可能吗?

最佳答案

Demo Viewchild是一种方法。

在 html 中将值设为参数。

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

在组件中
@ViewChild('counter') counter: ElementRef;
param=0;

使用 hoSTListener 监听滚动并检查滚动是否到达 html 如果是,则将计数更改为 365
@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
}

关于angular - 当元素进入 View 时触发 CountUp 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62242899/

相关文章:

javascript - Angular 2 - 找不到服务 worker - 错误的 HTTP 响应代码 (404)

Angular 12 TypeError : Cannot read properties of null (reading 'length' ) at HttpHeaders. applyUpdate

visual-studio-2013 - Visual Studio 2013 中的 Typescript 类和接口(interface)突出显示

angular - Typescript/Angular 中具有 0 个元素的函数返回映射

angular - Ngx-Datatable 无法访问模板中的行

angular - 升级到 Angular 10 会引发与 @ng-select/ng-select 相关的错误(不兼容的对等依赖项)

javascript - 如何在没有 Node.js/NPM 的情况下将我的 Web 应用程序转换为 Angular 4

angular - 如何在Angular2中动态更改按钮属性

asp.net - 找不到模块 '@angular/core' Visual Studio 2017

angular - 订阅combineLatest中的所有可观察量