学习从实践开始之jQuery插件开发 菜单插件开发(2)


View Code
(function ($) {
    $.fn.menu = function (options) {
        if (typeof options != 'undefined' && options.constructor === Array) options = { data: options };
        var opts = $.extend({}, $.fn.menu.defaults, options);
        var _tempMenuData = [];
        //返回数据级别
        function getLevel(id) {
            var _level = 0;
            var _o = getMenuData(id);
            while (_o != null) {
                _level++;
                _o = getMenuData(_o.pid);
            }
            return _level;
        }
        //返回数据对象
        function getMenuData(id) {
            for (var i = 0; i < opts.data.length; i++) {
                if (opts.data[i].id == id)
                    return opts.data[i];
            }
            return null;
        }
        //返回生成的HTML
        function getHtml(pid) {
            var _li_data = getData(pid);
            if (_li_data.length == 0) return null;
            var _ul = $('<ul></ul>');
            $.each(_li_data, function (i, _d) {
                var _children = getHtml(_d.id);
                var _li = $('<li></li>').appendTo(_ul);
                if (_d.n == null || _d.n.length == 0) {
                    _li.addClass('spacing');
                } else if (typeof _d.fn === 'function') {
                    $('<a href="javascript:;"></a>').html(_d.n)
                    .click(function () {
                        _d.fn(_d.url);
                    }).appendTo(_li);
                } else if (_d.url.length > 0) {
                    $('<a href="' + _d.url + '"></a>').html(_d.n).appendTo(_li);
                }
                if (_children != null) {
                    _li.addClass('item-has-children');
                    _children.appendTo(_li);
                    _li.bind({
                        mouseover: function () {
                            _children.show();
                        },
                        mouseout: function () {
                            _children.hide();
                        }
                    });
                }
            })
            if (pid == null && opts.type == 1) {
                _ul.addClass('horizontal');
            } else {
                var _level = getLevel(pid);
                _level > 0 && _ul.hide();
                _ul.addClass('vertical');
                if (_level > opts.type)
                    _ul.addClass('offset');
            }
            return _ul;
        }
        //返回下级数据数组
        function getData(pid) {
            var _data = [];
            _tempMenuData = $.grep(_tempMenuData, function (_d) {
                if (_d.pid == pid) {
                    _data.push(_d);
                    return true;
                }
                return false;
            }, true);
            return _data;
        }
        return this.each(function () {
            var me = $(this);
            me.addClass('ctcx-menu');
            if (opts.data != null && opts.data.length > 0) {
                $.merge(_tempMenuData, opts.data);
                me.append(getHtml(null));
            } else {
                me.find('.item-has-children').each(function () {
                    var self = $(this);
                    var _ul = self.children('ul');
                    _ul.hide();
                    self.bind({
                        mouseover: function () {
                            _ul.show();
                        },
                        mouseout: function () {
                            _ul.hide();
                        }
                    });
                });
            }
        });
    }
    //设置默认参数
    $.fn.menu.defaults = {
        type: 1, //菜单的显示方式(主要是指第一级是横向还是纵向,默认横向1,纵向0)
        /*
        data:动态生成菜单的数组数据,如果指定的此数据则会以此数据填充菜单(菜单内原有数据被替代)
        数据格式:[menu,menu,...]
        menu对象格式:{ id: 1, pid: null, n: '菜单名称1', url: '#', fn:回调函数 }
        */
        data: null
    }
})(jQuery);


调用JS代码

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdjzwz.html