UNPKG

ng-cw-v12

Version:

Angular UI component library

140 lines (131 loc) 9.05 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/virtual-scroll', ['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"]["virtual-scroll"] = {}), global.ng.core, global.ng.common)); })(this, (function (exports, i0, i1) { '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 i1__namespace = /*#__PURE__*/_interopNamespace(i1); var VirtualScrollComponent = /** @class */ (function () { function VirtualScrollComponent(ngZone) { this.ngZone = ngZone; this.ncItems = []; this.ncBufferCount = 2; //缓冲item数量,前后各2项 this.visibleItems = []; //显示items this.totalPadding = 0; //总填充高度 this.topPadding = 0; this.scrollTop = 0; //已滚动高度 this.itemHeight = 1; //若设为0,下方代码获取不到this.templateContainer this.viewportHeight = 0; //视窗可视高度 } VirtualScrollComponent.prototype.ngAfterViewInit = function () { var element = this.templateContainer.nativeElement.firstChild; var styles = getComputedStyle(element); this.itemHeight = element.offsetHeight + parseFloat(styles.marginTop) + parseFloat(styles.marginBottom); //先获取item高度 this.viewportHeight = this.viewport.nativeElement.clientHeight; //视窗可视高度 this.updateVisibleItems(); this.updateTotalPadding(); }; VirtualScrollComponent.prototype.ngOnChanges = function (changes) { if (changes['ncItems']) { this.updateTotalPadding(); this.updateVisibleItems(); } }; VirtualScrollComponent.prototype.onScroll = function () { var _this = this; this.ngZone.run(function () { _this.scrollTop = _this.viewport.nativeElement.scrollTop; _this.updateVisibleItems(); }); }; VirtualScrollComponent.prototype.updateVisibleItems = function () { var firstVisibleIndex = Math.floor(this.scrollTop / this.itemHeight); var startIndex = Math.max(0, firstVisibleIndex - this.ncBufferCount); var visibleItemsCount = Math.ceil(this.viewportHeight / this.itemHeight); var endIndex = Math.min(firstVisibleIndex + visibleItemsCount + this.ncBufferCount, this.ncItems.length - 1); this.visibleItems = []; for (var i = startIndex; i <= endIndex; i++) { this.visibleItems.push({ index: i, data: this.ncItems[i] }); } this.topPadding = startIndex * this.itemHeight; }; VirtualScrollComponent.prototype.updateTotalPadding = function () { this.totalPadding = this.ncItems.length * this.itemHeight; }; //采用trackBy提升性能,只渲染改变的dom VirtualScrollComponent.prototype.trackByIndex = function (index, item) { return item.index; }; return VirtualScrollComponent; }()); VirtualScrollComponent.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: VirtualScrollComponent, deps: [{ token: i0__namespace.NgZone }], target: i0__namespace.ɵɵFactoryTarget.Component }); VirtualScrollComponent.ɵcmp = i0__namespace.ɵɵngDeclareComponent({ minVersion: "12.0.0", version: "12.1.5", type: VirtualScrollComponent, selector: "nc-virtual-scroll", inputs: { ncItems: "ncItems", ncItemTemplate: "ncItemTemplate", ncBufferCount: "ncBufferCount" }, viewQueries: [{ propertyName: "viewport", first: true, predicate: ["viewport"], descendants: true, static: true }, { propertyName: "templateContainer", first: true, predicate: ["templateContainer"], descendants: true }], usesOnChanges: true, ngImport: i0__namespace, template: "<div class=\"viewport\" #viewport (scroll)=\"onScroll()\">\r\n <div class=\"total-padding\" [style.height.px]=\"totalPadding\">\r\n <div class=\"item-container\" [style.transform]=\"'translateY(' + topPadding + 'px)'\">\r\n <div #templateContainer *ngFor=\"let item of visibleItems; trackBy: trackByIndex\">\r\n <ng-container [ngTemplateOutlet]=\"ncItemTemplate\" #templateContainer \r\n [ngTemplateOutletContext]=\"{$implicit: item.data, index: item.index}\"></ng-container>\r\n </div>\r\n </div>\r\n </div>\r\n</div>", styles: [".viewport{width:100%;height:100%;overflow-y:auto;position:relative}.viewport::-webkit-scrollbar{width:6px;height:6px}.viewport::-webkit-scrollbar-thumb{background-color:#cdc9cf;border-radius:10px;-webkit-transition:background-color .3s ease;transition:background-color .3s ease}.viewport::-webkit-scrollbar-thumb:hover{background-color:#766d7b}.viewport::-webkit-scrollbar-track{background:#0000;cursor:pointer}\n"], directives: [{ type: i1__namespace.NgForOf, selector: "[ngFor][ngForOf]", inputs: ["ngForOf", "ngForTrackBy", "ngForTemplate"] }, { type: i1__namespace.NgTemplateOutlet, selector: "[ngTemplateOutlet]", inputs: ["ngTemplateOutletContext", "ngTemplateOutlet"] }] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: VirtualScrollComponent, decorators: [{ type: i0.Component, args: [{ selector: 'nc-virtual-scroll', templateUrl: './virtual-scroll.component.html', styleUrls: ['./virtual-scroll.component.less'] }] }], ctorParameters: function () { return [{ type: i0__namespace.NgZone }]; }, propDecorators: { ncItems: [{ type: i0.Input }], ncItemTemplate: [{ type: i0.Input }], ncBufferCount: [{ type: i0.Input }], viewport: [{ type: i0.ViewChild, args: ['viewport', { static: true }] }], templateContainer: [{ type: i0.ViewChild, args: ['templateContainer'] }] } }); var NcVirtualScrollModule = /** @class */ (function () { function NcVirtualScrollModule() { } return NcVirtualScrollModule; }()); NcVirtualScrollModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcVirtualScrollModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule }); NcVirtualScrollModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcVirtualScrollModule, declarations: [VirtualScrollComponent], imports: [i1.CommonModule], exports: [VirtualScrollComponent] }); NcVirtualScrollModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcVirtualScrollModule, imports: [[ i1.CommonModule ]] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.1.5", ngImport: i0__namespace, type: NcVirtualScrollModule, decorators: [{ type: i0.NgModule, args: [{ declarations: [ VirtualScrollComponent ], imports: [ i1.CommonModule ], exports: [ VirtualScrollComponent ] }] }] }); /** * Generated bundle index. Do not edit. */ exports.NcVirtualScrollModule = NcVirtualScrollModule; exports.VirtualScrollComponent = VirtualScrollComponent; Object.defineProperty(exports, '__esModule', { value: true }); })); //# sourceMappingURL=ng-cw-v12-virtual-scroll.umd.js.map