Variables in jquery plugin with multiple methods
Hello,
I have created my first jquery plugin and I am not sure if I am using the variables the"correct" way. The plugin is working fine as I did it, I just want to know if there is a more elegant way of doing it. My question is: am I declaring and setting the variables in a "correct" way?
The plugin:
It takes an element and turns it into a basic scroll roller.
Source code:
- (function( $ ){
var settings = {
speed :10000,
totalWidth :500,
loop :true,
construct :'span'
};
var root;
var methods = {
init : function( options )
{
return this.each(function() {
if ( options ) {
$.extend( settings, options );
}
var $this = $(this);
$this.children().css('left', $this.width());
methods.animate($this.children(settings.construct+":first-child"));
});
},
show : function( ) { },
hide : function( ) { },
update : function( content ) { },
animate : function(element)
{
var animBody;
if($(element).length==0 && settings.loop)
{
animBody=settings.root.children(settings.construct+":first-child")
}
else
{
animBody=$(element);
}
var distance = animBody.width()+settings.totalWidth+5;
animBody.animate({
left: '-='+distance
}, settings.speed , 'linear', function() {
animBody.css('left', settings.totalWidth+5);
methods.animate(animBody.next());
});
}
};
$.fn.textoller = function(method)
{
settings.totalWidth = this.width();
settings.root = this;
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.textoller' );
}
}
})( jQuery );
Thanks in advanced!