微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – Angular2可滚动内容

我使用了 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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


jQuery表单验证提交:前台验证一(图文+视频)
jQuery表单验证提交:前台验证二(图文+视频)
jQuery如何实时监听获取input输入框的值
JQuery怎么判断元素是否存在
jQuery如何实现定时重定向
jquery如何获取复选框选中的值
jQuery如何清空form表单数据
jQuery怎么删除元素节点
JQuery怎么循环输出数组元素
jquery怎么实现点击刷新当前页面