Matteo Spinelli did a terrific job writing a Javascript library that allows scrolling the content of a DIV element on iPhone and Android web browsers. The library is well documented in his site http://cubiq.org/iscroll. I don’t think that it is a must-have but I find very comfortable to implement functionality in a consistent way. Since I am a big jQuery fan I decided to wrap Matteo’s library in a very simple jQuery plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | (function($){ $.fn.iscroll = function(options){ if(this.data('iScrollReady') == null){ var that = this; var options = $.extend({}, options); options.onScrollEnd = function(){ that.triggerHandler('onScrollEnd', [this]); }; arguments.callee.object = new iScroll(this.get(0), options); // NOTE: for some reason in a complex page the plugin does not register // the size of the element. This will fix that in the meantime. setTimeout(function(scroller){ scroller.refresh(); }, 1000, arguments.callee.object); this.data('iScrollReady', true); }else{ arguments.callee.object.refresh(); } return arguments.callee.object; }; })(jQuery); |
(function($){ $.fn.iscroll = function(options){ if(this.data('iScrollReady') == null){ var that = this; var options = $.extend({}, options); options.onScrollEnd = function(){ that.triggerHandler('onScrollEnd', [this]); }; arguments.callee.object = new iScroll(this.get(0), options); // NOTE: for some reason in a complex page the plugin does not register // the size of the element. This will fix that in the meantime. setTimeout(function(scroller){ scroller.refresh(); }, 1000, arguments.callee.object); this.data('iScrollReady', true); }else{ arguments.callee.object.refresh(); } return arguments.callee.object; }; })(jQuery);
Basically you can use jQuery to select the content that you want to scroll and call ‘.iscroll()’ to add the behavior. If you need to overwrite the default parameters you pass an object with values that you wish to change. Here is an example of how to implement this:
1 2 3 4 5 6 7 | $(function(){ var elem = $('#content'); elem.iscroll(); elem.bind('onScrollEnd', function(e, iscroll){ alert($(this).attr('id') +' - '+ iscroll); }); }); |
$(function(){ var elem = $('#content'); elem.iscroll(); elem.bind('onScrollEnd', function(e, iscroll){ alert($(this).attr('id') +' - '+ iscroll); }); });
When you call .iscroll()
the library will find the parent element and make the content scrollable. This method will also return an instance of the iScroll class. If the method is called again it will refresh the content. This is convenient in case you modify the content dynamically (iScroll allows to detech DOM changes automatically).
The library dispatches an event name onScrollEnd
when the scroll action is completed. In touch screen devices this library adds momentum. This means that the content will continue moving for a short time after the touchend event. By default the version 3.7 accepts a callback function that is trigger at the end of the animation. The wrapper overwrites the parameter and triggers a jQuery event instead. This approach will allow multiple callback functions.
Not registered? Create an Account.
Lost your password? Click here to recover.
Create, customize, and publish mobile web applications using the Appcropolis Mobile Builder.
Get StartedWe offer 1,000's of mobile templates that are fully designed, coded, and ready to use. Learn more.
Download Free Templates
[...] Use jQuery iScroll to scroll the content inside a DIV. Register touch event listener as weel as a new onScrollEnd event Ajax Read the original post on DZone... [...]
[...] here to see the original: jQuery wrapper for iScroll | Appcropolis Bookmark on Delicious Digg this post Recommend on Facebook share via Reddit Share with Stumblers [...]
Nice ! I'd just add a simple :
if( $(this).size() ) { ... your code ... }
in order to avoid errors if I'd do something like :$('.iscroll').iscroll();
when there're no element selected. Thanks anyway.Hello, Very nice. I got it working, but I was actually interested in applying not just the scroll, but also the pinch/zoom on a single element or several different elements (pngs) on different pages. Is there any way to do this? I'm using jQuery Mobile/phonegap. Thank you for any help.
This article only cover the integration with iScroll, but pinch/zoom sound like a good material for a different post.
Hi sanraul, What i have is a main html and in a div i load an external html with the iscroll content... I'm able to get it working on any browser for desktop with your code but when i try it on my Ipod with ios 4.2.1 it is not working. My main html: $("#btn_clients").click(function(){ $('#clients').load('iscroll/iscroll.html', function() { $('#btn_clients').hide(); }); }); _____________________________________________ my external file : $(function(){ var elem = $('#container_logos'); elem.iscroll(); elem.bind('onScrollEnd', function(e, iscroll){ //alert($(this).attr('id') +' - '+ iscroll); }); }); So as i said this works well on desktop browser but not on mobile. If you have some time i'd really like to have your opinion and suggestions on that thanks.
I just found out that i had to refresh the scroll from my main html. This code did the trick: setTimeout(function () { myScroll = new iScroll('musicwrapper', { hScrollbar: false, vScrollbar: false, checkDOMChange:false }); myScroll.refresh(); }, 100); Thank you so much sanraul