Count List Items shown on screen and not overflow

情到浓时终转凉″ 提交于 2019-12-10 05:02:38

问题


How can I count all the list items that are displayed on the screen when overflow is set to hidden?

Using the code below still counts all the items, even the ones that overflow.

   var count = $("#myList ul li:visible").length;

Fiddle:

http://jsfiddle.net/kPAwX/2/


回答1:


var maxh = $("#myList ul").height();
$("#myList ul li").filter(function () {
    return $(this).position().top + $(this).height() < maxh;
});

This will select all of the lis that are completely visible. If an li is partially cut off, it will be filtered.

If you want even partially visible lis to not be filtered, simply remove the addition of the height (or create your own cut off any way you want).

http://jsfiddle.net/ExplosionPIlls/z6GXA/



来源:https://stackoverflow.com/questions/14742674/count-list-items-shown-on-screen-and-not-overflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!