To create a multi-item carousel that moves one card at a time instead of moving an entire "slide" (in this case, all three cards), you'll need to add some custom JavaScript logic that interacts with Bootstrap's carousel component. Below is an updated version of the previous code with JavaScript to achieve the desired functionality:
Updated HTML Structure with JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Multi-Item Card Carousel</title>
<!-- Bootstrap CSS -->
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/css/bootstrap.min.css" rel="stylesheet">
<style>
.carousel-item {
max-width: 100%;
display: flex; /* Use flexbox for layout */
}
.card {
margin: 10px; /* Add some margin around cards */
}
.carousel-inner {
display: flex; /* Keep the items in a row */
transition: transform 0.5s ease; /* Smooth transition */
}
</style>
</head>
<body>
<div id="multiCardCarousel" class="carousel slide" data-bs-ride="false">
<div class="carousel-inner">
<div class="carousel-item active">
<div class="row">
<div class="col-md-4">
<div class="card">
<img src="https://via.placeholder.com/300" class="card-img-top" alt="Card 1">
<div class="card-body">
<h5 class="card-title">Card 1</h5>
<p class="card-text">Some quick example text to build on Card 1.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<img src="https://via.placeholder.com/300" class="card-img-top" alt="Card 2">
<div class="card-body">
<h5 class="card-title">Card 2</h5>
<p class="card-text">Some quick example text to build on Card 2.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<img src="https://via.placeholder.com/300" class="card-img-top" alt="Card 3">
<div class="card-body">
<h5 class="card-title">Card 3</h5>
<p class="card-text">Some quick example text to build on Card 3.</p>
</div>
</div>
</div>
</div>
</div>
<div class="carousel-item">
<div class="row">
<div class="col-md-4">
<div class="card">
<img src="https://via.placeholder.com/300" class="card-img-top" alt="Card 4">
<div class="card-body">
<h5 class="card-title">Card 4</h5>
<p class="card-text">Some quick example text to build on Card 4.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<img src="https://via.placeholder.com/300" class="card-img-top" alt="Card 5">
<div class="card-body">
<h5 class="card-title">Card 5</h5>
<p class="card-text">Some quick example text to build on Card 5.</p>
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<img src="https://via.placeholder.com/300" class="card-img-top" alt="Card 6">
<div class="card-body">
<h5 class="card-title">Card 6</h5>
<p class="card-text">Some quick example text to build on Card 6.</p>
</div>
</div>
</div>
</div>
</div>
</div>
<button class="carousel-control-prev" type="button" id="prevBtn">
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="visually-hidden">Previous</span>
</button>
<button class="carousel-control-next" type="button" id="nextBtn">
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="visually-hidden">Next</span>
</button>
</div>
<!-- Bootstrap JS Bundle with Popper -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.3.3/js/bootstrap.bundle.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
const items = document.querySelectorAll('.carousel-item');
const nextButton = document.getElementById('nextBtn');
const prevButton = document.getElementById('prevBtn');
let currentItemIndex = 0;
function showItem(index) {
items.forEach((item, i) => {
item.style.display = i === index ? 'flex' : 'none';
});
}
nextButton.addEventListener('click', function () {
currentItemIndex = (currentItemIndex + 1) % items.length;
showItem(currentItemIndex);
});
prevButton.addEventListener('click', function () {
currentItemIndex = (currentItemIndex - 1 + items.length) % items.length;
showItem(currentItemIndex);
});
// Initialize to show the first item
showItem(currentItemIndex);
});
</script>
</body>
</html>
Explanation of the JavaScript Functionality
-
Initialization:
- We wait for the DOM content to load using
DOMContentLoaded.
- We select all carousel items and store them in the
items variable.
-
Show Item Function:
- The
showItem function takes an index and displays the corresponding item while hiding all others by changing the display style.
-
Next Button Event Listener:
- When the "Next" button is clicked, we increment the
currentItemIndex, wrapping it around using the modulus operator so it loops to the beginning when reaching the end of the items.
-
Previous Button Event Listener:
- The "Previous" button decrements the index, wrapping around to the end when going before the first item.
CSS Adjustments
- Flexbox Layout: The
flex display is used to allow easy alignment and arrangement of card elements within the carousel without relying on Bootstrap's grid system for each item directly.
This solution allows users to view one card at a time while still giving a visual grouped context with the carousel transition.