此插件是 Malihu jQuery Scrollbar 为了在 Angular2+ 环境下使用,封装的一个ts的版本。提供directive
和service
。css
从安装量来看,它比不过 perfect-scrollbar,因此我最后也没用它。可是也记录一下用法,万一之后要用呢!html
npm install ngx-malihu-scrollbar --save
我当时安装的版本是 `7.0.0`。
这个插件是依赖jQuery的。node
在_angular-cli.json
中添加jquery
"styles": [ "../src/scss/styles.scss", "../node_modules/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.css" ], "scripts": [ "../node_modules/jquery/dist/jquery.min.js", "../node_modules/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.concat.min.js" ],
在tsconfig.app.json添加shell
{ "compilerOptions": { ... "types": [ + "jquery", + "mcustomscrollbar" ] }, ... }
import { NgModule } from '@angular/core'; import { CommonModule } from '@angular/common'; + import { MalihuScrollbarModule } from 'ngx-malihu-scrollbar'; import { HomeComponent } from './home.component'; @NgModule({ imports: [ CommonModule, + MalihuScrollbarModule.forRoot(), ], declarations: [HomeComponent], })
<ul *ngIf="nodeList.length" class="indented mTop scroll" [style.max-height.px]="treeContentHeight" malihu-scrollbar [scrollbarOptions]="scrollbarOptions"> <li class="no_line" *ngFor="let node of nodeList"></li> </ul>
+ import { MalihuScrollbarService } from 'ngx-malihu-scrollbar'; + public scrollbarOptions: any = { axis: 'y', theme: 'my-theme'}; constructor( private _router: Router, private mScrollbarService: MalihuScrollbarService, ) { } // 更新 setTreeContentHeight() { ... // caculate height // update scroll this.mScrollbarService.update('#scrollContainer'); }
在 styles.css 中自定义样式npm
.mCS-my-theme.mCSB_scrollTools{ .mCSB_dragger{ .mCSB_dragger_bar{ background-color: #DEDEE4 !important; } } .mCSB_draggerRail{ background-color: transparent !important; } }
使用感觉:没有perfect-scrollbar流畅,每次滚动条出现时,会先出现浏览器的默认滚动条,而后才渲染出自定义的滚动条,不完美。故,弃用之~json