滚动条插件---jquery.nicescroll.js

nicescroll 滚动条插件是一个非常强大的基于 jQuery 的滚动条插件,不需要增加额外的css,几乎全浏览器兼容。ie6+,实现只需要一段代码,侵入性非常小,样式可完全自定义,支持触摸事件,可在触摸屏上使用。

官网地址:http://www.areaaperta.com/nicescroll/

GitHub下载地址:https://github.com/inuyaksa/jquery.nicescroll

引入核心文件,插件需要引入 1.5.X 以上版本的 jQuery 库

最简单的用法:

$(document).ready(function(){

$("html").niceScroll();

})

注意:一定要放在 $(document).ready 中进行初始化!

隐藏滚动条

$("#myDiv").getNiceScroll().hide();

我在项目中遇到一个问题就是 div 在重新渲染的时候(div内元素大小发生改变),滚动条还处于原来的 div 的长度,受到隐藏滚动条的启发(使用 hide() 但不起作用),使用了:

$("#myDiv").getNiceScroll().remove();

然后再配置使用 niceScroll 竟然奇迹的成功了。(该问题只出现的 IE10 中)

检测滚动条是否重置大小(当窗口改变大小时)

$("#myDiv").getNiceScroll().resize();

滚动到某个位置

$("#myDiv").getNiceScroll(0).doScrollLeft(x, duration); // Scroll X Axis

$("#myDiv").getNiceScroll(0).doScrollTop(y, duration); // Scroll Y Axis

可自定义各种参数

$(document).ready(function(){

$("#thisDiv").niceScroll({cursorcolor: "#00F"});

})

配置参数表

zindex: “auto”,

cursoropacitymin: 0, // 当滚动条是隐藏状态时改变透明度,值范围1到0

cursoropacitymax: 1, // 当滚动条是显示状态时改变透明度,值范围1到0

cursorcolor: “#424242”, // 滚动条颜色,使用16进制颜色值

cursorwidth: “6px”, // 滚动条的宽度,单位:像素

cursorborder: “1px solid #fff”, // CSS 方式定义滚动条边框

cursorborderradius: “5px”, // 滚动条圆角(像素)

scrollspeed: 40, // 滚动速度

mousescrollstep: 9 * 3, // 鼠标滚动的滚动速度(像素)

touchbehavior: false, // deprecated 激活拖拽滚动 (不赞成,不宜用)

emulatetouch: false, // replacing touchbehavior emulate(仿真)

hwacceleration: true, // 激活硬件加速

usetransition: true,

boxzoom: false, // 激活放大box的内容

dblclickzoom: true, // (仅当 boxzoom=true 时有效)双击 box 时放大

gesturezoom: true, // (仅 boxzoom=true 和触屏设备时有效)激活变焦当 out/in (两个手指外账或收缩)

grabcursorenabled: true, // (仅当 透彻behavior=true)显示“抓住”图标 display “grab” icon

autohidemode: true, // 隐藏滚动条的方式,可用的值:true|无滚动时隐藏,“cursor”|隐藏,false|不隐藏,“leave”|仅在指针离开内容时隐藏,“hidden”|一直隐藏,“scroll”|仅在滚动时显示

background: “”, // 轨道的背景颜色

iframeautoresize: true, // 在加载事件时自动重置 iframe 大小

cursorminheight: 32, // 设置滚动条的最小高度(像素)

preservenativescrolling: true, // 你可以用鼠标滚动可滚动区域的滚动条和增加鼠标滚轮事件

railoffset: false, // 可以使用 top/left 来修正位置

railhoffset: false,

bouncescroll: true, // (only hw accell)启用滚动跳跃的内容移动

spacebarenabled: true, // 当按下空格时使页面向下滚动

railpadding: { // 设置轨道的内间距

top: 0,

right: 0,

left: 0,

bottom: 0

},

disableoutline: true, // 当选中一个使用 niceScroll 的 div 时,Chrome浏览器中禁用 outline

horizrailenabled: true, // niceScroll 可以管理水平滚动

railalign: “right”, // 对齐垂直轨道

railvalign: “bottom”, // 对齐水平轨道

enabletranslate3d: true, // niceScroll 可以使用 CSS 变型来滚动内容

enablemousewheel: true, // niceScroll 可以管理鼠标滚轮事件

enablekeyboard: true, // niceScroll 可以管理键盘事件

smoothscroll: true, // ease 动画滚动

sensitiverail: true, // 单击轨道产生滚动

enablemouselockapi: true, // 可以用鼠标锁定 API 标题(类似对象拖动)

// cursormaxheight:false,

cursorfixedheight: false, // 修正光标的高度(像素)

directionlockdeadzone: 6, // 设定死区,为激活方向锁定(像素)

hidecursordelay: 400, // 设置滚动条淡出的延迟时间(毫秒)

nativeparentscrolling: true, // 检测内容底部便于让父级滚动

enablescrollonselection: true, // 当选择文本时激活内容自动滚动

overflowx: true,

overflowy: true,

cursordragspeed: 0.3, // 设置拖拽的速度

rtlmode: “auto”, // DIV 的水平滚动从左边开始

cursordragontouch: false, // 使用触屏模式来实现拖拽

oneaxismousemode: “auto”, // 当只用水平滚动时可以用鼠标来滚动,如果设为 false 则不支持水平滚动,如果设为 auto 支持双轴滚动

scriptpath: getScriptPath(), // 为 boxmode 图片自定义路径

preventmultitouchscrolling: true, // 防止多触点时间引发滚动

disablemutationobserver: false,

enableobserver: true,

scrollbarid: false

另外注意:当在同一页面中使用多个 niceScroll 插件时,要及时隐藏用完的 niceScroll 对象,加载时,需要先 show,再 resize。

当插件放在具有 absolute 浮动的容器中,并设置了 top 值时,插件的 top 会出现问题,解决方法使用插件的 railoffset 属性:

railoff,you can add offset top/left for rail position (default: false)

$("#scrollInner").niceScroll({ 
        cursorcolor: "#99bbba", //滚动条的颜色
        cursoropacitymax: 0.9, //滚动条的透明度,从0-1   
        touchbehavior: false, //使光标拖动滚动像在台式电脑触摸设备 true滚动条拖动不可用  
        cursorwidth: "6px", //滚动条的宽度  单位默认px 
        cursorborder: "0", // 游标边框css定义    
        cursorborderradius: "3px", //滚动条两头的圆角   
        autohidemode: false, //是否隐藏滚动条  true的时候默认不显示滚动条,当鼠标经过的时候显示滚动条   
        zindex: "auto", //给滚动条设置z-index值    
        railvalign:'defaul',
        railpadding: {
            top: 0,
            right: -6,
            left: 0,
            bottom: 0
        }, //滚动条的位置
     });
 

猜你喜欢

转载自blog.csdn.net/weixin_45959504/article/details/105980141
今日推荐