Hostlistener上的滚动事件 [英] scroll event on Hostlistener

查看:316
本文介绍了Hostlistener上的滚动事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经定义了模板

@Component({
    selector: 'name',
    directives: [ ... ],
    templateUrl: 'name.html'
})

和班级

export class ProductGridComponent implements OnInit {
    @HostListener('scroll', ['$event'])
    onScroll(e) {
        alert(window.pageYOffset)
    }

    products = [];
}

但是它什么也没拍,但是当我将click和onClick替换为scroll和onScroll时,它确实显示了警报.

But it does not shot anything , however when i replace scroll and onScroll with click and onClick it indeed show the alert.

为什么它不能与scroll一起使用,angular2是否还有其他实现?

Why does not it work with scroll , does angular2 has any other implementation for it?

谢谢

推荐答案

假定您要显示主机滚动条(而不是Windows滚动条),并且您使用的是+2.1角度

Assuming you want to display the host scroll (and not the windows one) and that you are using angular +2.1

  @HostListener('scroll', ['$event']) private onScroll($event:Event):void {
    console.log($event.srcElement.scrollLeft, $event.srcElement.scrollTop);
  };

这篇关于Hostlistener上的滚动事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆