0

我正在尝试根据此处的方向使用名称间距来构建 jQuery 插件:http: //docs.jquery.com/Plugins/Authoring#Namespacing

现在我遇到了一个问题,我的插件需要使用 setTimeout 来触发它的一种方法,

var jScrollerMethods = {
    ready:function(){
        return this.each(function(){
        var self = this,
    $this = $(this),
        data = $this.data('jScroller');

        settings.timeoutHandle = setTimeout(function(){
            if(settings.direction = "left"){
                this.moveLeft();
            }else if(settings.direction = "right"){
                this.moveRight();
            }
        }, settings.time);

        $this.data('jScroller', {
            settings: settings,
            element: this
        });
    });
}
$.fn.jScroller = function(call){
    if ( jScrollerMethods[call] ) {
      return jScrollerMethods[call].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof call === 'object' || ! call ) {
        if (call) { $.extend(settings, call); }
        return jScrollerMethods.init.apply( this, call );
    } else {
        $.error( 'Method ' +  call + ' does not exist on jQuery.jScroller' );
    } 
}

但是我虽然会发生 setTimeout 是从窗口而不是插件对象触发的,并且我希望插件在每页上不止一次可用,所以我不能只将当前对象保存到窗口中,我该如何实现这一点?

4

1 回答 1

0

我发现在this外部使用return this.each时会给你准确的选择器结果

所以通过一些工作,我设法做到了,

var jScrollerMethods = {
    ready:function(){
        selector = this;

        return this.each(function(){
        var self = this,
    $this = $(this),
        data = $this.data('jScroller');

        settings.timeoutHandle = setTimeout(function(){
            if(settings.direction = "left"){
                selector.jScroller("moveLeft");
            }else if(settings.direction = "right"){
                selector.jScroller("moveRight");
            }
        }, settings.time);

        $this.data('jScroller', {
            settings: settings,
            element: this
        });
    });
}
$.fn.jScroller = function(call){
    if ( jScrollerMethods[call] ) {
      return jScrollerMethods[call].apply( this, Array.prototype.slice.call( arguments, 1 ));
    } else if ( typeof call === 'object' || ! call ) {
        if (call) { $.extend(settings, call); }
        return jScrollerMethods.init.apply( this, call );
    } else {
        $.error( 'Method ' +  call + ' does not exist on jQuery.jScroller' );
    } 
}
于 2012-07-24T12:28:40.823 回答