将普通函数传递给jquery插件然后在插件中调用该函数?

时间:2013-07-03 12:47:52

标签: jquery plugins jquery-callback jquery-1.9

我可以将普通的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或某个地方输入一次功能 - 是否可能?

1 个答案:

答案 0 :(得分:0)

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