The example in this article describes how jQuery implements switching font size. Share it with everyone for your reference. The specific implementation method is as follows:
$.fn.switchSize = function(settings) {
// defaults settings
settings = $.extend({
container: 'body',
arrSizeClass: ['small', 'medium', 'large'],
defaultClass: 'medium',
SaveCookie: true
}, settings);
var $container = $(settings.container);
return this
.each(function() {
If ($.cookie('switchSize')) {
$container.addClass($.cookie('switchSize'));
$(this).data("current", $.cookie('switchSize'))
}
})
.bind("click", function() {
var pos;
If ($(this).data("current")) {
pos = jQuery.inArray($(this).data("current"), settings.arrSizeClass);
} else {
pos = jQuery.inArray(settings.defaultClass, settings.arrSizeClass);
}
If (pos >= 0) { //Found Class
If (pos == settings.arrSizeClass.length - 1) { //Check if last
$(this).data("current", settings.arrSizeClass[0]);
} else {
$(this).data("current", settings.arrSizeClass[pos 1]);
}
} else {
//To prevent error
$(this).data("current", settings.arrSizeClass[0]);
}
$container.removeClass(settings.arrSizeClass[pos]).addClass($(this).data("current"));
If (settings.saveCookie === true) {
$.cookie('switchSize', $(this).data("current"), { expires: 365, path: '/' });
}
});
};
I hope this article will be helpful to everyone’s jQuery programming.
Statement:The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn