Pagination skips page 1 when the first carousel item exceeds the viewport width
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
- Create a carousel with a viewport width of
300px. - Add a first item with a width of
500px. - Initialize jCarousel with the pagination plugin.
- Do not specify the
perPageoption.
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.