Created
December 14, 2019 13:44
-
-
Save Kcko/ea8559551e7aa171785aee9fe827df35 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// plugin example | |
(function($){ | |
// custom select class | |
function CustomSelect(item, options) { | |
this.options = $.extend({ | |
foo: 'bar' | |
}, options); | |
this.item = $(item); | |
this.init(); | |
} | |
CustomSelect.prototype = { | |
init: function() { | |
this.item.css({opacity:0.5}); | |
}, | |
resetOpacity: function() { | |
this.setOpacity(''); | |
}, | |
setOpacity: function(opacity) { | |
this.item.css({opacity:opacity}); | |
} | |
} | |
// jQuery plugin interface | |
$.fn.customSelect = function(opt) { | |
var args = Array.prototype.slice.call(arguments, 1); | |
return this.each(function() { | |
var item = $(this), instance = item.data('CustomSelect'); | |
if(!instance) { | |
// create plugin instance if not created | |
item.data('CustomSelect', new CustomSelect(this, opt)); | |
} else { | |
// otherwise check arguments for method call | |
if(typeof opt === 'string') { | |
instance[opt].apply(instance, args); | |
} | |
} | |
}); | |
} | |
}(jQuery)); | |
// plugin testing | |
$('select').customSelect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment