nicescroll是一款基于jQuery的滚动条插件,使用简单,功能强大,较mCustomScrollbar而言:
关于nicescroll使用方法,网上很多,但是关于nicescroll事件回调则很少,本文除介绍nicescroll使用方法和常用配置外,将着重介绍如何实现nicescroll事件回调,如:滚动开始、滚动结束等。参考 nicescroll github, 使用方法和参数配置如下:
使用方法:
// 1. Simple mode, it styles document scrollbar:
$(function() {
$("body").niceScroll();
});
// 2. Instance with object returned:
var nice = false;
$(function() {
nice = $("body").niceScroll();
});
// 3. 设置滚动条颜色:
$(function() {
$("#thisdiv").niceScroll({cursorcolor:"#00F"});
});
// 4. DIV with "wrapper", formed by two divs, the first is the vieport, the latter is the content:
$(function() {
$("#viewportdiv").niceScroll("#wrapperdiv",{cursorcolor:"#00F"});
});
// 5. Get nicescroll object:
var nice = $("#mydiv").getNiceScroll();
// 6. Hide scrollbars:
$("#mydiv").getNiceScroll().hide();
// 7. Check for scrollbars resize (when content or position have changed):
$("#mydiv").getNiceScroll().resize();
// 8. Scrolling to a position:
$("#mydiv").getNiceScroll(0).doScrollLeft(x, duration); // Scroll X Axis
$("#mydiv").getNiceScroll(0).doScrollTop(y, duration); // Scroll Y Axis
参数配置如下:
$("#thisdiv").niceScroll({
cursorcolor: "#424242", // 滚动条颜色
cursoropacitymin: 0, // 滚动条透明度
cursoropacitymax: 1, // 滚动条透明度, 显示min->max, 隐藏max->min
cursorwidth: "5px", // 滚动条宽度
cursorborder: "1px solid #fff", // css definition for cursor border
cursorborderradius: "5px", // border radius in pixel for cursor
zindex: "auto" | [number], // change z-index for scrollbar div
scrollspeed: 60, // scrolling speed
mousescrollstep: 40, // scrolling speed with mouse wheel (pixel)
touchbehavior: false, // DEPRECATED!! use "emulatetouch"
emulatetouch: false, // enable cursor-drag scrolling like touch devices in desktop computer
hwacceleration: true, // use hardware accelerated scroll when supported
boxzoom: false, // enable zoom for box content
dblclickzoom: true, // (only when boxzoom=true) zoom activated when double click on box
gesturezoom: true, // (only when boxzoom=true and with touch devices) zoom activated when pinch out/in on box
grabcursorenabled: true // (only when touchbehavior=true) display "grab" icon
autohidemode: true, // how hide the scrollbar works, possible values:
true | // hide when no scrolling
"cursor" | // only cursor hidden
false | // do not hide,
"leave" | // hide only if pointer leaves content
"hidden" | // hide always
"scroll", // show only on scroll
background: "", // change css for rail background
iframeautoresize: true, // autoresize iframe on load event
cursorminheight: 32, // set the minimum cursor height (pixel)
preservenativescrolling: true, // you can scroll native scrollable areas with mouse, bubbling mouse wheel event
railoffset: false, // you can add offset top/left for rail position
bouncescroll: false, // (only hw accell) enable scroll bouncing at the end of content as mobile-like
spacebarenabled: true, // enable page down scrolling when space bar has pressed
railpadding: { top: 0, right: 0, left: 0, bottom: 0 }, // set padding for rail bar
disableoutline: true, // for chrome browser, disable outline (orange highlight) when selecting a div with nicescroll
horizrailenabled: true, // nicescroll can manage horizontal scroll
railalign: right, // alignment of vertical rail
railvalign: bottom, // alignment of horizontal rail
enabletranslate3d: true, // nicescroll can use css translate to scroll content
enablemousewheel: true, // nicescroll can manage mouse wheel events
enablekeyboard: true, // nicescroll can manage keyboard events
smoothscroll: true, // scroll with ease movement
sensitiverail: true, // click on rail make a scroll
enablemouselockapi: true, // can use mouse caption lock API (same issue on object dragging)
cursorfixedheight: false, // set fixed height for cursor in pixel
hidecursordelay: 400, // set the delay in microseconds to fading out scrollbars
directionlockdeadzone: 6, // dead zone in pixels for direction lock activation
nativeparentscrolling: true, // detect bottom of content and let parent to scroll, as native scroll does
enablescrollonselection: true, // enable auto-scrolling of content when selection text
cursordragspeed: 0.3, // speed of selection when dragged with cursor
rtlmode: "auto", // horizontal div scrolling starts at left side
cursordragontouch: false, // drag cursor in touch / touchbehavior mode also
oneaxismousemode: "auto", // it permits horizontal scrolling with mousewheel on horizontal only content, if false (vertical-only) mousewheel don't scroll horizontally, if value is auto detects two-axis mouse
scriptpath: "" // define custom path for boxmode icons ("" => same script path)
preventmultitouchscrolling: true // prevent scrolling on multitouch events
disablemutationobserver: false // force MutationObserver disabled,
enableobserver: true // enable DOM changing observer, it tries to resize/hide/show when parent or content div had changed
scrollbarid: false // set a custom ID for nicescroll bars
});
nicescroll事件回调
以滚动scrollend回调为例,有2种实现方式:
方式1:
$("#mydiv").getNiceScroll(0).scrollend(function(e) {
// TODO
});
方式2:
var scroll = $("#mydiv").niceScroll();
scroll.scrollend(function(e) {
// TODO
});
其它回调诸如:scrollstart, scrollcancel等类似.