2015-04-04 64 views
1

我有一个下拉列表的表单。此列表是动态生成的,我使用mCustomScrollbar在下拉列表中显示元素。JQuery mCustomScrollbar动态高度

mCustomScrollbar需要将高度固定为px。

<ul class=" customScroll" role="menu" aria-labelledby="dropdownMenu1" > 

           <li >1</li> 
           <li >1</li> 
          </ul> 

脚本: autoExpandScrollbar

$(".customScroll").mCustomScrollbar(); 

这些参数如果有很多li元素做工精细,但在2-3 li元素案例: 我在初始化脚本 即尝试各种参数下拉列表中有一个空白区域,因为高度超出了现有元素。

有关动态更改元素高度的任何想法。

感谢,

+0

总结所有children()元素的高度并将其设置为父级ul标记 – mohamedrias 2015-04-04 13:21:13

回答

1

我以这种方式解决了这个问题。

首先我添加了2个类。

.customScroll-auto{ height:auto ; overflow:hidden;} 
.customScroll-fixed{ height:150px; overflow:hidden;} 

然后在我的jQuery文件我检查,如果

if(array_length > myLimit) 
    add class customScroll-fixed to the ul element 
else 
    add class customScroll-auto to the ul element 

我不知道这是否是正确的approach.But它为我工作。

1

可以使用计算customScroll高度:

$(function() { 
    function getChildrenHeight(element) { 
     var height = 0; 
     element.children().each(function() {height+= $(this).height();}); 
     return height; 
    } 

    $(".customScroll").height(getChildrenHeight($(".customScroll"))); 
}); 

可以使用功能getChildrenheight()功能与任何元素,以获得元素的基础上的所有子总和的高度高度。

+0

感谢@mohamedrias的回复。我想这种方法可行,但我采用了不同的方法。 – 2015-04-04 14:23:28

+0

是的,两者都可以。很高兴知道:) – mohamedrias 2015-04-04 14:28:05