将水平下拉转换为垂直下拉

时间:2014-01-02 21:38:15

标签: java html css drop-down-menu

我正在将水平下拉转换为垂直下拉。问题是我看起来像是一个下拉,但是第一个下面的li不会“激活”我甚至不能对它们产生悬停效果。我拥有的CSS如下

不应该忘记它是一个java下拉所以我现在已经添加了java代码

Iv在这上花了几个小时,似乎无法找到解决方案。任何见解将不胜感激。

    #menu {
    position:absolute;
    display:block;
    padding: 0 5px 0 5px;
    width: 225px;
    height:400px;
    font-size: 15px;
    font-size: 15px;
        font-style: normal;
        font-weight: lighter;
    margin:0px;
    padding-right:10px;
        text-align: right;
        text-decoration: none;
    font-family: "Palatino Linotype","Book Antiqua",Palatino,serif;
    }

#menu {
    text-transform: uppercase;
    letter-spacing: -1px;
    padding:0px;
    color: #333333;
}

#menu a {
    color: #333333;
    text-decoration: none;
}

#menu ul {
    list-style: none;
}

#menu ul li {
    position:relative;
    display:block;
    padding: 0 20px 0 20px;
    display: inline-block;
    width: 225px;
    list-style: none outside none;
}

#menu ul li.first {
    padding-left: 0;
}

#menu ul li.active {
    position:relative;
    display:block;
    background: #346176 url(images/btn_bg.jpg) top left no-repeat;
}

#menu .arrow {
    display: inline-block;
    margin: 0 0 0 0.5em;
    position: relative;
    width: 0;
    height: 0;
    top: -3px;
    border-bottom: 1px solid #E8D3A3;
}

Java代码

(function($) {

    jQuery.fn.dropotron = function(options) {
        var settings = jQuery.extend({
            selectorParent:     jQuery(this)
        }, options);
        return jQuery.dropotron(settings);
    }

    jQuery.dropotron = function(options) {

        // Settings
            var settings = jQuery.extend({
                selectorParent:         null,               // The parent jQuery object
                menuClass:              'dropotron',        // Menu class (assigned to every UL)
                expandMode:             'hover',            // Expansion mode ("hover" or "click")
                hoverDelay:             150,                // Hover delay (in ms)
                hideDelay:              250,                // Hide delay (in ms; 0 disables)
                openerClass:            'opener',           // Opener class
                openerActiveClass:      'active',           // Active opener class
                mode:                   'slide',            // Menu mode ("instant", "fade", "slide", "zoom")
                speed:                  'fast',             // Menu speed ("fast", "slow", or ms)
                easing:                 'swing',            // Easing mode ("swing", ???)
                alignment:              'left',             // Alignment ("left", "center", "right")
                offsetX:                0,                  // Submenu offset X
                offsetY:                0,                  // Submenu offset Y
                baseZIndex:             1                   // Base Z-Index
            }, options);

        // Variables
            var _top = settings.selectorParent, _menus = _top.find('ul');
            var _window = jQuery('html');
            var isLocked = false, hoverTimeoutId = null, hideTimeoutId = null;

        // Main
            if (settings.hideDelay > 0)
            {
                _menus.add(_top.children('li').has('ul'))
                    .mousemove(function(e) {
                        window.clearTimeout(hideTimeoutId);
                        hideTimeoutId = null;
                    })
                    .mouseleave(function(e) {
                        if (hideTimeoutId == null)
                            hideTimeoutId = window.setTimeout(function() {
                                _top.trigger('doCollapseAll');
                            }, settings.hideDelay);
                    });
            }

            _top
                .bind('doCollapseAll', function() {
                    _menus
                        .trigger('doCollapse');
                });

            _menus.each(function() {
                var menu = jQuery(this), opener = menu.parent();

                menu
                    .hide()
                    .addClass(settings.menuClass)
                    .css('position', 'absolute')
                    .bind('doExpand', function() {

                        if (menu.is(':visible'))
                            return false;

                        window.clearTimeout(hideTimeoutId);

                        _menus.each(function() {
                            var t = jQuery(this);
                            if (!jQuery.contains(t.get(0), opener.get(0)))
                                t.trigger('doCollapse');
                        });

                        var left, top, isTL = (menu.css('z-index') == 1), oo = opener.offset(), op = opener.position(), opp = opener.parent().position(), ow = opener.outerWidth(), mw = menu.outerWidth();

                        if (isTL)
                        {
                            top = oo.top + opener.outerHeight();

                            switch (settings.alignment)
                            {
                                case 'right':
                                    left = oo.left - mw + ow;

                                    if (left < 0)
                                        left = oo.left;

                                    break;

                                case 'center':
                                    left = oo.left - Math.floor((mw - ow) / 2);

                                    if (left < 0)
                                        left = oo.left;
                                    else if (left + mw > _window.width())
                                        left = oo.left - mw + ow;

                                    break;

                                case 'left':
                                default:
                                    left = oo.left;

                                    if (left + mw > _window.width())
                                        left = oo.left - mw + ow;

                                    break;
                            }
                        }
                        else
                        {
                            // Non-static position fix
                                if (opener.css('position') == 'relative'
                                ||  opener.css('position') == 'absolute')
                                {
                                    top = settings.offsetY;
                                    left = (-1 * op.left);
                                }
                                else
                                {
                                    top = op.top + settings.offsetY;
                                    left = 0;
                                }

                            switch (settings.alignment)
                            {
                                case 'right':
                                    left += (-1 * opener.parent().outerWidth()) + settings.offsetX;

                                    break;

                                case 'center':
                                case 'left':
                                default:
                                    left += opener.parent().outerWidth() + settings.offsetX;

                                    break;
                            }
                        }

                        menu
                            .css('left', left + 'px')
                            .css('top', top + 'px');

                        menu.css('opacity', '0.01').show();

                        // Kludge!
                            var tmp = false;

                            // Non-static position fix
                                if (opener.css('position') == 'relative'
                                ||  opener.css('position') == 'absolute')
                                    left = (-1 * op.left);
                                else
                                    left = 0;

                            if (menu.offset().left < 0)
                            {
                                left += opener.parent().outerWidth() - settings.offsetX;
                                tmp = true;
                            }
                            else if (menu.offset().left + mw > _window.width())
                            {
                                left += (-1 * opener.parent().outerWidth()) - settings.offsetX;
                                tmp = true;
                            }

                            if (tmp)
                                menu.css('left', left + 'px');

                            menu.hide().css('opacity', '1');

                        switch (settings.mode)
                        {
                            case 'zoom':

                                isLocked = true;

                                opener.addClass(settings.openerActiveClass);
                                menu.animate({
                                    width: 'toggle',
                                    height: 'toggle'
                                }, settings.speed, settings.easing, function() {
                                    isLocked = false;
                                });

                                break;

                            case 'slide':

                                isLocked = true;

                                opener.addClass(settings.openerActiveClass);
                                menu.animate({ height: 'toggle' }, settings.speed, settings.easing, function() {
                                    isLocked = false;
                                });

                                break;

                            case 'fade':

                                isLocked = true;

                                if (isTL)
                                {
                                    var tmp;

                                    if (settings.speed == 'slow')
                                        tmp = 80;
                                    else if (settings.speed == 'fast')
                                        tmp = 40;
                                    else
                                        tmp = Math.floor(settings.speed / 2);

                                    opener.fadeTo(tmp, 0.01, function() {
                                        opener.addClass(settings.openerActiveClass);
                                        opener.fadeTo(settings.speed, 1);
                                        menu.fadeIn(settings.speed, function() {
                                            isLocked = false;
                                        });
                                    });
                                }
                                else
                                {
                                    opener.addClass(settings.openerActiveClass);
                                    opener.fadeTo(settings.speed, 1);
                                    menu.fadeIn(settings.speed, function() {
                                        isLocked = false;
                                    });
                                }

                                break;

                            case 'instant':
                            default:

                                opener.addClass(settings.openerActiveClass);
                                menu.show();

                                break;
                        }

                        return false;
                    })
                    .bind('doCollapse', function() {

                        if (!menu.is(':visible'))
                            return false;

                        menu.hide();
                        opener.removeClass(settings.openerActiveClass);
                        menu.find('.' + settings.openerActiveClass).removeClass(settings.openerActiveClass);
                        menu.find('ul').hide();

                        return false;

                    })
                    .bind('doToggle', function(e) {

                        if (menu.is(':visible'))
                            menu.trigger('doCollapse');
                        else
                            menu.trigger('doExpand');

                        return false;

                    });

                opener
                    .addClass('opener')
                    .css('cursor', 'pointer')
                    .click(function(e) {

                        if (isLocked)
                            return;

                        e.stopPropagation();
                        menu.trigger('doToggle');

                    });

                if (settings.expandMode == 'hover')
                    opener.hover(function(e) {
                            if (isLocked)   
                                return;
                            hoverTimeoutId = window.setTimeout(function() {
                                menu.trigger('doExpand');
                            }, settings.hoverDelay);
                        },
                        function (e) {
                            window.clearTimeout(hoverTimeoutId);
                        }
                    );
            });

            _menus.find('a')
                .css('display', 'block')
                .click(function(e) {

                    if (isLocked)
                        return;

                    _top.trigger('doCollapseAll');

                    e.stopPropagation();

                });

            _top.find('li')
                .css('white-space', 'break-word')
                .each(function() {
                    var t = jQuery(this), a = t.children('a'), ul = t.children('ul');
                    if (a.length > 0 && ul.length == 0)
                        t.click(function(e) {

                            if (isLocked)
                                return;

                            _top.trigger('doCollapseAll');

                            e.stopPropagation();

                            window.location.href = a.attr('href');

                        });
                });

            _top.children('li').each(function() {

                var opener = jQuery(this), menu = opener.children('ul');

                if (menu.length > 0)
                {
                    menu
                        .detach()
                        .appendTo('body');

                    for(var z = settings.baseZIndex, i = 1, y = menu; y.length > 0; i++)
                    {
                        y.css('z-index', z++);
                        y = y.find('> li > ul');
                    }
                }
            });

this is how the menu looks. the top one (farmers) works but none of the rest do


解决

出于某种原因,当要显示第二级菜单项时,它可以正常工作。值得庆幸的是,我的所有菜单项都有第二级,我不知道为什么会出现这种情况,但它现在有效。

2 个答案:

答案 0 :(得分:1)

浮动左边的列表项应该可以解决这个问题。这将水平对齐列表项。如果你发布了一个jsfiddle,我们可以通过一些游戏来确认。

答案 1 :(得分:0)

小提琴会很好。

另外,这里:

#menu ul li {
position:relative;
display:block;
padding: 0 20px 0 20px;
display: inline-block;
width: 225px;
list-style: none outside none;
}

你有显示:inline-block(导致它显示菜单水平)和display:block(导致它显示为普通列表。)选择一个..;)