jcarousel icon indicating copy to clipboard operation
jcarousel copied to clipboard

Pagination skips page 1 when the first carousel item exceeds the viewport width

Open codeCraft-Ritik opened this issue 3 months ago • 0 comments

Bug: Pagination skips page 1 when the first item is wider than the viewport

Affected version: jCarousel 0.3.9

Description

The pagination plugin incorrectly skips page 1 when the first carousel item is wider than the available clipping/viewport area.

The issue appears in src/pagination.js, where the page counter is incremented before the first item is assigned to a page:

if ((wh + dim) > clip) {
    page++;
    wh = 0;
}

Since wh starts at 0, an oversized first item causes page to increment from 1 to 2 before the item is added. As a result, page 1 remains empty and the first item is assigned to page 2.

Steps to Reproduce

  1. Create a carousel with a viewport width of 300px.
  2. Add a first item with a width of 500px.
  3. Initialize jCarousel with the pagination plugin.
  4. Do not specify the perPage option.

Example

<div class="jcarousel" id="carousel">
    <ul>
        <li style="width: 500px;">First item</li>
        <li style="width: 100px;">Second item</li>
    </ul>
</div>

<div id="pagination"></div>

<script>
    $('#carousel').jcarousel();
    $('#pagination').jcarouselPagination();
</script>

Expected Behavior

The first item should be assigned to page 1, even though it is wider than the clipping area.

Pagination should therefore display:

1, 2

Actual Behavior

The first item is assigned to page 2, leaving page 1 empty.

Pagination starts at:

2

Suggested Fix

Only increment the page number when the current page already contains an item:

if (wh > 0 && (wh + dim) > clip) {
    page++;
    wh = 0;
}

The wh > 0 check prevents an oversized first item from creating an empty page before it is assigned.

Regression Test

It would also be helpful to add a regression test covering the case where the first carousel item is wider than the clipping area, ensuring that:

  • The first item is assigned to page 1.
  • No empty page is created before it.
  • Pagination numbering remains sequential.
  • Subsequent items are assigned to the appropriate page.

This should preserve the existing pagination behavior while fixing the edge case where the first item exceeds the viewport width.

codeCraft-Ritik avatar Aug 19 '26 04:08 codeCraft-Ritik