简体   繁体   English

将所有溢出div的元素水平移动到另一个

[英]Move all elements which overflow a div horizontally into another

I have a "menu bar" (for lack of a better term) which is basically a fixed width ul at 980px. 我有一个“菜单栏”(由于缺乏更好的用语),基本上是980px的固定宽度ul The li elements are populated on page load, and contain an image, a span, and a divider (for the next li ). li元素在页面加载时填充,并包含一个图像,一个跨度和一个分隔符(对于下一个li )。

If there are too many li elements, they overflow onto the next line. 如果li元素太多,它们会溢出到下一行。 I want to prepare for such an event by running some JS/jquery after the list is populated to move all of the overflow elements into another div, which will essentially be a css hover menu with the elements inside it (it will say '...view more' or similar). 我想通过在列表填充后将所有溢出元素移动到另一个div中来运行一些JS / jquery来为此类事件做准备,这实际上是一个css悬停菜单,其中包含元素(它会说'.。查看更多”或类似内容。

My initial approach has been to check, after populating the menu, if the menu's total width exceeds a certain number, and if so, to append the last element to the new div for overflow. 我的最初方法是在填充菜单后检查菜单的总宽度是否超​​过特定数目,如果是,则将最后一个元素附加到新的div上以防溢出。 This function would either loop with a variable checking the width of the menu, or call itself recursively, whichever. 该函数将与检查菜单宽度的变量一起循环,或者以递归方式调用自身(以两者中的任意一个为准)。 I got the basic functionality working with an if statement, but it was a single iteration to test - the last element got moved to the div successfully. 我获得了使用if语句使用的基本功能,但它仅需测试一次即可进行迭代-最后一个元素已成功移至div。 Unfortunately, when I tried to iterate it (loop or recursive) the browser seemed to go into an endless loop in the js and never loaded the page properly. 不幸的是,当我尝试对其进行迭代(循环或递归)时,浏览器似乎陷入了js的无限循环,并且从未正确加载页面。

A version of my current attempt looks like this: 我当前尝试的一个版本如下所示:

    /**code for populating etc happens here ... then calls trimList(); */
    function trimList(){
       var menuWidth = $('#menu').width();
       if(menuWidth > 700){
          $('#menu li:last-child').appendTo('#overflowList');
          trimList();
       }
    }

With this version or with a while loop the browser eventually crashes, but I'm not sure why. 使用此版本或while循环,浏览器最终会崩溃,但是我不确定为什么。 I'm sure it's something obvious. 我敢肯定这很明显。

I'd like to either fix my current implementation, or do it some other way (perhaps - if width is longer, grab ALL elements that push beyond that width, and throw them in the div. All at once, rather than iterating. But I'm not sure what the best way to grab the first offending (boundary pushing) element would be.) 我想修复当前的实现,或者以其他方式来实现(也许-如果宽度更长,则抓住所有超过该宽度的元素,然后将它们扔到div中。而不是反复进行。)我不确定抓住第一个违规(边界推动)元素的最佳方法是什么。)

I'm sure this is something simple, I'm just struggling to figure out what's causing the issue now. 我敢肯定这很简单,我只是在努力找出导致问题的原因。

Thanks 谢谢

edit: see below - fixed my issue 编辑:见下文-解决了我的问题

Fixed it! 修复!

The issue was that the 'overflow' div was inside the menu div and my CSS selectors targeted all descendants. 问题是'overflow'div位于菜单div中,而我的CSS选择器针对所有后代。 So, after the first iteration, every subsequent .appendTo targeted the one which had already been appended. 因此,在第一次迭代之后,每个后续的.appendTo都以已经附加的对象为目标。 The stopping condition would never be reached, and it iterated endlessly..stack overflow. 停止条件将永远不会达到,并且会不断迭代。堆栈溢出。

For clarification, my answer involved changing $('#menu li:last-child') to $('#menu > li:last-child) . 为了澄清起见,我的答案涉及将$('#menu li:last-child')更改$('#menu > li:last-child) Works great now. 现在效果很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM