Angular5 自定义scrollbar样式之 ngx-perfect-scollbar

本文详细介绍了在Angular项目中如何正确使用ngx-perfect-scrollbar组件来实现平滑滚动效果,包括版本选择、安装步骤、模块导入、模板使用、CSS引入及路由跳转后滚动条更新的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版本

  • angular 5.0
  • ngx-perfect-scrollbar ^5.3.5

为什么不用 ngx-perfect-scrollbar 最新的版本 v7 呢?

因为它报错啊!!! 每次init的时候,就报一个Object() is not a function。 根据GitHub上热心网友的建议,就downgrade了。

详见:https://github.com/zefoy/ngx-perfect-scrollbar/issues/189

725304-20181130143000632-1474504105.png

安装

npm 安装没啥好说的
npm install --save ngx-perfect-scrollbar@^5.0.0

使用

1. 导入module

// app.module.ts
import { PerfectScrollbarModule, PerfectScrollbarConfigInterface, PERFECT_SCROLLBAR_CONFIG } from 'ngx-perfect-scrollbar';

const DEFAULT_PERFECT_SCROLLBAR_CONFIG: PerfectScrollbarConfigInterface = {
  wheelPropagation: true
};

@NgModule({
  bootstrap: [
    AppComponent
  ],
  declarations: [
    ... 
  ],
  imports: [
    ..., 
    PerfectScrollbarModule
  ],
  exports: [
  ],
  providers: [
    {
      provide: PERFECT_SCROLLBAR_CONFIG,
      useValue: DEFAULT_PERFECT_SCROLLBAR_CONFIG
    }
  ]
})
export class AppModule {}

Providing the global configuration is optional and when used you should only provide the configuration in your root module.
提供全局配置是可选的,但是如果你需要使用,那么只需要在应用的root module中配置。

2. 在模板中使用

API 提供了两种使用方式,一种是 Component 使用方式,一种是 Derective使用方式。我在项目中用的后者。


<ul *ngIf="nodeList.length" class="indented mTop scroll"  [style.max-height.px]="treeContentHeight" [perfectScrollbar]="config">
        <li class="no_line" *ngFor="let node of nodeList"></li>
</ul>                
import { PerfectScrollbarConfigInterface, PerfectScrollbarDirective } from 'ngx-perfect-scrollbar';

public config: PerfectScrollbarConfigInterface = {};
@ViewChild(PerfectScrollbarDirective) directiveRef?: PerfectScrollbarDirective;

3. 导入CSS

// styles.scss
@import '~perfect-scrollbar/css/perfect-scrollbar.css';

效果:
725304-20181130145515561-987968667.png

遇到的问题

上中下结构中,只让 content部分有滚动条,但是路由跳转后,滚动条的高度没有重新计算。

解决方案:

// home.component.html
<div class="container">
    <div class="header">
        <app-header></app-header>
    </div>
    <div class="content" [perfectScrollbar]="config">
        <router-outlet></router-outlet>
    </div>
    <div class="footer">
        <app-footer></app-footer>
    </div>
</div>

在路由跳转后,需要调用update()更新scrollbar的size 和 position

import { Component, OnInit, ViewChild } from '@angular/core';
import { Router} from '@angular/router';
import { PerfectScrollbarConfigInterface, PerfectScrollbarDirective } from 'ngx-perfect-scrollbar';


@Component({
    selector: 'app-home',
    templateUrl: './home.component.html',
    styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
    public config: PerfectScrollbarConfigInterface = {};
    @ViewChild(PerfectScrollbarDirective) directiveRef?: PerfectScrollbarDirective;
    constructor(public _router: Router) { }

    ngOnInit() {
        this._router.events.subscribe((event) => {
            // update the scrollbar
            this.directiveRef.update();
          });
    }

}

转载于:https://www.cnblogs.com/liulei-cherry/p/10044159.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值