(function ($) { $.CustomData = { elements:$() }; $.fn.extend({ Custom:function (options) { var elements = this; $.CustomData.elements = $.CustomData.elements.add(elements); /* */ var defaults = { customStyleClass:"checkbox", customHeight:"16" }; /* */ options = $.extend(defaults, options); /* */ var pushed = function () { var element = $(this).children('input'); if (element.is(':checked')) { /* */ $(this).css('backgroundPosition', "0px -" + options.customHeight * 3 + "px"); } else { $(this).css('backgroundPosition', "0px -" + options.customHeight + "px"); } }; /* , (radio)*/ var check = function () { var element = $(this).children('input'); if (element.is(':checked') && element.attr('type') === 'checkbox') {/* */ $(this).css('backgroundPosition', '0px 0px'); $(this).children('input').attr('checked', false).change(); /* */ } else { if (element.attr('type') === 'checkbox') {/* */ $(this).css('backgroundPosition', "0px -" + options.customHeight * 2 + "px"); } else { /**/ $(this).css('backgroundPosition', "0px -" + options.customHeight * 2 + "px"); $('input[name=' + element.attr('name') + ']').not(element).parent().css('backgroundPosition', '0px 0px'); } $(this).children('input').attr('checked', 'checked').change(); } }; /* */ var update = function () { $.CustomData.elements.each(function () { /* */ if ($(this).is(':checked')) { $(this).parent().css('backgroundPosition', "0px -" + $(this).attr('data-height') * 2 + "px"); } else { $(this).parent().css('backgroundPosition', "0px 0px"); } }); }; /* disabled/enabled */ var refresh = function () { if (!$(this).prop('disabled')) { $(this).parent().mousedown(pushed); $(this).parent().mouseup(check); $(this).parent().removeClass('disabled'); } else { $(this).parent().addClass('disabled'); $(this).parent().unbind('mousedown', pushed); $(this).parent().unbind('mouseup', check); } }; return this.each(function () { if ($(this).attr('data-init') != '1') { $(this).attr('data-init', '1'); $(this).attr('data-height', options.customHeight); /* <span></span>*/ $(this).wrap('<span/>'); /* */ var span = $(this).parent().addClass(options.customStyleClass); if ($(this).is(':checked') === true) { /* */ span.css('backgroundPosition', "0px -" + (options.customHeight * 2) + "px"); } /* */ $(this).bind('change', update); $(this).bind('custom.refresh', refresh); if (!$(this).prop('disabled')) { /* span*/ span.mousedown(pushed); span.mouseup(check); } else { /* */ span.addClass('disabled'); } } }); } }); })(jQuery);
(function ($) { $.fn.extend({ Custom:function (options) { /* */ var defaults = { customStyleClass:"checkbox", customHeight:"16" }; /* */ options = $.extend(defaults, options); }; return this.each(function () { }); } });
return this.each(function () { if ($(this).attr('data-init') != '1') { $(this).attr('data-init', '1'); $(this).attr('data-height', options.customHeight); /* <span></span>*/ $(this).wrap('<span/>'); /* */ var span = $(this).parent().addClass(options.customStyleClass); if ($(this).is(':checked') === true) { /* */ span.css('backgroundPosition', "0px -" + (options.customHeight * 2) + "px"); } /* */ $(this).bind('change', update); $(this).bind('custom.refresh', refresh); if (!$(this).prop('disabled')) { /* span*/ span.mousedown(pushed); span.mouseup(check); } else { /* */ span.addClass('disabled'); } } });
$(this).attr('data-init', '1');
$(this).attr('data-height', options.customHeight);
$(this).bind('custom.refresh', refresh);
$('#radio3').removeAttr('disabled').trigger('custom.refresh');
Custom:function (options) {...};
var pushed = function () { var element = $(this).children('input'); if (element.is(':checked')) { /* */ $(this).css('backgroundPosition', "0px -" + options.customHeight * 3 + "px"); } else { $(this).css('backgroundPosition', "0px -" + options.customHeight + "px"); } };
/* , (radio)*/ var check = function () { var element = $(this).children('input'); if (element.is(':checked') && element.attr('type') === 'checkbox') {/* */ $(this).css('backgroundPosition', '0px 0px'); $(this).children('input').attr('checked', false).change(); /* */ } else { if (element.attr('type') === 'checkbox') {/* */ $(this).css('backgroundPosition', "0px -" + options.customHeight * 2 + "px"); } else { /**/ $(this).css('backgroundPosition', "0px -" + options.customHeight * 2 + "px"); $('input[name=' + element.attr('name') + ']').not(element).parent().css('backgroundPosition', '0px 0px'); } $(this).children('input').attr('checked', 'checked').change(); } };
$(this).children('input').attr('checked', false).change();
/* */ var update = function () { $.CustomData.elements.each(function () { /* */ if ($(this).is(':checked')) { $(this).parent().css('backgroundPosition', "0px -" + $(this).attr('data-height') * 2 + "px"); } else { $(this).parent().css('backgroundPosition', "0px 0px"); } }); };
$.CustomData = { elements:$() };
var elements = this; $.CustomData.elements = $.CustomData.elements.add(elements);
/* disabled/enabled */ var refresh = function () { if (!$(this).prop('disabled')) { $(this).parent().mousedown(pushed); $(this).parent().mouseup(check); $(this).parent().removeClass('disabled'); } else { $(this).parent().addClass('disabled'); $(this).parent().unbind('mousedown', pushed); $(this).parent().unbind('mouseup', check); } };
.radio { display:block; height: 25px; width: 19px; overflow:hidden; background: url("radio.png") no-repeat 0 0 transparent; position:relative; } .radio.disabled{ opacity:0.5; filter:Alpha(opacity="50"); } .radio input{ position:absolute; right:-400px; top:0px; }
$("input[type='radio']").Custom({ customStyleClass:'radio', customHeight:'25' });
Source: https://habr.com/ru/post/142182/
All Articles