【问题标题】:Has anyone seen a jQuery horizontal nav menu that will dynamically adjust its size?有没有人见过会动态调整其大小的 jQuery 水平导航菜单?
【发布时间】:2014-11-09 17:03:25
【问题描述】:

我有一个水平样式的元素列表:

选项 1 |选项 2 |选项 3 |选项 4 |更多

现在列表实际上很长(20 个选项)。我想让菜单动态调整并填写一个下拉菜单,当您使用无法在用户屏幕上显示的选项 X 突出显示更多选项时出现的下拉菜单。

我见过一些全有或全无的菜单,它们可以通过响应式布局从移动设备等的水平列表中创建一个“汉堡包”菜单......但我似乎无法找到一个将一次动态删除一个选项,并根据用户的屏幕宽度将它们填充到 MORE 菜单中。

有没有人看到过这样的事情并能指出我正确的方向?

非常感谢!

【问题讨论】:

    标签: jquery html css responsive-design navigation


    【解决方案1】:

    您可以自己轻松完成,看看这个Fiddle,或者运行下面的sn-p(整页):

    $(document).on('ready', function () {
        function getHiddenItems() {
            var isOneLine = true;
            var topPosition = false;
            var invisibleItems = $('<ul/>');
            $('.menu li').each(function () {
                var thisPosition = $(this).position();
                if (topPosition === false) {
                    topPosition = thisPosition.top;
                } else if (thisPosition.top > topPosition) {
                    invisibleItems.append($(this).clone());
                    isOneLine = false;
                };
            });
            return isOneLine ? false : invisibleItems;
        };
    
        function checkMenu() {
            $('.more').hide();
            var hiddenItems = getHiddenItems();
            if (!hiddenItems) {
    
                $('.more_menu').html('');
            } else {
                $('.more').show();
                /* What items are not visible now? */
                hiddenItems = getHiddenItems();
                $('.more_menu').html('<ul>' + hiddenItems.html() + '</ul>');
            };
        };
        $('.more').on('click', function (e) {
            e.preventDefault();
            $('.more').toggleClass('more_menu_opened');
            $('.more_menu').toggleClass('active');
        });
        $(window).on('resize', checkMenu);
        checkMenu();
    });
    * {
        margin: 0;
        padding: 0;
    }
    body {
        margin: 20px 0;
        font-family: Arial, sans-serif;
    }
    .menu {
        list-style-type: none;
        height: 35px;
        overflow: hidden;
    }
    .more_menu {
        display: none;
    }
    .more_menu.active {
        display: block;
    }
    .menu li {
        display: block;
        float: left;
        height: 35px;
        line-height: 35px;
        white-space: nowrap;
        padding: 0 10px;
    }
    .more {
        display: block;
        float: right;
        height: 35px;
        line-height: 35px;
        white-space: nowrap;
        padding: 0 10px;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <a class="more" href="#show_all">More</a>
    <ul class="menu">
        <li>Item 1</li>
        <li>Item 2</li>
        <li>Item 3</li>
        <li>Item 4</li>
        <li>Item 5</li>
        <li>Item 6</li>
    </ul>
    <br />
    <br />
    <div class="more_menu"></div>

    【讨论】:

    • 难以置信!!非常感谢
    • 没什么,只有 15 分钟的欢乐 ;)
    【解决方案2】:

    还有另一个解决方案:demo

    $(function() {
        alignMenu();
    
        $(window).resize(function() {
            $("#horizontal").append($("#horizontal li.hideshow ul").html());
            $("#horizontal li.hideshow").remove();
            alignMenu();
        });
    
        function alignMenu() {
        var w = 0;
        var mw = $("#horizontal").width() - 150;
        var i = -1;
        var menuhtml = '';
        jQuery.each($("#horizontal").children(), function() {
            i++;
            w += $(this).outerWidth(true);
            if (mw < w) {
                menuhtml += $('<div>').append($(this).clone()).html();
                $(this).remove();
            }
        });
        $("#horizontal").append(
                '<li  style="position:relative;" href="#" class="hideshow">'
                        + '<a href="#">more '
                        + '<span style="font-size:13px">&#8595;</span>'
                        + '</a><ul>' + menuhtml + '</ul></li>');
        $("#horizontal li.hideshow ul").css("top",
                $("#horizontal li.hideshow").outerHeight(true) + "px");
        $("#horizontal li.hideshow").click(function() {
            $(this).children("ul").toggle();
        });
        if (menuhtml == '') {
            $("#horizontal li.hideshow").hide();
        } else {
            $("#horizontal li.hideshow").show();
        }
    }
    });
    ul#horizontal, #horizontal ul  {
    list-style-type: none;
    margin: 0;
    padding: 0;
    }
    
    #horizontal li {
    float: left;  
    }
    #horizontal {
    height: 50px;
    float: left;
    width:100%;
    }
    #horizontal a:visited {
    font-weight: bold;
    background-color: #98bf21;
    text-align: center;
    text-decoration: none;
    text-transform: uppercase;
    }
    #horizontal a:link, div.horizontal a:visited {
    font-weight: bold;
    background-color: #98bf21;
    text-align: center;
    text-decoration: none;
    text-transform: uppercase;
    }
    #horizontal a {
    display: block;
    line-height:50px;
      padding:38px 36px;
        color: #6e6e6e;
        font: bold 12px Helvetica, Arial, sans-serif;
        text-decoration: none;
        text-shadow: 0 1px 0 #fff;
        -webkit-transition: border-color .218s;
        -moz-transition: border .218s;
        -o-transition: border-color .218s;
        transition: border-color .218s;
        background: #f3f3f3;
        background: -webkit-gradient(linear,0% 40%,0% 70%,from(#F5F5F5),to(#F1F1F1));
        background: -moz-linear-gradient(linear,0% 40%,0% 70%,from(#F5F5F5),to(#F1F1F1));
        
    }
    
    #horizontal a:hover {
    color: #333;
    border-bottom:3px solid rgb(1,202,255);
    padding-bottom:35px;
    }
    
    #horizontal li.hideshow ul{
    position:absolute;
    background: #f3f3f3;
    display:none;
    left:0px;
    }
    
    #horizontal li.hideshow
    {
    position:relative;
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <ul id="horizontal">
    <li><a href="#">item 1</a></li>
    <li><a href="#">item 2</a></li>
    <li><a href="#">item 3</a></li>
    <li><a href="#">item 4</a></li>
    <li><a href="#">item 5</a></li>
    <li><a href="#">item 6</a></li>
    
    </ul>

    【讨论】:

    • 这是一个不错的脚本,但是如果那里没有要显示的项目,您真的可以隐藏更多链接吗?
    • 现在检查...可能对你有帮助
    • 仍然不完美:虽然最后一个项目有空间,但脚本将其隐藏并显示“更多”。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多