Created
April 15, 2013 14:29
-
-
Save Schniz/5388513 to your computer and use it in GitHub Desktop.
jQuery iScroll wrapper. depends on jQuery ($) and iScroll
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
/** | |
* 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(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you have a code example how to wrap it to multiple instances of iScroll on the page?