adding more button for list in responsive navigation

前端 未结 4 1111
北恋
北恋 2021-01-03 13:52

I have a navigation of lets say 12 items, and when resolution gets smaller items drop in a new line. I need to make that when an item doesn\'t fit on a navigation anymore it

4条回答
  •  独厮守ぢ
    2021-01-03 14:03

    If you have fixed-width list-items, then it is simple to collect extra list-items and push them into a separate list. Here is a simple example. Explanation is in the code comments.

    View the snippet in full-screen and try changing the window width.

    Also a Fiddle: http://jsfiddle.net/abhitalks/860LzgLL/

    Full Screen: http://jsfiddle.net/abhitalks/860LzgLL/embedded/result/

    Snippet:

    var elemWidth, fitCount, fixedWidth = 120,  
        $menu = $("ul#menu"), $collectedSet;
    
    // Assuming that the list-items are of fixed-width.
    
    collect();
    $(window).resize(collect);
    
    function collect() {
        // Get the container width
        elemWidth = $menu.width();
      
        // Calculate how many list-items can be accomodated in that width
        fitCount = Math.floor(elemWidth / fixedWidth) - 1; 
      
        // Create a new set of list-items more than the fit count
        $collectedSet = $menu.children(":gt(" + fitCount + ")");
      
        // Empty the collection submenu and add the cloned collection set
        $("#submenu").empty().append($collectedSet.clone());    
    }
    * { box-sizing: border-box; margin: 0; padding: 0; }
    div { position: relative; background-color: #ccc; height: 32px; overflow: visible; }
    ul#menu, ol { height: 32px; max-width: 80%; overflow: hidden; }
    ul#menu > li, ol > li { display: block; float: left;  height: 32px; width: 120px; padding: 4px 8px; }
    ol { position: absolute; right: 0; top: 0; overflow: visible; }
    ol > li { min-width: 120px; }
    ol ul { position: absolute; top: 120%; right: 10%; }
    ol li ul > li { list-style: none; background-color: #eee; border: 1px solid gray; padding: 4px 8px;}
    
    
    1. Collected


    Update:

    This is regarding your query on differing / variable widths of list-items. There would be a minor change.

    Also a Fiddle: http://jsfiddle.net/abhitalks/tkbmcupt/1/

    Full Screen: http://jsfiddle.net/abhitalks/tkbmcupt/1/embedded/result/

    Snippet:

    var elemWidth, fitCount, varWidth = 0, ctr, $menu = $("ul#menu"), $collectedSet;
    
    // Get static values here first
    ctr = $menu.children().length;         // number of children will not change
    $menu.children().each(function() {
        varWidth += $(this).outerWidth();  // widths will not change, so just a total
    });
    
    collect();  // fire first collection on page load
    $(window).resize(collect); // fire collection on window resize
    
    function collect() {
        elemWidth = $menu.width();  // width of menu 
      
        // Calculate fitCount on the total width this time
        fitCount = Math.floor((elemWidth / varWidth) * ctr) - 1;
        
        // Reset display and width on all list-items
        $menu.children().css({"display": "block", "width": "auto"});
      
        // Make a set of collected list-items based on fitCount
        $collectedSet = $menu.children(":gt(" + fitCount + ")");
      
        // Empty the more menu and add the collected items
        $("#submenu").empty().append($collectedSet.clone());  
      
        // Set display to none and width to 0 on collection,
        // because they are not visible anyway.
        $collectedSet.css({"display": "none", "width": "0"});
    }
    * { box-sizing: border-box; margin: 0; padding: 0; }
    div { position: relative; background-color: #ccc; height: 32px; overflow: visible; }
    ul#menu, ol { height: 32px; max-width: 80%; overflow: hidden; }
    ul#menu > li, ol > li { display: block; float: left; height: 32px; white-space: nowrap; padding: 4px 8px; }
    ol { position: absolute; right: 0; top: 0; overflow: visible; }
    ol > li { min-width: 120px; }
    ol ul { position: absolute; top: 120%; right: 10%; }
    ol li ul > li { list-style: none; background-color: #eee; border: 1px solid gray; padding: 4px 8px;}
    
    
    1. Collected

提交回复
热议问题