转自:http://www.cnblogs.com/jinqi79731/p/nicescroll.htmlcss
Nicescroll滚动条插件是一个很是强大的基于JQUERY的滚动条插件,不须要增长额外的css,几乎全浏览器兼容。ie6+,实现只须要一段代码,侵入性很是小,样式可彻底自定义,支持触摸事件,可在触摸屏上使用。html
官网地址:http://www.areaaperta.com/nicescroll/jquery
引入核心文件,插件须要引入1.5.X以上版本的jquery库chrome
最简单的用法以下:api
1 $(document).ready( 2 function() { 3 $("html").niceScroll(); 4 } 5 );
注意:必定要放在 $(document).ready 中进行初始化!浏览器
隐藏滚动条:(当在同一页面中使用多个nicescroll插件时,要及时隐藏用完的nicescroll对象,加载时,须要先show,再resize。)ide
1 $("#mydiv").getNiceScroll().hide();
检测滚动条是否重置大小(当窗口改变大小时):动画
1 $("#mydiv").getNiceScroll().resize();
滚动到某个位置:this
1 $("#mydiv").getNiceScroll(0).doScrollLeft(x, duration); // Scroll X Axis;第一个参数为滚动的位置,第二个为滚动须要的时间 2 $("#mydiv").getNiceScroll(0).doScrollTop(y, duration); // Scroll Y Axis
设置各类参数:spa
1 $("#thisdiv").niceScroll({ 2 cursorcolor: "#424242", // 改变滚动条颜色,使用16进制颜色值 3 cursoropacitymin: 0, // 当滚动条是隐藏状态时改变透明度, 值范围 1 到 0 4 cursoropacitymax: 1, // 当滚动条是显示状态时改变透明度, 值范围 1 到 0 5 cursorwidth: "5px", // 滚动条的宽度,单位:便素 6 cursorborder: "1px solid #fff", // CSS方式定义滚动条边框 7 cursorborderradius: "5px", // 滚动条圆角(像素) 8 zindex: "auto" | <number>, // 改变滚动条的DIV的z-index值 9 scrollspeed: 60, // 滚动速度 10 mousescrollstep: 40, // 鼠标滚轮的滚动速度 (像素) 11 touchbehavior: false, // 激活拖拽滚动 12 hwacceleration: true, // 激活硬件加速 13 boxzoom: false, // 激活放大box的内容 14 dblclickzoom: true, // (仅当 boxzoom=true时有效)双击box时放大 15 gesturezoom: true, // (仅 boxzoom=true 和触屏设备时有效) 激活变焦当out/in(两个手指外张或收缩) 16 grabcursorenabled: true // (仅当 touchbehavior=true) 显示“抓住”图标display "grab" icon 17 autohidemode: true, // 隐藏滚动条的方式, 可用的值: 18 true | // 无滚动时隐藏 19 "cursor" | // 隐藏 20 false | // 不隐藏, 21 "leave" | // 仅在指针离开内容时隐藏 22 "hidden" | // 一直隐藏 23 "scroll", // 仅在滚动时显示 24 background: "", // 轨道的背景颜色 25 iframeautoresize: true, // 在加载事件时自动重置iframe大小 26 cursorminheight: 32, // 设置滚动条的最小高度 (像素) 27 preservenativescrolling: true, // 你能够用鼠标滚动可滚动区域的滚动条和增长鼠标滚轮事件 28 railoffset: false, // 可使用top/left来修正位置 29 bouncescroll: false, // (only hw accell) 启用滚动跳跃的内容移动 30 spacebarenabled: true, // 当按下空格时使页面向下滚动 31 railpadding: { top: 0, right: 0, left: 0, bottom: 0 }, // 设置轨道的内间距 32 disableoutline: true, // 当选中一个使用nicescroll的div时,chrome浏览器中禁用outline 33 horizrailenabled: true, // nicescroll能够管理水平滚动 34 railalign: right, // 对齐垂直轨道 35 railvalign: bottom, // 对齐水平轨道 36 enabletranslate3d: true, // nicescroll 可使用CSS变型来滚动内容 37 enablemousewheel: true, // nicescroll能够管理鼠标滚轮事件 38 enablekeyboard: true, // nicescroll能够管理键盘事件 39 smoothscroll: true, // ease动画滚动 40 sensitiverail: true, // 单击轨道产生滚动 41 enablemouselockapi: true, // 能够用鼠标锁定API标题 (相似对象拖动) 42 cursorfixedheight: false, // 修正光标的高度(像素) 43 hidecursordelay: 400, // 设置滚动条淡出的延迟时间(毫秒) 44 directionlockdeadzone: 6, // 设定死区,为激活方向锁定(像素) 45 nativeparentscrolling: true, // 检测内容底部便于让父级滚动 46 enablescrollonselection: true, // 当选择文本时激活内容自动滚动 47 cursordragspeed: 0.3, // 设置拖拽的速度 48 rtlmode: "auto", // DIV的水平滚动从左边开始 49 cursordragontouch: false, // 使用触屏模式来实现拖拽 50 oneaxismousemode: "auto", // 当只有水平滚动时能够用鼠标滚轮来滚动,若是设为false则不支持水平滚动,若是设为auto支持双轴滚动 51 scriptpath: "" // 为boxmode图片自定义路径 ("" => same script path) 52 preventmultitouchscrolling: true // 防止多触点事件引起滚动 53 });
当插件放在具备absolute浮动的容器中,并设置了top值时,插件的top会出现问题,解决方法使用插件的railoffset属性:
1 railoffset, you can add offset top/left for rail position (default:false)
本人使用过程当中遇到的问题:
加载数据的时候nicescroll滚动条闪烁,还有对绝对定位的元素(例以下拉框)使用滚动条,特别是在须要滚动条比较多的页面,致使位置错乱,这些问题须要对设置滚动条的元素的直接子元素设置,官方给出的答案是:
因此使用的时候应该这么设置:
1 $('#addContent_scroll_wrap').niceScroll('#sumStackColumn',{'cursorcolor':'#686868','cursorborder':'none','smoothscroll': 'false','autohidemode':false});
可是以后仍然发现一个问题,在页面比较复杂的状况下,可能会出现,下拉框出来了,可是滚动条没出来,官方给出的说法是:当在同一页面中使用多个nicescroll插件时,要及时隐藏用完的nicescroll对象,加载时,须要先show,再resize。
因此采用resize()的方法,重置了滚动条,可是必定要注意,这里选择滚动条的对象,必定不能选错,不然也是无效的:
1 $('#addContent_scroll_wrap').getNiceScroll().show(); 2 $('#addContent_scroll_wrap').getNiceScroll().resize();