我有一个无限滚动类:
(function(){
"use strict";
var InfiniteScroll = function() {
this.init();
};
var p = InfiniteScroll.prototype = mc.BaseClass.extend(gd.BaseClass);
p.BaseClass_init = p.init;
/*
* Public properties
*/
p.loading = false;
/*
* Public methods
*/
p.init = function() {
// Super
this.BaseClass_init();
// Init
this.ready();
};
p.ready = function() {
this._initInfiniteScroll();
};
p._initInfiniteScroll = function() {
$(window).scroll(function(){
if($(window).scrollTop() == $(document).height() - $(window).height()){
if(!p.loading)
{
p.loading = true;
//send a message up to say loading
}
}
});
}
mc.InfiniteScroll = InfiniteScroll;
}(window));
现在这是从另一个类调用的:
this.infiniteScroll = new mc.InfiniteScroll();
在另一个类中,我希望听一下滚动条何时从我有评论的地方触发: //向上发送消息表示正在加载
我只是想知道如何解决这个问题,我熟悉 AS3 中的事件监听器,但是有人可以为我指出正确的 js 方向吗?
最佳答案
您可以在您的类中实现自定义事件处理程序,并在其他类中添加监听器函数,如 this post 中所示。 .
您将在 _initInfiniteScroll 函数中使用它,例如:
//send a message up to say loading
this.fire({ type: "ContentLoadingEvent" });
不过,正如文章中所建议的,为 InfiniteScroll 事件创建一个单独的类会更好。
关于javascript - 事件监听器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21600449/