我想限制列表中元素的数量。我想动态设置元素的数量。
我的第一个方法如下:
var winHeight = $(window).height() ;
var listHeight = $('.listElement').height() * $('.listElement').length ;
if ( listHeight > (winHeight * 0.66) ){
// If the list is larger than 66% of the screen, cut off those elements
// and provide buttons to flick through the list
}
它看起来不太好,我不确定这是否是正确的编码方式。
有没有更好的方法?
最佳答案
为什么你不能使用 css 解决方案而不是 jQuery?
我不太明白,但是在你的 listElement 类上,尝试这样做:
.listElement {
max-height: 300px; // put here your max height do you want
height: 200px;
overflow: auto; // when your list get the maximum height, css will put a scroolbar
}
如果 a 不明白,我很抱歉。
拥抱, 文。
关于jquery - 如何响应式地限制元素的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16037311/