0

我可以将普通的 js 函数传递到 jquery 插件中,然后在插件中调用该函数吗?

(function($){

    $.fn.extend({ 

    responsive: function(options,callbacks) {

    var defaults = {
                setup:                      {},
                windowTargetWidth:          1903,
                beforeRespond:              function() {},
                onRespond:                  function() {},
                onElse:                     function() {}
            };

            var options =  $.extend(defaults, options);
            var o = options;

            var $this = this;

            return this.ready(function(e){

                var window_loaded_width = $(window).width();

                $(window).resize(function() {

                    options.beforeRespond(ratio);

                    // Make the website responsive
                    if(window_loaded_width < o.windowTargetWidth )
                    {
                        // call the function
                        setup.func;

                        //options.onRespond(ratio);
                    }
                    else
                    {
                        var window_resized_width = $(window).width();
                        var ratio =  window_resized_width/window_loaded_width; 

                        options.onElse(ratio);
                    }

                });


            });

        }
    });

})(jQuery);


$(window).responsive({
    setup: {
        func: responsive_element(ratio)
    },

    beforeRespond: function(ratio){

        // something happens...

    },

    onRespond: function(ratio){

        responsive_element(ratio);
    },
    onElse: function(ratio){

        responsive_element(ratio);
    }
});

function responsive_element(ratio){
    $("#container").css({
        "width": 5400 * ratio
    });
}

当然,我得到一个错误 -

比率未定义

对于这条线,

func: responsive_element(ratio)

也许我做错了?

我想将函数传入并在插件中调用它的原因是,responsive_element(ratio)当我将此插件附加到对象时重复两次或更多次$(window).responsive({...})

理想情况下,我只需要在某个地方或某个地方输入一次函数-setup可能吗?

4

1 回答 1

0

在您的情况下调用插件中的函数使用 o.beforeRespond.call(this)

于 2013-07-03T12:53:24.707 回答