Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
I

iscroll

> 编程语言
Open source

Smooth scrolling for the web

12.8K stars0 likes3 views
WebsiteGitHub

About

Smooth scrolling for the web

iScroll, 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:

  • Granular control over the scroll position, even during momentum. You can always get and set the x,y coordinates of the scroller.
  • Animation can be customized with user defined easing functions (bounce, elastic, back, ...).
  • You can easily hook to a plethora of custom events (onBeforeScrollStart, onScrollStart, onScroll, onScrollEnd, flick, ...).
  • Out of the box multi-platform support. From older Android devices to the latest iPhone, from Chrome to Internet Explorer.

The many faces of iScroll

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, it is the general purpose script. It includes the most commonly used features and grants very high performance in a small footprint.
  • 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.
  • 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).
  • iscroll-zoom.js, adds zooming to the standard scroll.
  • 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.

Getting started

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:

xml

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:

xml
<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:

javascript
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:

javascript
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.

Initialization

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:

xml
<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.

Configuring the iScroll

iScroll can be configured by passing a second parameter during the initialization phase.

javascript
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:

javascript
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.

Understanding the core

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.

options.useTransform

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

options.useTransition

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

options.HWCompositing

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

Basic features

options.bounce

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

options.click

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

options.disableMouse
options.disablePointer
options.disableTouch

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:

javascript
var myScroll = new IScroll('#wrapper', {
    disableMouse: true,
    disablePointer: true
});

Default: false

options.eventPassthrough

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).

options.freeScroll

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

options.keyBindings

Set this to true to activate keyboard (and remote controls) interaction. See the Key bindings section below for more information.

Default: false

options.invertWheelDirection

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

options.momentum

You can turn on/off the momentum animation performed when the user quickly flicks on screen. Turning this off greatly enhances performance.

Default: true

options.mouseWheel

Listen to the mouse wheel event.

Default: false

options.preventDefault

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

options.scrollbars

Wheter or not to display the default scrollbars. See more in the Scrollbar section.

Default: false.

options.scrollX
options.scrollY

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

options.startX
options.startY

By default iScroll starts at 0, 0 (top left) position, you can instruct the scroller to kickoff at a different location.

Default: 0

options.tap

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:

javascript
element.addEventListener('tap', doSomething, false); \\ Native
$('#element').on('tap', doSomething); \\ jQuery

You can also customize the event name by passing a string. Eg:

javascript
tap: 'myCustomTapEvent'

In this case you'd listen to myCustomTapEvent.

Default: false

Scrollbars

The scrollbars are

Issues· 576 open

View all issuesOpen on GitHub
  • #1259

    Is this project still alive?

    Updated Mar 27, 2019
  • #1260

    Snap threshold on scrollbar(indicator) not working.

    Updated Mar 11, 2019
  • #1258

    google map drag not working with iScrolll

    Updated Feb 8, 2019
  • #1254

    华为9,小米8手机。iscroll的使用阻止了页面的其他事件

    Updated Jan 31, 2019
  • #1056

    Vertical scroll not working on iPhone or iPad

    Updated Jan 14, 2019
  • #1257

    How to use iScroll in Angular / TypeScript project?

    Updated Jan 2, 2019
  • #600

    Input of type "file" is not reachable

    Updated Dec 31, 2018
  • #1166

    Not working properly in touch screen device (mobile, tablets)

    Updated Dec 11, 2018
  • #1233

    android手机上,列表当包含input输入框,在软键盘弹出再收起时,列表上面一部分下拉不了

    Updated Dec 4, 2018
  • #1252

    NPM packages are not up to date

    Updated Oct 29, 2018

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 28, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言