简体   繁体   English

对象文字滚动中的未定义函数

[英]Undefined function in object literal scroll

Hi Im trying to make an image gallery similar to instagram that loads images on scroll. 嗨,我正在尝试制作类似于instagram的图片库,以滚动方式加载图片。 Im trying it as an object literal variable but am getting an undefined method for this.showPics... getMore is called from an ajax success method. 我尝试将其作为对象文字变量,但为此获得了未定义的方法。showPics... getAjax成功方法调用了getMore。 Is this a scope issue because of the scroller being called in the .scroll? 这是范围问题吗,因为在.scroll中调用了滚动条? How should I do this? 我应该怎么做? BTW showPics is defined I just didnt paste it in.. Thanks 顺便说一句,showPics是定义的,我只是没有粘贴它。

var Pics{
    showPics: function() {
        //loop thru images
    }
    scroller: function() {
        if ($(window).scrollTop() + $(window).height() == $(document).height()) {
            this.showPics();
        }
    }
    getMore: function(){
        $(window).scroll(this.scroller);
    }
}

Correcting syntax produces: 更正语法会产生:

var Pics = {
    showPics: function() {
        //loop thru images
    },
    scroller: function() {
        if ($(window).scrollTop() + $(window).height() == $(document).height()) {
            this.showPics();
        }
    },
    getMore: function(){
        $(window).scroll(this.scroller);
    }
};

You were missing assignment after Pics and commas between member functions. 图片和成员函数之间的逗号分隔后,您缺少分配。

EDIT: Combined with https://stackoverflow.com/a/25184634/481422 answer from @Karl-André Gagnon it produces something alike http://jsfiddle.net/zlatin_zlatev/bue3ytek/ 编辑:与来自@Karl-AndréGagnon的https://stackoverflow.com/a/25184634/481422答案结合使用,它产生的效果类似于http://jsfiddle.net/zlatin_zlatev/bue3ytek/

If this is alike what you need, please accept Karl-André Gagnon's comment as an answer. 如果这是您所需要的,请接受Karl-AndréGagnon的评论作为答案。

In the function, this doesnt refer to the object anymore, but to the window. 在函数中, this不再引用对象,而是引用窗口。 Try using bind like that : 尝试像这样使用bind

$(window).scroll(this.scroller.bind(this));

It will change the reference of this inside the scroller function to the current object. 它会的参考改变this里面scroller功能当前对象。

Also, you have syntax errors in your object, but I assume that you just made a pseudo code for the question, right? 另外,您的对象中有语法错误,但是我想您只是为问题创建了伪代码,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM