Angular5 自定义scrollbar样式之 ngx-perfect-scollbar
版本
- 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

安装
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';
效果:

遇到的问题
上中下结构中,只让 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();
});
}
}
Angular5 自定义scrollbar样式之 ngx-perfect-scollbar的更多相关文章
- Angular5 自定义scrollbar样式之 ngx-malihu-scrollbar
简介 此插件是 Malihu jQuery Scrollbar 为了在 Angular2+ 环境下使用,封装的一个ts的版本.提供directive和service. 从安装量来看,它比不过 perf ...
- Android必知必会-自定义Scrollbar样式
如果移动端访问不佳,请使用–>GitHub版 背景 设计师给的设计图完全依照 IOS 的标准来的,导致很多细节的控件都得自己重写,最近的设计图中有显示滚动条,Android 默认的滚动条样式(带 ...
- jQuery自定义滚动条样式插件mCustomScrollbar
如果你构建一个很有特色和创意的网页,那么肯定希望定义网页中的滚动条样式,这方面的 jQuery 插件比较不错的,有两个:jScrollPane 和 mCustomScrollbar. 关于 jScro ...
- 自定义scrollbar
Chrome ::-webkit-scrollbar 整体部分 ::-webkit-scrollbar-track 轨道 ::-webkit-scrollbar-track-piece 内层轨道 :: ...
- WPF 自定义滚动条样式
先看一下效果: 先分析一下滚动条有哪儿几部分组成: 滚动条总共有五部分组成: 两端的箭头按钮,实际类型为RepeatButton Thumb 两端的空白,实际也是RepeatButton 最后就是Th ...
- scrollbar样式设置
转载:https://segmentfault.com/a/1190000012800450?utm_source=tag-newest author:specialCoder 一 前言 在CSS 中 ...
- 自定义滚动条样式-transition无效
问题 需求是自定义滚动条样式,然后2秒内无操作隐藏滚动条. 2s内隐藏比较麻烦,不能用css实现,只能监听容器的touch事件,然后给滚动条加个opacity: 0的class. .class::-w ...
- CSS3:scrollbar样式设置
CSS3:scrollbar样式设置 1. 设置出现滚动条的方式 overflow:scroll --- x和y方向都会出现滚动条 或者 overflow-x:scroll --- 只有x方向出现滚动 ...
- Siteserver-stl:searchOutput(搜索结果)自定义显示样式
stl:searchOutput 自定义显示样式 自定义搜索提交表单需要在<stl:searchOutput>中嵌入显示搜索结果的标签,必须包含的标签 有<stl:pageConte ...
随机推荐
- EXE中释放DLL中分配的内存
在DLL中分配的内存,如果到其调用者中释放,可能会出现CRASH的情况,其原因在于: 在DLL中的Code Generation如果是采用了MT(静态加载LIBCRTD.LIB)在该库中维护了一个al ...
- Luogu P2511 [HAOI2008]木棍分割 二分+DP
思路:二分+DP 提交:3次 错因:二分写萎了,$cnt$记录段数但没有初始化成$1$,$m$切的次数没有$+1$ 思路: 先二分答案,不提: 然后有个很$naive$的$DP$: 设$f[i][j] ...
- python 学习资料 常用
https://www.cnblogs.com/vamei/archive/2012/09/13/2682778.html 利用python 进行数据分析第二版 https://www.jianshu ...
- HDU6223 Infinite Fraction Path bfs+剪枝
Infinite Fraction Path 这个题第一次看见的时候,题意没搞懂就没做,这第二次也不会呀.. 题意:第i个城市到第(i*i+1)%n个城市,每个城市有个权值,从一个城市出发走N个城市, ...
- Leetcode题目279.完全平方数(动态规划-中等)
题目描述: 给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, ...)使得它们的和等于 n.你需要让组成和的完全平方数的个数最少. 示例 1: 输入: n = 12 输出: 3 解 ...
- LeetCode347——优先队列解决查询前k高频率数字问题
给定一个非空的整数数组,返回其中出现频率前 k 高的元素. 例如, 给定数组 [1,1,1,2,2,3] , 和 k = 2,返回 [1,2]. 注意: 你可以假设给定的 k 总是合理的,1 ≤ k ...
- NUnit -- Test discovery or execution might not work for this project
[7/31/2019 2:06:58.100 PM Warning] No test matches the given testcase filter `FullyQualifiedName=Sil ...
- 报错1251 - Client does not support authentication protocol 解决办法
# 1.容器中登录mysql,查看mysql的版本 status; # 2,进行授权远程连接(注意mysql 8.0跟之前的授权方式不同) GRANT ALL ON *.* TO 'root'@'%' ...
- 性能测试 | 记一次生产数据库sql由451s优化为0.4s的过程
概述 最近开发说某个接口跑的很慢,排查了下发现其中一条sql,数据量不大,但居然要跑451s,下面简单记录一下优化的过程. 问题sql SELECT l.location_gid ENUMVALUE, ...
- 1.springAOP原理分析
环境:jdk1.8 + spring boot 2.0.9.RELEASE Spring AOP的实现本质上就是代理Proxy + 一系列的拦截器 使用@Aspect,引入依赖 <depende ...