我使用了
Perfect Scrollbar,然后我开始使用Angular 2,但我找不到类似的添加.在Angular 2项目中实现完美滚动条的正确方法是什么?
我跟着this great example但我很遗憾如何改变ngOnInit()
jQuery(this.elementRef.nativeElement).draggable({containment:'#draggable-parent'});
到这个=>
$(function() { $('#Demo').perfectScrollbar();
解决方法
您可以使用vanilla js在自定义指令中初始化完美的滚动条(因为它支持它;-)),如下所示:
import Ps from 'perfect-scrollable'; @Directive({ selector: '[ps]' }) export class PsDirective { constructor(private elementRef:ElementRef) { } ngAfterViewInit() { Ps.initialize(this.elementRef.nativeElement); } }
您可以像这样使用/应用它:
@Component({ selector: 'app' template: ` <div ps class="container"> <div class="content"></div> </div> `,styles: [` .content { background-image: url('https://noraeSAE.github.io/perfect-scrollbar/azusa.jpg'); width: 1280px; height: 720px; } .container { position: relative; margin: 0px auto; padding: 0px; width: 600px; height: 400px; } `],directives: [ PsDirective ] }) export class App { }
必须在此之前配置库(css和SystemJS):
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.perfect-scrollbar/0.6.11/css/perfect-scrollbar.min.css"/> <script> System.config({ transpiler: 'typescript',typescriptOptions: { emitDecoratorMetadata: true },map: { 'perfect-scrollable': 'https://cdnjs.cloudflare.com/ajax/libs/jquery.perfect-scrollbar/0.6.11/js/min/perfect-scrollbar.min.js' },packages: { 'app': { defaultExtension: 'ts' } } }); System.import('app/main') .then(null,console.error.bind(console)); </script>
看到这个plunkr:https://plnkr.co/edit/S8DJpHFVNFioklTl1xg6?p=preview.