nanoScroller.js is a jQuery plugin that offers a simplistic way of implementing Mac OS X Lion-styled scrollbars for your website.
It uses minimal HTML markup being .nano > .content
. The other scrollbar div elements .pane > .slider
are added during run time to prevent clutter in templating. The latest version utilizes native scrolling and works with the iPad, iPhone, and some Android Tablets.
Please visit the downloads section to get the js and css template file (.zip).
To start using, you to do three basic things:
The following type of markup structure is needed to make the plugin work:
<div id="about" class="nano">
<div class="content"> ... content here ... </div>
</div>
Copy the HTML markup. Change .nano
into something related to your content. Though you can also remove that attribute as long as you have a parent div with an ID reference. e.g. #parent > .nano
. nano
and content
classnames can be customized via plugin options (in that case you must rename them inside the plugin's CSS file as well).
Link to the nanoscroller.css
file inside your page's <head>
section (...or copy the contents from it to your page's main stylesheet file).
<link rel="stylesheet" href="nanoscroller.css">
You should specify a width and a height to your container, and apply some custom styling for your scrollbar. Here's an example:
.nano { background: #bba; width: 500px; height: 500px; }
.nano .content { padding: 10px; }
.nano .pane { background: #888; }
.nano .slider { background: #111; }
Running this script will apply the nanoScroller plugin to all DOM elements with a .nano
className.
$(".nano").nanoScroller();
To scroll at the top:
$(".nano").nanoScroller({ scroll: 'top' });
To scroll at the bottom:
$(".nano").nanoScroller({ scroll: 'bottom' });
To scroll at the top with an offset value:
$(".nano").nanoScroller({ scrollTop: value });
To scroll at the bottom with an offset value:
$(".nano").nanoScroller({ scrollBottom: value });
To scroll to an element:
$(".nano").nanoScroller({ scrollTo: $('#a_node') });
To stop the operation. This option will tell the plugin to disable all event bindings and hide the gadget scrollbar from the UI.
$(".nano").nanoScroller({ stop: true });
To flash the scrollbar gadget for an amount of time defined in plugin settings (defaults to 1,5s). Useful if you want to show the user (e.g. on pageload) that there is more content waiting for him.
$(".nano").nanoScroller({ flash: true });
Refresh the scrollbar. This simply re-calculates the position and height of the scrollbar gadget.
$(".nano").nanoScroller();
A custom 'scrollend'
event is triggered on the element every time the user has scrolled to the end of the content element.
$(".nano").bind("scrollend", function(e){
console.log("current HTMLDivElement", e.currentTarget);
});
Some browsers trigger this event more than once each time, so to listen to the custom event, instead of using jQuery's normal .bind
or .on
, you most likely want to use this tiny jQuery debounce plugin to prevent browsers from firing your function more than once every 100ms.
$(".nano").debounce("scrollend", function() {
alert("The end");
}, 100);
Same as the 'scrollend'
event, but it is triggered every time the user has scrolled to the top of the content element.
There are a few options that you can change when running nanoScroller, e.g. $(".nano").nanoScroller({ paneClass: 'myclass' });
Set to true if you want to use native scrolling in iOS 5+. This will disable your custom nanoScroller scrollbar in iOS 5+ and use the native one instead. While the native iOS scrollbar usually works much better, there could possibly be bugs in certain situations.
Default: false
$(".nano").nanoScroller({ iOSNativeScrolling: true });
Sets the minimum height of the slider element.
Default: 20
$(".nano").nanoScroller({ sliderMinHeight: 40 });
Sets the maximum height of the slider element.
Default: null
$(".nano").nanoScroller({ sliderMaxHeight: 200 });
Set to true to prevent page scrolling when top or bottom inside the content div is reached.
Default: false
$(".nano").nanoScroller({ preventPageScrolling: true });
Set to true to disable the resize from nanoscroller. Useful if you want total control of the resize event. If you set this option to true remember to call the reset method so that the scroll don't have strange behavior.
Default: false
$(".nano").nanoScroller({ disableResize: true });
Set to true to stop the scrollbar from auto-hiding itself.
Default: false
$(".nano").nanoScroller({ alwaysVisible: true });
Use this setting to specify the scrollbar hide delay in milliseconds if you have enabled the flash
option.
$(".nano").nanoScroller({ flashDelay: 1000 });
Default: 1500
A classname for scrollbar track element. If you change this setting, you also have to change it in the plugin's CSS file.
Default: 'pane'
$(".nano").nanoScroller({ paneClass: 'scrollPane' });
A classname for scrollbar thumb element. If you change this setting, you also have to change it in the plugin's CSS file.
Default: 'slider'
$(".nano").nanoScroller({ sliderClass: 'scrollSlider' });
A classname for your content div. If you change this setting, you also have to change it in the plugin's CSS file.
Default: 'content'
$(".nano").nanoScroller({ contentClass: 'sliderContent' });
The plugin works by creating a scrollbar gadget (with pre-defined css for styling) and then subscribing the .content
's scroll events to it. Mouse press and drag events are also subscribed to the .pane
and .pane > .slider
to emulate the native scrollbar's mechanism. The system scrollbars are hidden from the viewport (Fig 1). By doing this, we still retain the natural scrolling experience provided by the OS.
We are still working on doing a horizontal scrolling feature. If you're interested in contributing to the project, you are free to fork it and create a pull request.
To build nanoScroller from source you need the following libraries installed:
- Node.js and npm: homepage / download
- Grunt: homepage |
npm install -g grunt
- Make sure that you have Grunt installed.
- In terminal move to nanoscroller folder and run
npm install
to install all dependencies. - Make all Javascript changes in Coffeescript file(s), CSS changes in CSS file(s).
- run
grunt
to build nanoScroller - Make sure that all changes are valid and open a pull request.
- You need to have PhantomJS installed. On Mac OS X the easiest way is to install Homebrew and run
brew install phantomjs
. - run
grunt test
in terminal
Tested desktop browsers:
- IE7+
- Firefox 3+
- Chrome
- Safari 4+
- Opera 11.60+
Mobile support:
- iOS 5+ (iPhone, iPad and iPod Touch)
- iOS 4 (with a polyfill)
- Android Firefox
- Android 2.2/2.3 native browser (with a polyfill)
- Android Opera 11.6 (with a polyfill)
- If you see it's broken on other tablets and mobile devices, please file a ticket in the git repo. Along with model name, and OS of the device.
If you find a bug, please report here at the issues section.
You can use overthrow.js polyfill (~1.5kb minified and gzipped) to make nanoScroller work on many mobile devices. It emulates CSS overflow (overflow: auto;/overflow: scroll;) in devices that are lacking it.
To use overthrow, link to the javascript file in your HTML document...
<script src="overthrow.js"></script>
...and add an overthrow
class to your content
div.
<div id="about" class="nano">
<div class="overthrow content"> ... content here ... </div>
</div>
If you wish to include your site in this list, you can do so by tweeting the link on Twitter with a #nanoScroller
hashtag.
Other people who have contributed code:
- livskiy #83
- iStefo #65
- tahajahangir #60
- n0valyfe #57
- johanbaath #42
- marcelombc #40, #46
- zacstewart #30
- michael-lefebvre #22, #29
- AlicanC #28
- camerond #26
- jesstelford #23
- lluchs #7, #8
- Dlom
- Initially written by James Florentino in CoffeeScript
- Released under MIT License