Debian Codegraph @

README.md

raw symbols SCIP
1 <h1 id="intro">iScroll, smooth scrolling for the web</h1>
2
3 iScroll is a high performance, small footprint, dependency free, multi-platform javascript scroller.
4
5 It works on desktop, mobile and smart TV. It has been vigorously optimized for performance and size so to offer the smoothest result on modern and old devices alike.
6
7 iScroll does not just *scroll*. It can handle any element that needs to be moved with user interaction. It adds scrolling, zooming, panning, infinite scrolling, parallax scrolling, carousels to your projects and manages to do that in just 4kb. Give it a broom and it will also clean up your office.
8
9 Even on platforms where native scrolling is good enough, iScroll adds features that wouldn't be possible otherwise. Specifically:
10
11 * Granular control over the scroll position, even during momentum. You can always get and set the x,y coordinates of the scroller.
12 * Animation can be customized with user defined easing functions (bounce, elastic, back, ...).
13 * You can easily hook to a plethora of custom events (onBeforeScrollStart, onScrollStart, onScroll, onScrollEnd, flick, ...).
14 * Out of the box multi-platform support. From older Android devices to the latest iPhone, from Chrome to Internet Explorer.
15
16 <h2 id="iscroll-versions">The many faces of iScroll</h2>
17
18 iScroll is all about optimization. To reach the highest performance it has been divided into multiple versions. You can pick the version that better suits your need.
19
20 Currently we have the following fragrances:
21
22 * **iscroll.js**, it is the general purpose script. It includes the most commonly used features and grants very high performance in a small footprint.
23 * **iscroll-lite.js**, it is a stripped down version of the main script. It doesn't support snap, scrollbars, mouse wheel, key bindings. But if all you need is scrolling (especially on mobile) *iScroll lite* is the smallest, fastest solution.
24 * **iscroll-probe.js**, probing the current scroll position is a demanding task, that's why I decided to build a dedicated version for it. If you need to know the scrolling position at any given time, this is the iScroll for you. (I'm making some more tests, this might end up in the regular `iscroll.js` script, so keep an eye on it).
25 * **iscroll-zoom.js**, adds zooming to the standard scroll.
26 * **iscroll-infinite.js**, can do infinite and cached scrolling. Handling very long lists of elements is no easy task for mobile devices. *iScroll infinite* uses a caching mechanism that lets you scroll a potentially infinite number of elements.
27
28 <h2 id="getting-started">Getting started</h2>
29
30 So you want to be an iScroll master. Cool, because that is what I'll make you into.
31
32 The best way to learn the iScroll is by looking at the demos. In the archive you'll find a `demo` folder [stuffed with examples](https://github.com/cubiq/iscroll/tree/master/demos). Most of the script features are outlined there.
33
34 `IScroll` is a class that needs to be initiated for each scrolling area. There's no limit to the number of iScrolls you can have in each page if not that imposed by the device CPU/Memory.
35
36 Try to keep the DOM as simple as possible. iScroll uses the hardware compositing layer but there's a limit to the elements the hardware can handle.
37
38 The optimal HTML structure is:
39
40 ```html
41 <div id="wrapper">
42 <ul>
43 <li>...</li>
44 <li>...</li>
45 ...
46 </ul>
47 </div>
48 ```
49
50 iScroll must be applied to the wrapper of the scrolling area. In the above example the `UL` element will be scrolled. Only the first child of the container element is scrolled, additional children are simply ignored.
51
52 <div class="tip">
53 <p><code>box-shadow</code>, <code>opacity</code>, <code>text-shadow</code> and alpha channels are all properties that don't go very well together with hardware acceleration. Scrolling might look good with few elements but as soon as your DOM becomes more complex you'll start experiencing lag and jerkiness.</p>
54
55 <p>Sometimes a background image to simulate the shadow performs better than <code>box-shadow</code>. The bottom line is: experiment with CSS properties, you'll be surprised by the difference in performance a small CSS change can do.</p>
56 </div>
57
58 The minimal call to initiate the script is as follow:
59
60 ```html
61 <script type="text/javascript">
62 var myScroll = new IScroll('#wrapper');
63 </script>
64 ```
65
66 The first parameter can be a string representing the DOM selector of the scroll container element OR a reference to the element itself. The following is a valid syntax too:
67
68 ```js
69 var wrapper = document.getElementById('wrapper');
70 var myScroll = new IScroll(wrapper);
71 ```
72
73 So basically either you pass the element directly or a string that will be given to `querySelector`. Consequently to select a wrapper by its class name instead of the ID, you'd do:
74
75 ```js
76 var myScroll = new IScroll('.wrapper');
77 ```
78
79 Note that iScroll uses `querySelector` not `querySelectorAll`, so only the first occurrence of the selector is used. If you need to apply iScroll to multiple objects you'll have to build your own cycle.
80
81 <div class="tip">
82 <p>You don't strictly need to assign the instance to a variable (<code>myScroll</code>), but it is handy to keep a reference to the iScroll.</p>
83
84 <p>For example you could later check the <a href="#scroller-info">scroller position</a> or <a href="#destroy">unload unnecessary events</a> when you don't need the iScroll anymore.</p>
85 </div>
86
87 <h2 id="initialization">Initialization</h2>
88
89 The iScroll needs to be initiated when the DOM is ready. The safest bet is to start it on window `onload` event. `DOMContentLoaded` or inline initialization are also fine but remember that the script needs to know the height/width of the scrolling area. If you have images that don't have explicit width/height declaration, iScroll will most likely end up with a wrong scroller size.
90
91 <div class="important">
92 <p>Add <code>position:relative</code> or <code>absolute</code> to the scroll container (the wrapper). That alone usually solves most of the problems with wrongly calculated wrapper dimensions.</p>
93 </div>
94
95 To sum up, the smallest iScroll configuration is:
96
97 ```html
98 <head>
99 ...
100 <script type="text/javascript" src="iscroll.js"></script>
101 <script type="text/javascript">
102 var myScroll;
103 function loaded() {
104 myScroll = new IScroll('#wrapper');
105 }
106 </script>
107 </head>
108 ...
109 <body onload="loaded()">
110 <div id="wrapper">
111 <ul>
112 <li>...</li>
113 <li>...</li>
114 ...
115 </ul>
116 </div>
117 </body>
118 ```
119
120 Refer to the [barebone example](http://lab.cubiq.org/iscroll5/demos/barebone/) for more details on the minimal CSS/HTML requirements.
121
122 <div class="tip">
123 <p>If you have a complex DOM it is sometimes smart to add a little delay from the <code>onload</code> event to iScroll initialization. Executing the iScroll with a 100 or 200 milliseconds delay gives the browser that little rest that can save your ass.</p>
124 </div>
125
126 <h2 id="configuring">Configuring the iScroll</h2>
127
128 iScroll can be configured by passing a second parameter during the initialization phase.
129
130 ```js
131 var myScroll = new IScroll('#wrapper', {
132 mouseWheel: true,
133 scrollbars: true
134 });
135 ```
136
137 The example above turns on mouse wheel support and scrollbars.
138
139 After initialization you can access the *normalized* values from the `options` object. Eg:
140
141 ```js
142 console.dir(myScroll.options);
143 ```
144
145 The above will return the configuration the `myScroll` instance will run on. By *normalized* I mean that if you set `useTransform:true` (for example) but the browser doesn't support CSS transforms, `useTransform` will be `false`.
146
147 <h2 id="the-core">Understanding the core</h2>
148
149 iScroll uses various techniques to scroll based on device/browser capability. **Normally you don't need to configure the engine**, iScroll is smart enough to pick the best for you.
150
151 Nonetheless it is important to understand which mechanisms iScroll works on and how to configure them.
152
153 ### <small>options.</small>useTransform
154
155 By default the engine uses the `transform` CSS property. Setting this to `false` scrolls like we were in 2007, ie: using the `top`/`left` (and thus the scroller needs to be absolutely positioned).
156
157 This might be useful when scrolling sensitive content such as Flash, iframes and videos, but be warned: performance loss is huge.
158
159 Default: `true`
160
161 ### <small>options.</small>useTransition
162
163 iScroll uses CSS transition to perform animations (momentum and bounce). By setting this to `false`, `requestAnimationFrame` is used instead.
164
165 On modern browsers the difference is barely noticeable. On older devices transitions perform better.
166
167 Default: `true`
168
169 ### <small>options.</small>HWCompositing
170
171 This option tries to put the scroller on the hardware layer by appending `translateZ(0)` to the transform CSS property. This greatly increases performance especially on mobile, but there are situations where you might want to disable it (notably if you have too many elements and the hardware can't catch up).
172
173 Default: `true`
174
175 <div class="important">
176 <p>If unsure leave iScroll decide what's the optimal config. For best performance all the above options should be set to <code>true</code> (or better leave them undefined as they are set to true automatically). You may try to play with them in case you encounter hiccups and memory leaks.</p>
177 </div>
178
179 <h2 id="basic-features">Basic features</h2>
180
181 ### <small>options.</small>bounce
182
183 When the scroller meets the boundary it performs a small bounce animation. Disabling bounce may help reach smoother results on old or slow devices.
184
185 Default: `true`
186
187 ### <small>options.</small>click
188
189 To override the native scrolling iScroll has to inhibit some default browser behaviors, such as mouse clicks. If you want your application to respond to the *click* event you have to explicitly set this option to `true`. Please note that it is suggested to use the custom `tap` event instead (see below).
190
191 Default: `false`
192
193 ### <small>options.</small>disableMouse<br/><small>options.</small>disablePointer<br/><small>options.</small>disableTouch
194
195 By default iScroll listens to all pointer events and reacts to the first one that occurs. It may seem a waste of resources but feature detection has proven quite unreliable and this *listen-to-all* approach is our safest bet for wide browser/device compatibility.
196
197 If you have an internal mechanism for device detection or you know in advance where your script will run on, you may want to disable all event sets you don't need (mouse, pointer or touch events).
198
199 For example to disable mouse and pointer events:
200
201 ```js
202 var myScroll = new IScroll('#wrapper', {
203 disableMouse: true,
204 disablePointer: true
205 });
206 ```
207
208 Default: `false`
209
210 ### <small>options.</small>eventPassthrough
211
212 Sometimes you want to preserve native vertical scroll but being able to add an horizontal iScroll (maybe a carousel). Set this to `true` and the iScroll area will react to horizontal swipes only. Vertical swipes will naturally scroll the whole page.
213
214 See [event passthrough demo](http://lab.cubiq.org/iscroll5/demos/event-passthrough/) on a mobile device. Note that this can be set to `'horizontal'` to inverse the behavior (native horizontal scroll, vertical iScroll).
215
216 ### <small>options.</small>freeScroll
217
218 This is useful mainly on 2D scrollers (when you need to scroll both horizontally and vertically). Normally when you start scrolling in one direction the other is locked.
219
220 Sometimes you just want to move freely with no constrains. In these cases you can set this option to `true`. See [2D scroll demo](http://lab.cubiq.org/iscroll5/demos/2d-scroll/).
221
222 Default: `false`
223
224 ### <small>options.</small>keyBindings
225
226 Set this to `true` to activate keyboard (and remote controls) interaction. See the [Key bindings](#key-bindings) section below for more information.
227
228 Default: `false`
229
230 ### <small>options.</small>invertWheelDirection
231
232 Meaningful when mouse wheel support is activated, in which case it just inverts the scrolling direction. (ie. going down scrolls up and vice-versa).
233
234 Default: `false`
235
236 ### <small>options.</small>momentum
237
238 You can turn on/off the momentum animation performed when the user quickly flicks on screen. Turning this off greatly enhance performance.
239
240 Default: `true`
241
242 ### <small>options.</small>mouseWheel
243
244 Listen to the mouse wheel event.
245
246 Default: `false`
247
248 ### <small>options.</small>preventDefault
249
250 Whether or not to `preventDefault()` when events are fired. This should be left `true` unless you really know what you are doing.
251
252 See `preventDefaultException` in the [Advanced features](#advanced-features) for more control over the preventDefault behavior.
253
254 Default: `true`
255
256 ### <small>options.</small>scrollbars
257
258 Wheter or not to display the default scrollbars. See more in the [Scrollbar](#scrollbar) section.
259
260 Default: `false`.
261
262 ### <small>options.</small>scrollX<br/><small>options.</small>scrollY
263
264 By default only vertical scrolling is enabled. If you need to scroll horizontally you have to set `scrollX` to `true`. See [horizontal demo](http://lab.cubiq.org/iscroll5/demos/horizontal/).
265
266 See also the **freeScroll** option.
267
268 Default: `scrollX: false`, `scrollY: true`
269
270 <div class="important">
271 <p>Note that <code>scrollX/Y: true</code> has the same effect as <code>overflow: auto</code>. Setting one direction to <code>false</code> helps to spare some checks and thus CPU cycles.</p>
272 </div>
273
274 ### <small>options.</small>startX<br/><small>options.</small>startY
275
276 By default iScroll starts at `0, 0` (top left) position, you can instruct the scroller to kickoff at a different location.
277
278 Default: `0`
279
280 ### <small>options.</small>tap
281
282 Set this to `true` to let iScroll emit a custom `tap` event when the scroll area is clicked/tapped but not scrolled.
283
284 This is the suggested way to handle user interaction with clickable elements. To listen to the tap event you would add an event listener as you would do for a standard event. Example:
285
286 ```js
287 element.addEventListener('tap', doSomething, false); \\ Native
288 $('#element').on('tap', doSomething); \\ jQuery
289 ```
290
291 You can also customize the event name by passing a string. Eg:
292
293 ```js
294 tap: 'myCustomTapEvent'
295 ```
296
297 In this case you'd listen to `myCustomTapEvent`.
298
299 Default: `false`
300
301 <h2 id="scrollbars">Scrollbars</h2>
302
303 The scrollbars are more than just what the name suggests. In fact internally they are referenced as *indicators*.
304
305 An indicator listens to the scroller position and normally it just shows its position in relation to whole, but what it can do is so much more.
306
307 Let's start with the basis.
308
309 ### <small>options.</small>scrollbars
310
311 As we mentioned in the [Basic features section](#basic-features) there's only one thing that you got to do to activate the scrollbars in all their splendor, and that one thing is:
312
313 ```js
314 var myScroll = new IScroll('#wrapper', {
315 scrollbars: true
316 });
317 ```
318
319 Of course the default behavior can be personalized.
320
321 ### <small>options.</small>fadeScrollbars
322
323 When not in use the scrollbar fades away. Leave this to `false` to spare resources.
324
325 Default: `false`
326
327 ### <small>options.</small>interactiveScrollbars
328
329 The scrollbar becomes draggable and user can interact with it.
330
331 Default: `false`
332
333 ### <small>options.</small>resizeScrollbars
334
335 The scrollbar size changes based on the proportion between the wrapper and the scroller width/height. Setting this to `false` makes the scrollbar a fixed size. This might be useful in case of custom styled scrollbars ([see below](#styling-the-scrollbar)).
336
337 Default: `true`
338
339 ### <small>options.</small>shrinkScrollbars
340
341 When scrolling outside of the boundaries the scrollbar is shrunk by a small amount.
342
343 Valid values are: `'clip'` and `'scale'`.
344
345 `'clip'` just moves the indicator outside of its container, the impression is that the scrollbar shrinks but it is simply moving out of the screen. If you can live with the visual effect this option **immensely improves overall performance**.
346
347 `'scale'` turns off `useTransition` hence all animations are served with `requestAnimationFrame`. The indicator is actually varied in size and the end result is nicer to the eye.
348
349 Default: `false`
350
351 <div class="tip">
352 <p>Note that resizing can't be performed by the GPU, so <code>scale</code> is all on the CPU.</p>
353 <p>If your application runs on multiple devices my suggestion would be to switch this option to <code>'scale'</code>, <code>'clip'</code> or <code>false</code> based on the platform responsiveness (eg: on older mobile devices you could set this to <code>'clip'</code> and on desktop browser to <code>'scale'</code>).</p>
354 </div>
355
356 See the [scrollbar demo](http://lab.cubiq.org/iscroll5/demos/scrollbars/).
357
358 <h3 id="styling-the-scrollbar">Styling the scrollbar</h3>
359
360 So you don't like the default scrollbar styling and you think you could do better. Help yourself! iScroll makes dressing the scrollbar a snap. First of all set the `scrollbars` option to `'custom'`:
361
362 ```js
363 var myScroll = new IScroll('#wrapper', {
364 scrollbars: 'custom'
365 });
366 ```
367
368 Then use the following CSS classes to style the little bastards.
369
370 * **.iScrollHorizontalScrollbar**, this is applied to the horizontal container. The element that actually hosts the scrollbar indicator.
371 * **.iScrollVerticalScrollbar**, same as above but for the vertical container.
372 * **.iScrollIndicator**, the actual scrollbar indicator.
373 * **.iScrollBothScrollbars**, this is added to the container elements when both scrollbars are shown. Normally just one (horizontal or vertical) is visible.
374
375 The [styled scrollbars demo](http://lab.cubiq.org/iscroll5/demos/styled-scrollbars/) should make things clearer than my lousy explanation.
376
377 If you set `resizeScrollbars: false` you could make the scrollbar of a fixed size, otherwise it would be resized based on the scroller length.
378
379 Please keep reading to the following section for a revelation that will shake your world.
380
381 <h2 id="indicators">Indicators</h2>
382
383 All the scrollbar options above are in reality just wrappers to the low level `indicators` option. It looks more or less like this:
384
385 ```js
386 var myScroll = new IScroll('#wrapper', {
387 indicators: {
388 el: [element|element selector]
389 fade: false,
390 ignoreBoundaries: false,
391 interactive: false,
392 listenX: true,
393 listenY: true,
394 resize: true,
395 shrink: false,
396 speedRatioX: 0,
397 speedRatioY: 0,
398 }
399 });
400 ```
401
402 ### <small>options.indicators.</small>el
403
404 This is a mandatory parameter which holds a reference to the scrollbar container element. The first child inside the container will be the indicator. Note that the scrollbar can be anywhere on your document, it doesn't need to be inside the scroller wrapper. Do you start perceiving the power of such tool?
405
406 Valid syntax would be:
407
408 ```js
409 indicators: {
410 el: document.getElementById('indicator')
411 }
412 ```
413
414 Or simply:
415
416 ```js
417 indicators: {
418 el: '#indicator'
419 }
420 ```
421
422 ### <small>options.indicators.</small>ignoreBoundaries
423
424 This tells the indicator to ignore the boundaries imposed by its container. Since we can alter the speed ratio of the scrollbar, it is useful to just let the scrollbar go. Say you want the indicator to go twice as fast as the scroller, it would reach the end of its run very quickly. This option is used for [parallax scrolling](#parallax-scrolling).
425
426 Default: `false`
427
428 ### <small>options.indicators.</small>listenX<br/><small>options.indicators.</small>listenY
429
430 To which axis the indicator listens to. It can be just one or both.
431
432 Default: `true`
433
434 ### <small>options.indicators.</small>speedRatioX<br/><small>options.indicators.</small>speedRatioY
435
436 The speed the indicator moves in relation to the main scroller size. By default this is set automatically. You rarely need to alter this value.
437
438 Default: `0`
439
440 ### <small>options.indicators.</small>fade<br/><small>options.indicators.</small>interactive<br/><small>options.indicators.</small>resize</br><small>options.indicators.</small>shrink
441
442 These are the same options we explored in the [scrollbars section](#scrollbars), I'm not going to insult your intelligence and repeat them here.
443
444 <div class="important">
445 <p><strong>Do not cross the streams. It would be bad!</strong> Do not mix the scrollbars syntax (<code>options.scrollbars</code>, <code>options.fadeScrollbars</code>, <code>options.interactiveScrollbars</code>, ...) with the indicators! Use one or the other.</p>
446 </div>
447
448 Have a look at the [minimap demo](http://lab.cubiq.org/iscroll5/demos/minimap/) to get a glance at the power of the `indicators` option.
449
450 The wittiest of you would have noticed that `indicators` is actually plural... Yes, exactly, passing an array of objects you can have a virtually infinite number of indicators. I don't know what you may need them for, but hey! who am I to argue about your scrollbar preferences?
451
452 ## <span id="parallax-scrolling">Parallax scrolling</span>
453
454 Parallax scrolling is just a *collateral damage* of the [Indicators](#indicators) functionality.
455
456 An indicator is just a layer that follows the movement and animation applied to the main scroller. If you see it like that you'll understand the power behind this feature. To this add that you can have any number of indicators and the parallax scrolling is served.
457
458 Please refer to the [parallax demo](http://lab.cubiq.org/iscroll5/demos/parallax/).
459
460 ## Scrolling programmatically
461
462 You silly! Of course you can scroll programmaticaly!
463
464 ### scrollTo(x, y, time, easing)
465
466 Say your iScroll instance resides into the `myScroll` variable. You can easily scroll to any position with the following syntax:
467
468 ```js
469 myScroll.scrollTo(0, -100);
470 ```
471
472 That would scroll down by 100 pixels. Remember: 0 is always the top left corner. To scroll you have to pass negative numbers.
473
474 `time` and `easing` are optional. They regulates the duration (in ms) and the easing function of the animation respectively.
475
476 The easing functions are available in the `IScroll.utils.ease` object. For example to apply a 1 second elastic easing you'd do:
477
478 ```js
479 myScroll.scrollTo(0, -100, 1000, IScroll.utils.ease.elastic);
480 ```
481
482 The available options are: `quadratic`, `circular`, `back`, `bounce`, `elastic`.
483
484 ### scrollBy(x, y, time, easing)
485
486 Same as above but X and Y are relative to the current position.
487
488 ```js
489 myScroll.scrollBy(0, -10);
490 ```
491
492 Would scroll 10 pixels down. If you are at -100, you'll end up at -110.
493
494 ### scrollToElement(el, time, offsetX, offsetY, easing)
495
496 You're gonna like this. Sit tight.
497
498 The only mandatory parameter is `el`. Pass an element or a selector and iScroll will try to scroll to the top/left of that element.
499
500 `time` is optional and sets the animation duration.
501
502 `offsetX` and `offsetY` define an offset in pixels, so that you can scroll to that element plus a the specified offset. Not only that. If you set them to `true` the element will be centered on screen. Refer to the [scroll to element](http://lab.cubiq.org/iscroll5/demos/scroll-to-element/) example.
503
504 `easing` works the same way as per the **scrollTo** method.
505
506 <h2 id="snap">Snap</h2>
507
508 iScroll can snap to fixed positions and elements.
509
510 ### <small>options.</small>snap
511
512 The simplest snap config is as follow:
513
514 ```js
515 var myScroll = new IScroll('#wrapper', {
516 snap: true
517 });
518 ```
519
520 This would automatically split the scroller into pages the size of the container.
521
522 `snap` also takes a string as a value. The string will be the selector to the elements the scroller will be snapped to. So the following
523
524 ```js
525 var myScroll = new IScroll('#wrapper', {
526 snap: 'li'
527 });
528 ```
529
530 would snap to each and every `LI` tag.
531
532 To help you navigate through the snap points iScroll grants access to a series of interesting methods.
533
534 ### goToPage(x, y, time, easing)
535
536 `x` and `y` represent the page number you want to scroll to in the horizontal or vertical axes (yeah, it's the plural of *axis*, I checked). If the scroller in mono-dimensional, just pass `0` to the axis you don't need.
537
538 `time` is the duration of the animation, `easing` the easing function used to scroll to the point. Refer to the **option.bounceEasing** in the [Advanced features](#advanced-features). They are both optional.
539
540 ```js
541 myScroll.goToPage(10, 0, 1000);
542 ```
543
544 This would scroll to the 10th page on the horizontal axis in 1 second.
545
546 ### next()<br/>prev()
547
548 Go to the next and previous page based on current position.
549
550 <h2 id="zoom">Zoom</h2>
551
552 To use the pinch/zoom functionality you better use the `iscroll-zoom.js` script.
553
554 ### <small>options.</small>zoom
555
556 Set this to `true` to activate zoom.
557
558 Default: `false`
559
560 ### <small>options.</small>zoomMax
561
562 Maximum zoom level.
563
564 Default: `4`
565
566 ### <small>options.</small>zoomMin
567
568 Minimum zoom level.
569
570 Default: `1`
571
572 ### <small>options.</small>startZoom
573
574 Starting zoom level.
575
576 Default: `1`
577
578 ### <small>options.</small>wheelAction
579
580 Wheel action can be set to `'zoom'` to have the wheel regulate the zoom level instead of scrolling position.
581
582 Default: `undefined` (ie: the mouse wheel scrolls)
583
584 To sum up, a nice zoom config would be:
585
586 ```js
587 myScroll = new IScroll('#wrapper', {
588 zoom: true,
589 mouseWheel: true,
590 wheelAction: 'zoom'
591 });
592 ```
593
594 <div class="important">
595 <p>The zoom is performed with CSS transform. iScroll can zoom only on browsers that support that.</p>
596 </div>
597
598 <div class="tip">
599 <p>Some browsers (notably webkit based ones) take a snapshot of the zooming area as soon as they are placed on the hardware compositing layer (say as soon as you apply a transform to them). This snapshot is used as a texture for the zooming area and it can hardly be updated. This means that your texture will be based on elements at <strong>scale 1</strong> and zooming in will result in blurred, low definition text and images.</p>
600
601 <p>A simple solution is to load content at double (or triple) its actual resolution and scale it down inside a <code>scale(0.5)</code> div. This should be enough to grant you a better result. I hope to be able to post more demos soon</p>
602 </div>
603
604 Refer to the [zoom demo](http://lab.cubiq.org/iscroll5/demos/zoom/).
605
606 ### zoom(scale, x, y, time)
607
608 Juicy method that lets you zoom programmatically.
609
610 `scale` is the zoom factor.
611
612 `x` and `y` the focus point, aka the center of the zoom. If not specified, the center of the screen will be used.
613
614 `time` is the duration of the animation in milliseconds (optional).
615
616 <h2 id="infinite-scrolling">Infinite scrolling</h2>
617
618 iScroll integrates a smart caching system that allows to handle of a virtually infinite amount of data using (and reusing) just a bunch of elements.
619
620 Infinite scrolling is in an early stage of development and although it can be considered stable, it is not ready for wide consumption.
621
622 Please review the [infinite demo](http://lab.cubiq.org/iscroll5/demos/infinite/) and send your suggestions and bug reports.
623
624 I will add more details as soon as the functionality evolves.
625
626 <h2 id="advanced-options">Advanced options</h2>
627
628 For the hardcore developer.
629
630 ### <small>options.</small>bindToWrapper
631
632 The `move` event is normally bound to the document and not the scroll container. When you move the cursor/finger out of the wrapper the scrolling keeps going. This is usually what you want, but you can also bind the move event to wrapper itself. Doing so as soon as the pointer leaves the container the scroll stops.
633
634 Default: `false`
635
636 ### <small>options.</small>bounceEasing
637
638 Easing function performed during the bounce animation. Valid values are: `'quadratic'`, `'circular'`, `'back'`, `'bounce'`, `'elastic'`. See the [bounce easing demo](http://lab.cubiq.org/iscroll5/demos/bounce-easing/), drag the scroller down and release.
639
640 `bounceEasing` is a bit smarter than that. You can also feed a custom easing function, like so:
641
642 ```js
643 bounceEasing: {
644 style: 'cubic-bezier(0,0,1,1)',
645 fn: function (k) { return k; }
646 }
647 ```
648
649 The above would perform a linear easing. The `style` option is used every time the animation is executed with CSS transitions, `fn` is used with `requestAnimationFrame`. If the easing function is too complex and can't be represented by a cubic bezier just pass `''` (empty string) as `style`.
650
651 Note that `bounce` and `elastic` can't be performed by CSS transitions.
652
653 Default: `'circular'`
654
655 ### <small>options.</small>bounceTime
656
657 Duration in millisecond of the bounce animation.
658
659 Default: `600`
660
661 ### <small>options.</small>deceleration
662
663 This value can be altered to change the momentum animation duration/speed. Higher numbers make the animation shorter. Sensible results can be experienced starting with a value of `0.01`, bigger than that basically doesn't make any momentum at all.
664
665 Default: `0.0006`
666
667 ### <small>options.</small>mouseWheelSpeed
668
669 Set the speed of the mouse wheel.
670
671 Default: `20`
672
673 ### <small>options.</small>preventDefaultException
674
675 These are all the exceptions when `preventDefault()` would be fired anyway despite the **preventDefault** option value.
676
677 This is a pretty powerful option, if you don't want to `preventDefault()` on all elements with *formfield* class name for example, you could pass the following:
678
679 ```js
680 preventDefaultException: { className: /(^|\s)formfield(\s|$)/ }
681 ```
682
683 Default: `{ tagName: /^(INPUT|TEXTAREA|BUTTON|SELECT)$/ }`.
684
685 ### <small>options.</small>resizePolling
686
687 When you resize the window iScroll has to recalculate elements position and dimension. This might be a pretty daunting task for the poor little fella. To give it some rest the polling is set to 60 milliseconds.
688
689 By reducing this value you get better visual effect but the script becomes more aggressive on the CPU. The default value seems a good compromise.
690
691 Default: `60`
692
693 <h2 id="refresh">Mastering the refresh method</h2>
694
695 iScroll needs to know the exact dimensions of both the wrapper and the scroller. They are computed at start up but if your elements change in size, we need to tell iScroll that you are messing with the DOM.
696
697 This is achieved by calling the `refresh` method with the right timing. Please follow me closely, understanding this will save you hours of frustration.
698
699 Every time you touch the DOM the browser renderer repaints the page. Once this repaint has happened we can safely read the new DOM properties. The repaint phase is not instantaneous and it happens only at the end of the scope that triggered it. That's why we need to give the renderer a little rest before refreshing the iScroll.
700
701 To ensure that javascript gets the updated properties you should defer the refreh with something like this:
702
703 ```js
704 ajax('page.php', onCompletion);
705
706 function onCompletion () {
707 // Update here your DOM
708
709 setTimeout(function () {
710 myScroll.refresh();
711 }, 0);
712 };
713 ```
714
715 We have placed the `refresh()` call into a zero timeout. That is likely all you need to correctly refresh the iScroll boundaries. There are other ways to wait for the repaint, but the zero-timeout has proven pretty solid.
716
717 <div class="tip">
718 <p>Consider that if you have a very complex HTML structure you may give the browser some more rest and raise the timeout to 100 or 200 milliseconds.</p>
719
720 <p>This is generally true for all the tasks that have to be done on the DOM. Always give the renderer some rest.</p>
721 </div>
722
723 <h2 id="custom-events">Custom events</h2>
724
725 iScroll also emits some useful custom events you can hook to.
726
727 To register them you use the `on(type, fn)` method.
728
729 ```js
730 myScroll = new IScroll('#wrapper');
731 myScroll.on('scrollEnd', doSomething);
732 ```
733
734 The above code executes the `doSomething` function every time the content stops scrolling.
735
736 The available types are:
737
738 * **beforeScrollStart**, executed as soon as user touches the screen but before the scrolling has initiated.
739 * **scrollCancel**, scroll initiated but didn't happen.
740 * **scrollStart**, the scroll started.
741 * **scroll**, the content is scrolling. Available only in `scroll-probe.js` edition. See [onScroll event](#onscroll).
742 * **scrollEnd**, content stopped scrolling.
743 * **flick**, user flicked left/right.
744 * **zoomStart**, user started zooming.
745 * **zoomEnd**, zoom ended.
746
747 <h2 id="onscroll">onScroll event</h2>
748
749 The `scroll` event is available on **iScroll probe edition** only (`iscroll-probe.js`). The probe behavior can be altered through the `probeType` option.
750
751 ### <small>options.</small>probeType
752
753 This regulates the probe aggressiveness or the frequency at which the `scroll` event is fired. Valid values are: `1`, `2`, `3`. The higher the number the more aggressive the probe. The more aggressive the probe the higher the impact on the CPU.
754
755 `probeType: 1` has no impact on performance. The `scroll` event is fired only when the scroller is not busy doing its stuff.
756
757 `probeType: 2` always executes the `scroll` event except during momentum and bounce. This resembles the native `onScroll` event.
758
759 `probeType: 3` emits the `scroll` event with a to-the-pixel precision. Note that the scrolling is forced to `requestAnimationFrame` (ie: `useTransition:false`).
760
761 Please see the [probe demo](http://lab.cubiq.org/iscroll5/demos/probe/).
762
763 <h2 id="key-bindings">Key bindings</h2>
764
765 You can activate support for keyboards and remote controls with the `keyBindings` option. By default iScroll listens to the arrow keys, page up/down, home/end but they are (wait for it) totally customizable.
766
767 You can pass an object with the list of key codes you want iScroll to react to.
768
769 The default values are as follow:
770
771 ```js
772 keyBindings: {
773 pageUp: 33,
774 pageDown: 34,
775 end: 35,
776 home: 36,
777 left: 37,
778 up: 38,
779 right: 39,
780 down: 40
781 }
782 ```
783
784 You can also pass a string (eg: `pageUp: 'a'`) and iScroll will convert it for you. You could just think of a key code and iScroll would read it out of your mind.
785
786 <h2 id="scroller-info">Useful scroller info</h2>
787
788 iScroll stores many useful information that you can use to augment your application.
789
790 You will probably find useful:
791
792 * **myScroll.x/y**, current position
793 * **myScroll.directionX/Y**, last direction (-1 down/right, 0 still, 1 up/left)
794 * **myScroll.currentPage**, current snap point info
795
796 These pieces of information may be useful when dealing with custom events. Eg:
797
798 ```js
799 myScroll = new IScroll('#wrapper');
800 myScroll.on('scrollEnd', function () {
801 if ( this.x < -1000 ) {
802 // do something
803 }
804 });
805 ```
806
807 The above executes some code if the `x` position is lower than -1000px when the scroller stops. Note that I used `this` instead of `myScroll`, you can use both of course, but iScroll passes itself as `this` context when firing custom event functions.
808
809 <h2 id="destroy">Destroy</h2>
810
811 The public `destroy()` method can be used to free some memory when the iScroll is not needed anymore.
812
813 ```js
814 myScroll.destroy();
815 myScroll = null;
816 ```
817
818 <h2 id="contributing">Contributing and CLA</h2>
819
820 If you want to contribute to the iScroll development, before I can accept your submission I have to ask you to sign the [Contributor License Agreement](http://cubiq.org/iscroll/cla/). Unfortunately that is the only way to enforce the openness of the script.
821
822 As an end user you have to do nothing of course. Actually the CLA ensures that nobody will even come after you asking for your first born for using the iScroll.
823
824 Please note that pull requests may take some time to be accepted. Testing iScroll is one of the most time consuming tasks of the project. iScroll works from desktop to smartphone, from tablets to smart TVs. I do not have physical access to all the testing devices, so before I can push a change I have to make sure that the new code is working everywhere.
825
826 Critical bugs are usually applied very quickly, but enhancements and coding style changes have to pass a longer review phase. *Remember that this is still a side project for me.*
827
828 <h2 id="whos">Who is using iScroll</h2>
829
830 It's impossible to track all the websites and applications that use the iScroll. It has been spotted on: Apple, Microsoft, People, LinkedIn, IKEA, Nike, Playboy, Bose, and countless others.
831
832 <h2 id="license">License (MIT)</h2>
833
834
835 Copyright (c) 2014 Matteo Spinelli, [cubiq.org](http://cubiq.org/)
836
837 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
838
839 The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
840
841 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.