简体   繁体   English

动态引导导航包装菜单项

[英]Dynamic bootstrap nav wrapping menu items

I have a Bootstrap 3.3.7 page with a navbar component along the top of the page. 我有一个Bootstrap 3.3.7页面,该页面顶部带有导航栏组件。 It all works perfectly when populated with static data, but when I'm trying to populate the menu with new drop down lists based on some ajax loads from the server the menu line wraps even though there is loads of horizontal space. 当使用静态数据填充时,这一切都可以完美地工作,但是当我尝试根据服务器上的一些ajax负载使用新的下拉列表填充菜单时,即使有很多水平空间,菜单行也会自动换行。

I assume it is setting some fixed width based on the initial menu, and then failing to cope with the dynamic widening. 我假设它是根据初始菜单设置一些固定宽度,然后无法应对动态扩展。 If I manually narrow the page the point the nav collapses, and then widen it again then the reflow catches up and it all ends up back on one line. 如果我手动缩小页面的位置,导航栏将折叠,然后再次加宽,然后回流将赶上,所有结果都将返回一行。

Is there any way to force the Bootstrap nav to reflow programatically? 有什么方法可以强制Bootstrap导航程序以编程方式重排?

The specific element I need to make wider is ".navbar-right", and if I hand code CSS to make it wider after the dynamic addition then this works, but I'd rather not hard-code arbitrary constants into the code given that the menus are not constant and the font may change. 我需要加宽的特定元素是“ .navbar-right”,如果我在动态加法之后递给CSS使其更宽的代码,则此方法有效,但鉴于此,我宁愿不将任意常量硬编码到代码中菜单不是恒定的,字体可能会改变。

Answering my own question (although would appreciate any better alternatives if anyone knows any) - this seems to work OK. 回答我自己的问题(尽管任何人都知道,还有其他更好的选择)-这似乎可以正常工作。

// Perform the dynamic update
var htmlOutput = $.render.mytemplate(params);
$("#mynavtemplate").replaceWith(htmlOutput);

// Measure the total width of the children of .navbar-right
var newWidth = 0;
$(".navbar-right").children().each(function(){ newWidth += $(this).width(); });

// Force .navbar-right to be at least as wide as the sum of children
$(".navbar-right").css("min-width", newWidth);

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

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