UNPKG

ng-cw-v12

Version:

Angular UI component library

232 lines (223 loc) 10.7 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/common')) : typeof define === 'function' && define.amd ? define('ng-cw-v12/number-ticker', ['exports', '@angular/core', '@angular/common'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory((global["ng-cw-v12"] = global["ng-cw-v12"] || {}, global["ng-cw-v12"]["number-ticker"] = {}), global.ng.core, global.ng.common)); })(this, (function (exports, i0, common) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n["default"] = e; return Object.freeze(n); } var i0__namespace = /*#__PURE__*/_interopNamespace(i0); var NumberTickerComponent = /** @class */ (function () { function NumberTickerComponent(elementRef) { this.elementRef = elementRef; /** 要显示的数字 */ this._value = 0; /** 起始数值 */ this.ncStartValue = 0; /** 开始前的延迟 */ this.ncDelay = 0; /** 小数位数 */ this.ncDecimalPlaces = 0; /** 动画持续时间(毫秒) */ this.ncDuration = 3000; /** 是否在视图可见时才开始动画 */ this._startOnView = false; /** 是否只执行一次动画 */ this._once = false; this.observer = null; this.animationFrame = null; this.timeout = null; this.currentValue = 0; this.displayValue = '0'; } Object.defineProperty(NumberTickerComponent.prototype, "ncValue", { get: function () { return this._value; }, set: function (value) { var _this = this; this._value = value; setTimeout(function () { _this.start(); }); }, enumerable: false, configurable: true }); Object.defineProperty(NumberTickerComponent.prototype, "ncStartOnView", { get: function () { return this._startOnView; }, set: function (val) { this._startOnView = val !== null && val !== undefined && val !== false && val !== 'false'; }, enumerable: false, configurable: true }); Object.defineProperty(NumberTickerComponent.prototype, "ncOnce", { get: function () { return this._once; }, set: function (val) { this._once = val !== null && val !== undefined && val !== false && val !== 'false'; }, enumerable: false, configurable: true }); NumberTickerComponent.prototype.ngOnInit = function () { }; NumberTickerComponent.prototype.ngOnDestroy = function () { this.clear(); }; NumberTickerComponent.prototype.start = function () { var _this = this; this.clear(); // 初始值:无论什么方向,都从startValue开始 this.currentValue = this.ncStartValue; this.updateDisplayValue(); if (!this.ncStartOnView) { this.timeout = setTimeout(function () { _this.startAnimation(); }, this.ncDelay); } else { this.setupIntersectionObserver(); } }; NumberTickerComponent.prototype.clear = function () { if (this.observer) { this.observer.disconnect(); } if (this.animationFrame) { cancelAnimationFrame(this.animationFrame); } if (this.timeout) { clearTimeout(this.timeout); } }; NumberTickerComponent.prototype.setupIntersectionObserver = function () { var _this = this; this.observer = new IntersectionObserver(function (entries) { if (entries[0].isIntersecting) { _this.timeout = setTimeout(function () { _this.startAnimation(); }, _this.ncDelay); if (_this.ncOnce) { if (_this.observer) { _this.observer.disconnect(); } } } else if (!_this.ncOnce) { _this.resetAnimation(); } }, { threshold: 0.1 }); this.observer.observe(this.elementRef.nativeElement); }; NumberTickerComponent.prototype.startAnimation = function () { this.animate(); }; NumberTickerComponent.prototype.resetAnimation = function () { if (this.timeout) { clearTimeout(this.timeout); } if (this.animationFrame) { cancelAnimationFrame(this.animationFrame); } this.currentValue = this.ncStartValue; this.updateDisplayValue(); }; NumberTickerComponent.prototype.animate = function () { var _this = this; var startTime = performance.now(); var startVal = this.currentValue; // 目标值:无论什么方向,都动画到value var targetVal = this.ncValue; var difference = targetVal - startVal; var animateStep = function (currentTime) { var elapsed = currentTime - startTime; var progress = Math.min(elapsed / _this.ncDuration, 1); // 使用缓动函数(ease-out) var easedProgress = 1 - Math.pow(1 - progress, 3); _this.currentValue = startVal + (difference * easedProgress); _this.updateDisplayValue(); if (progress < 1) { _this.animationFrame = requestAnimationFrame(animateStep); } }; this.animationFrame = requestAnimationFrame(animateStep); }; NumberTickerComponent.prototype.updateDisplayValue = function () { this.displayValue = this.currentValue.toFixed(this.ncDecimalPlaces); }; return NumberTickerComponent; }()); NumberTickerComponent.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NumberTickerComponent, deps: [{ token: i0__namespace.ElementRef }], target: i0__namespace.ɵɵFactoryTarget.Component }); NumberTickerComponent.ɵcmp = i0__namespace.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.1.5", type: NumberTickerComponent, selector: "nc-number-ticker", inputs: { ncValue: "ncValue", ncStartValue: "ncStartValue", ncDelay: "ncDelay", ncDecimalPlaces: "ncDecimalPlaces", ncDuration: "ncDuration", ncStartOnView: "ncStartOnView", ncOnce: "ncOnce" }, ngImport: i0__namespace, template: "<span class=\"number-ticker\">{{displayValue}}</span>", styles: [":host{display:inline-block}\n"] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NumberTickerComponent, decorators: [{ type: i0.Component, args: [{ selector: 'nc-number-ticker', templateUrl: './number-ticker.component.html', styleUrls: ['./number-ticker.component.less'] }] }], ctorParameters: function () { return [{ type: i0__namespace.ElementRef }]; }, propDecorators: { ncValue: [{ type: i0.Input }], ncStartValue: [{ type: i0.Input }], ncDelay: [{ type: i0.Input }], ncDecimalPlaces: [{ type: i0.Input }], ncDuration: [{ type: i0.Input }], ncStartOnView: [{ type: i0.Input }], ncOnce: [{ type: i0.Input }] } }); var NcNumberTickerModule = /** @class */ (function () { function NcNumberTickerModule() { } return NcNumberTickerModule; }()); NcNumberTickerModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcNumberTickerModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule }); NcNumberTickerModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcNumberTickerModule, declarations: [NumberTickerComponent], imports: [common.CommonModule], exports: [NumberTickerComponent] }); NcNumberTickerModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcNumberTickerModule, imports: [[ common.CommonModule ]] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcNumberTickerModule, decorators: [{ type: i0.NgModule, args: [{ declarations: [ NumberTickerComponent ], imports: [ common.CommonModule ], exports: [ NumberTickerComponent ] }] }] }); /** * Generated bundle index. Do not edit. */ exports.NcNumberTickerModule = NcNumberTickerModule; exports.NumberTickerComponent = NumberTickerComponent; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=ng-cw-v12-number-ticker.umd.js.map