代码拉取完成,页面将自动刷新
<virtual-scroll-viewport #viewport [list]="list" [itemSize]="30" style="height: 300px;">
<ul>
<li class="item" *ngFor="let item of viewport.vList" style="height: 30px;">{{item}}</li>
</ul>
</virtual-scroll-viewport>
参数 | 说明 | 类型 | 默认值 |
---|---|---|---|
[list] | 列表数据源 | any[] | |
[itemSize] | 行高 | number | |
[maxBuffer] | 最大缓冲尺寸 | number | 默认为视口的高度 |
[minBuffer] | 最小缓冲尺寸, 滚动时小于该值将更新视图 | number | 默认为视口高度的一半 |
[disabled] | 是否禁用虚拟滚动 | boolean | false |
(update) | 视图更新事件 | EventEmitter |
<virtual-scroll-viewport (scroll)="updateVList()">
<div class="virtual-scroll-content-wrapper" #wrapper style="margin-top: {{marginTop}}px;">
<ng-content></ng-content>
</div>
<div class="virtual-scroll-content-spacer" [style.height.px]="totalHeight"></div>
</virtual-scroll-viewport>
vList
// 滚动事件
updateVList() {
// 标识是否需要更新
needUpdate = false;
if (内容向上滚动) {
needUpdate = bottomBuffer(底部缓冲区) < minBuffer;
} else {
needUpdate = topBuffer(顶部缓冲区) < minBuffer;
}
if (!needUpdate) return;
// 切割出需要展示的内容
start = Math.max(scrollTop - maxBuffer, 0) / this.itemSize;
end = (scrollTop + viewportClientHeight + maxBuffer) / this.itemSize;
this.vList = this.list.slice(start, end);
// 设置视口内容偏移
this.marginTop = start * this.itemSize;
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。