因为须要在一个固定的的高度作无限滚动,原本css的overflow-y也能够完成的,奈何安卓不是很流畅,还很生硬,就是用了第三方库better-scroll,配合angular的ng-content。angular的ng-content和vue的插槽很像,里面一些不肯定的内容能够经过ng-content投影进去。javascript
1: npm install better-scroll --savecss
2: 安装types npm install better-scroll @types/better-scroll --savehtml
3:在angular-cli里面引入vue
根据官方的文档能够看出,better-scroll对dom的结构是有要求的,最外层的wrapper那一层是须要固定高度的,里面那一层content是根据内容的高度来撑起的。java
<div class="scroll" #scroll> <ng-content></ng-content> </div>
ng-content就是要投影进来的内容npm
1: import引入 BScrollapp
2:在OnInit这个钩子里面来初始化,因为OnInit的时候,ngFor还没执行完毕,因此就加了一个定时器来延迟。dom
import { Component, OnInit, Input, ElementRef, ViewChild } from '@angular/core'; declare let BScroll; @Component({ selector: 'app-listscroll', templateUrl: './listscroll.component.html', styleUrls: ['./listscroll.component.css'] }) export class ListscrollComponent implements OnInit { @ViewChild('scroll') scrollEl: ElementRef; @Input() private height: number; public scroll; constructor() { } ngOnInit() { // 设置高度 this.scrollEl.nativeElement.style.height = `${this.height}px`; // 初始化 setTimeout(() => { this.scroll = new BScroll(this.scrollEl.nativeElement, {click: true}); }, 20); } }
<app-listscroll [height]="height"> <ul> <li class="item" *ngFor="let item of list; let num = index;">第{{num}}个</li> </ul> </app-listscroll>
这样better-scroll简单的使用就完成,固然better-scroll还有不少功能,能够依赖它作上拉和下拉的加载,作轮播图等等,具体可参考官方的文档。this