Last active
August 29, 2015 14:07
-
-
Save ethyde/135e5165a9ea24ba3398 to your computer and use it in GitHub Desktop.
Simple jQuery base plugin
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
$(document).ready(function() { | |
// put all your jQuery goodness in here. | |
$('#sandbox').pluginName({ | |
fontSize : '2em', | |
onComplete : function() { | |
$( this ).fadeOut( 5000 ); | |
}, | |
onSetup : function() { | |
$( this ).css( 'color', 'blue' ); | |
} | |
}); | |
}); |
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
;(function( window, document, $, undefined ) { | |
'use strict'; | |
$.fn.pluginName = function( options ) { | |
options = $.extend( $.fn.pluginName.defaults, options ); | |
// Plugin code | |
return this.each( function() { | |
var element = $( this ); | |
element.animate({ | |
marginLeft : options.fromLeft, | |
fontSize : options.fontSize | |
}, { | |
complete : options.onComplete // call "onComplete" animate callback | |
} | |
); | |
// call "onSetup" callback and apply the scope: | |
if ( $.isFunction( options.onSetup ) ) { | |
options.onSetup.call( element ); | |
} | |
}); | |
}; | |
// Set up the default options. | |
$.fn.pluginName.defaults = { | |
fromLeft : '+=50', | |
fontSize : '20px', | |
onComplete : null, | |
onSetup : null | |
}; | |
})( window, document, jQuery ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment