To remove the minimum constraint of 3 posts in the container and allow the carousel to adapt to any number of items, we'll need to modify the logic for calculating item widths and visible counts. Here's the updated code:
Removed Fixed Item Count: The carousel now works with any number of items (minimum of 1).
Dynamic Width Calculation: Items are sized based on the container width divided by the number of items.
Updated Logic:
createItems() generates the original items from the originalItems array
updateItemWidths() calculates widths based on the actual number of items
Removed the responsive breakpoints (1, 2, 3 items) as the carousel now adapts to the content
How it works now:
The carousel will automatically size items to fill the container width
If you have fewer items, each will take more space
If you add more items, each will adjust to fit the available space
The infinite scroll effect remains smooth and continuous
To add/remove items dynamically, you would need to:
Update the originalItems array
Call setup() again to recreate the carousel
Note: For dynamic updates, you'd need to reinitialize the entire carousel as the cloning logic is based on the initial items.
User
je veux que la largeur de la carte soir a 250px minimum sans maximum
Assistant
To ensure each card has a minimum width of 250px without a maximum width, we'll need to adjust the layout logic. Here's the updated code:
html
<body class="bg-gray-100 p-8">
<div class="max-w-4xl mx-auto">
<h1 class="text-2xl font-bold mb-6">Carousel Infini Scroll Fluide</h1>
<div class="carousel-container relative w-full border rounded-lg bg-white overflow-hidden">
<div id="carousel-track" class="carousel-track flex will-change-transform select-none">
<!-- Items will be cloned dynamically -->
</div>
</div>
</div>
</body>
<style>
.carousel-container {
overflow: hidden;
}
.carousel-track {
display: flex;
}
.carousel-item {
flex: 0 0 auto; /* Important for minimum width */
min-width: 250px; /* Minimum width for cards */
}
.card {
box-sizing: border-box; /* Ensure padding is included in width */
padding: 2rem;
border: 1px solid red;
height: 100%;
margin-right: 1rem; /* Use margin-right instead of margin-left */
}
</style>
<script>
const track = document.getElementById("carousel-track");
const originalItems = [
{ title: "Post title de l'article en cours... Post 1" },
{ title: "Post 2" },
{ title: "Post 3" },
{ title: "Post 4" },
{ title: "Post 5" }
];
let position = 0;
const speed = 0.1; // px/ms
let animationId;
let lastTimestamp = null;
let totalItems = 0;
let totalWidth = 0;
// Create original items
function createItems() {
originalItems.forEach(item => {
const carouselItem = document.createElement('div');
carouselItem.className = 'carousel-item';
carouselItem.innerHTML = `
<div class="card rounded-xl">
<p>${item.title}</p>
</div>
`;
track.appendChild(carouselItem);
});
}
function cloneItems() {
const items = Array.from(track.children);
const clones1 = items.map(item => item.cloneNode(true));
const clones2 = items.map(item => item.cloneNode(true));
clones1.forEach(clone => track.appendChild(clone));
clones2.forEach(clone => track.appendChild(clone));
}
function getItemTotalWidth(item) {
return item.offsetWidth + parseFloat(getComputedStyle(item).marginRight);
}
function updateWidths() {
const items = document.querySelectorAll(".carousel-item");
const slideWidth = getItemTotalWidth(items[0]);
totalItems = items.length;
totalWidth = totalItems * slideWidth;
}
function animate(timestamp) {
if (!lastTimestamp) lastTimestamp = timestamp;
const delta = timestamp - lastTimestamp;
lastTimestamp = timestamp;
position -= speed * delta;
if (position <= -totalWidth / 3) {
position += totalWidth / 3;
}
track.style.transform = `translateX(${position}px)`;
animationId = requestAnimationFrame(animate);
}
function setup() {
createItems();
cloneItems();
updateWidths();
position = 0;
lastTimestamp = null;
if (animationId) cancelAnimationFrame(animationId);
animationId = requestAnimationFrame(animate);
}
window.addEventListener("load", setup);
window.addEventListener("resize", () => {
updateWidths();
position = 0;
});
</script>
Key Changes:
Minimum Width on .carousel-item:
Added min-width: 250px; to ensure each card has a minimum width.
Used flex: 0 0 auto; to allow items to grow beyond the minimum width.
Margin Adjustment:
Changed margin-left to margin-right on .card to ensure consistent spacing.
Box Sizing:
Added box-sizing: border-box; to .card to include padding within the specified width.
Removed Dynamic Width Calculation:
Since items now have a minimum width, we no longer need to calculate widths based on the container. Instead, the browser will handle the layout based on available space and minimum width constraints.
How it works now:
Each card will have a minimum width of 250px.
If there's extra space in the container, cards will distribute the remaining space evenly.
The infinite scroll effect remains smooth and continuous.
Cards will shrink below their minimum width only if the container is narrower than the total minimum width of all cards.
Note: If you have fewer items and the container is wide enough, cards will be larger than 250px but will never be smaller.