使用window.MutationObserver技术和jquery.nicescroll.js组件l封装通用滚动条插件


(function ($) {

    //=====启动轮子
    var openMutationObserver = function (controlID, options) {
        var ID = controlID;
        //MutationObserver监听

        var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
        var observer = new MutationObserver(function () {
            options.resizeNiceScroll();
        });
        observer.observe(document.getElementById(ID), { attributes: true, subtree: true, attributeFilter: ['style'], attributeOldValue: true });
    }
    //=====窗体调整
    var windowResize = function (controlID, options) {
        var ID = controlID;
        jQuery(window).resize(function () {
            options.resizeNiceScroll();
        });

    }


    //初始化
    var init = function (contains, options) {
        var controlID = options.id;
        //1 窗体调整 
        windowResize(controlID, options);
        //2 启动监听
        openMutationObserver(controlID, options);
        //3 加载滚动条
        options.reLoadNiceScroll();

        return options;//返回
    };


    //main #container
    $.fn.extend({
        lzscroll: function (options) {
            var defaults = {
                id: "",
                cursoropacitymax: 0.9,
                cursorwidth: "8px",
                cursorcolor: "#ccc",
                resizeNiceScroll: function () {
                    //计算滚动条
                    jQuery("#" + this.id).getNiceScroll().resize();
                },
                reLoadNiceScroll: function () {
                    //重载滚动条
                    jQuery("#" + this.id).niceScroll({ cursoropacitymax: this.cursoropacitymax, cursorwidth: this.cursorwidth, cursorcolor: this.cursorcolor });
                },
                removeNiceScroll: function () {
                    //移除滚动条
                    jQuery("#" + this.id).getNiceScroll().remove();
                }
            }
            var options = jQuery.extend(defaults, options);

            //入口点
            return init(this, options);
        }
    });

})(jQuery);
/*
//=======入口
jQuery(document).ready(function () {
    //接受返回操作对象
    window.lzscroll = jQuery("body").lzscroll({
        id: "Lzcontleft",
        removeNiceScroll: function () {
            //移除滚动条
            jQuery("#" + this.id).getNiceScroll().remove();
            jQuery("#" + this.id).removeAttr("style");
        }
    });
});
*/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值