Skip to content

Instantly share code, notes, and snippets.

@Schniz
Created April 15, 2013 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Schniz/5388513 to your computer and use it in GitHub Desktop.
Save Schniz/5388513 to your computer and use it in GitHub Desktop.
jQuery iScroll wrapper. depends on jQuery ($) and iScroll
/**
* Creates an iScroll instance and returns it.
* use string as the options param for calling a function.
* you can use the args for sending it to the function!
*
* @param {JSON} options for the new iScroll item
* if its a string, it will call the specific function
* @param arg arguments for sending the args to the selected function.
*
* @return IScroll for the current item.
*/
$.fn.iScroll = function(options /** , arguments.. **/) {
var $this = $(this);
// Check if thats a function
if (typeof(options) === 'string') {
var iScrollObject = $this.data('iScroll-Object');
// Check that the iScroll exists
if (!iScrollObject) {
throw new Error('this DOM object is not an iScroll!');
} else if (typeof(iScrollObject[options]) !== 'function') { // Check for the function
throw new Error('Cannot use ' + options + ': its not a function!');
} else {
// Get the argument data
var data = Array.prototype.slice.call(arguments, 1);
iScrollObject[options].apply(iScrollObject, data);
}
} else {
var defaults = {
hideScrollbar: true,
fadeScrollbar: true
};
options = $.extend(defaults, options);
var scrollbarInstance = new iScroll($this[0], options);
$this.data('iScroll-Object', scrollbarInstance);
// bind refresh to mouseenter!
$this.bind('mouseenter', function(e) {
$(this).data('iScroll-Object').refresh();
});
}
}
@PlippiePlop
Copy link

Do you have a code example how to wrap it to multiple instances of iScroll on the page?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment