JQuery Plugin 2 - Passing Options into Your Plugin

weixin_34104341發表於2020-04-07

overriding the default options with user-supplied options and the jQuery extend() method

eg:

$.fn.pulse = function (options) {
    // Merge passed options with defaults
    var opts = $.extend({}, $.fn.pulse.defaults, options);
    return this.each(function () {
        // Pulse
        for (var i = 0; i < opts.pulses; i++) {
            $(this).fadeTo(opts.speed, opts.fadeLow).fadeTo(opts.speed, opts.fadeHigh);
        }
        // Reset to normal
        $(this).fadeTo(opts.speed, 1);
    });
};
// Pulse plugin default options
$.fn.pulse.defaults = {
    speed: "slow",
    pulses: 2,
    fadeLow: 0.2,
    fadeHigh: 1
};


call the plugin

// Override only one option
$('#myText1').pulse({ pulses: 6 });

// Override all options
$('#myText2').pulse({ speed: "fast", pulses: 10, fadeLow: 0.3, fadeHigh: 0.8 });

 

 

 

轉載於:https://www.cnblogs.com/davidgu/p/3331550.html

相關文章