使用滚动条插件mCustomScrollbar

本文介绍了如何使用jQuery的mCustomScrollbar插件实现自定义滚动条,并提供了具体实例代码,包括初始化、更新滚动条及自动隐藏功能。

之前自己写了一个滚动条,后来了解到滚动条不同的浏览器的兼容性不一样。

于是开始使用jquery的滚动条插件

先在js文件中实例化一个对象

createOrUpdateScroll:function(cls,type, hideBar,axis){ 
        //cls:待添加滚动条的容器
        //type:0-初始化, 1-更新
        //hideBar-是否自动隐藏滚动条,true:是,false:否
        //axis:待建立的坐标轴:x / y / yx
        var obj = $(cls);
        if(0 == type){
            var autoHideScrollbar = typeof(hideBar) == 'undefined' ? true : hideBar;
            var $axis = typeof(axis) == 'undefined' ? 'y' : axis;
            obj.mCustomScrollbar({
                axis:$axis,
                autoHideScrollbar:autoHideScrollbar,
                theme:"light-thin",
                scrollInertia:260
            });
        }else{
            obj.mCustomScrollbar("update");
            obj.mCustomScrollbar("scrollTo","top");
        }
    }

然后在需要加滚动条的div中设置样式

div{
height:500px;
flowover-y:auto;
}

这样就行了

 

=====================后续增加=========================

$("#jqDiv-body").mCustomScrollbar({
            autoHideScrollbar:true,
            theme:"light-thin",
            scrollInertia:260,
            callbacks:{
                whileScrolling:function(){
                    if(this.mcs.topPct > 10){
                        $(".right_tool_panel").fadeIn(1000);
                    }else{
                        $(".right_tool_panel").fadeOut(1000);
                    }
                }
            }
        });

上述写法即可

如果存在两个滚动条,可能是因为文件mCustomScrollbar.js加载了两次

转载于:https://www.cnblogs.com/zhengyuanyuan/p/8435592.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值