Smooth scrolling for the web
iScroll is a high performance, small footprint, dependency free, multi-platform javascript scroller.
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.
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.
Even on platforms where native scrolling is good enough, iScroll adds features that wouldn't be possible otherwise. Specifically:
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.
Currently we have the following fragrances:
iscroll.js script, so keep an eye on it).So you want to be an iScroll master. Cool, because that is what I'll make you into.
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. Most of the script features are outlined there.
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.
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.
The optimal HTML structure is:
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.
The minimal call to initiate the script is as follow:
<script type="text/javascript">
var myScroll = new IScroll('#wrapper');
</script>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:
var wrapper = document.getElementById('wrapper');
var myScroll = new IScroll(wrapper);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:
var myScroll = new IScroll('.wrapper');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.
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.
To sum up, the smallest iScroll configuration is:
<head>
...
<script type="text/javascript" src="iscroll.js"></script>
<script type="text/javascript">
var myScroll;
function loaded() {
myScroll = new IScroll('#wrapper');
}
</script>
</head>
...
<body onload="loaded()">
</body>Refer to the barebone example for more details on the minimal CSS/HTML requirements.
iScroll can be configured by passing a second parameter during the initialization phase.
var myScroll = new IScroll('#wrapper', {
mouseWheel: true,
scrollbars: true
});The example above turns on mouse wheel support and scrollbars.
After initialization you can access the normalized values from the options object. Eg:
console.dir(myScroll.options);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.
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.
Nonetheless it is important to understand which mechanisms iScroll works on and how to configure them.
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).
This might be useful when scrolling sensitive content such as Flash, iframes and videos, but be warned: performance loss is huge.
Default: true
iScroll uses CSS transition to perform animations (momentum and bounce). By setting this to false, requestAnimationFrame is used instead.
On modern browsers the difference is barely noticeable. On older devices transitions perform better.
Default: true
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).
Default: true
When the scroller meets the boundary it performs a small bounce animation. Disabling bounce may help reach smoother results on old or slow devices.
Default: true
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).
Default: false
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.
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).
For example to disable mouse and pointer events:
var myScroll = new IScroll('#wrapper', {
disableMouse: true,
disablePointer: true
});Default: false
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.
See event passthrough demo on a mobile device. Note that this can be set to 'horizontal' to inverse the behavior (native horizontal scroll, vertical iScroll).
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.
Sometimes you just want to move freely with no constrains. In these cases you can set this option to true. See 2D scroll demo.
Default: false
Set this to true to activate keyboard (and remote controls) interaction. See the Key bindings section below for more information.
Default: false
Meaningful when mouse wheel support is activated, in which case it just inverts the scrolling direction. (ie. going down scrolls up and vice-versa).
Default: false
You can turn on/off the momentum animation performed when the user quickly flicks on screen. Turning this off greatly enhances performance.
Default: true
Listen to the mouse wheel event.
Default: false
Whether or not to preventDefault() when events are fired. This should be left true unless you really know what you are doing.
See preventDefaultException in the Advanced options for more control over the preventDefault behavior.
Default: true
Wheter or not to display the default scrollbars. See more in the Scrollbar section.
Default: false.
By default only vertical scrolling is enabled. If you need to scroll horizontally you have to set scrollX to true. See horizontal demo.
See also the freeScroll option.
Default: scrollX: false, scrollY: true
By default iScroll starts at 0, 0 (top left) position, you can instruct the scroller to kickoff at a different location.
Default: 0
Set this to true to let iScroll emit a custom tap event when the scroll area is clicked/tapped but not scrolled.
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:
element.addEventListener('tap', doSomething, false); \\ Native
$('#element').on('tap', doSomething); \\ jQueryYou can also customize the event name by passing a string. Eg:
tap: 'myCustomTapEvent'In this case you'd listen to myCustomTapEvent.
Default: false
The scrollbars are
Is this project still alive?
Snap threshold on scrollbar(indicator) not working.
google map drag not working with iScrolll
华为9,小米8手机。iscroll的使用阻止了页面的其他事件
Vertical scroll not working on iPhone or iPad
How to use iScroll in Angular / TypeScript project?
Input of type "file" is not reachable
Not working properly in touch screen device (mobile, tablets)
android手机上,列表当包含input输入框,在软键盘弹出再收起时,列表上面一部分下拉不了
NPM packages are not up to date