Search code examples
jquerycountjcarousel

jQuery: Count items in carousel, maybe by width


I'm working on a flexible jCarousel that needs a number to know how many items it will scroll. But because the carousel is fluid/liquid width, the number is flexible too.

I guess I could do two things:

Count items: Is it possible to count items that are visible? There is a lot of items, but i want the number of items that is visible.

Calculate: I guess I could get the width and devide it by the width of the items. div.width() / 75 .. and make it return a single number. But how do I get the width of a DIV (let's say 1421px) and devide it by 75 (which is the width of a item) (which will be: 18,94666666666667) but will return the nearest number??

What and how should I do it? Thank you in advance...


Solution

  • I don't know, if there's an easy way to only get the visible items. If I understand that right, you mean the items in the visible area - otherwise you could use $('.item:visible'), .item being the class name of the carousel items.

    For calculating try this:

    var carouselWidth = $('#carousel').width();  // carousel main div with id=carousel
    var numberOfItems = Math.round(carouselWidth / 75);
    

    Hope this helps!