Tim Marshall Tim Marshall - 1 month ago 19
jQuery Question

Passing fields to script

In my script, how can I read 'things' such as

autoHide: true
to set variables and whatnot on my script. I call them 'things' as I am not sure what they are called exactly thus meaning there are probably thousands of resources out there yet due to my lack of knowledge, i cannot find how to achieve this.

I wish to have multiple options such as
scrollbarWidth
,
theme
and whatnot yet not knowing how to read them on my file, I'm at a bit of a loss!

I've currently got a live demonstration here of my full working script in action.

(function(jQuery, undefined) {
"use strict";
jQuery.fn.extend({
BananzaScroll: function() {

// Script Functions Here

}
});
})(jQuery);


$( "#demo" ).BananzaScroll({
autoHide: true
});

Answer

Pass an argument to the definition of BananzaScroll, like so.

(function(jQuery, undefined) {
  "use strict";

  jQuery.fn.extend({
    BananzaScroll: function(opts) {
      console.log(opts); // opts contains the object you pass in
    }
  });
})(jQuery);

$('#demo').BananzaScroll({
    autoHide: true
});

JSBin demo