Angular中使用better-scroll
better-scroll的使用?? 安装better-scroll1: npm install better-scroll --save 2: 安装types npm install better-scroll @types/better-scroll --save 3:在angular-cli里面引入 listscroll组件的编写??根据官方的文档可以看出,better-scroll对dom的结构是有要求的,最外层的wrapper那一层是需要固定高度的,里面那一层content是根据内容的高度来撑起的。
<div class="scroll" #scroll> <ng-content></ng-content> </div> ??ng-content就是要投影进来的内容
1: import引入 BScroll 2:在OnInit这个钩子里面来初始化,由于OnInit的时候,ngFor还没执行完毕,所以就加了一个定时器来延迟。 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还有很多功能,可以依赖它做上拉和下拉的加载,做轮播图等等,具体可参考官方的文档。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |