A lightweight, reusable image carousel component built with vanilla JavaScript. Part of The Odin Project curriculum.
- π― Pure JavaScript - No dependencies required
- β¬ οΈβ‘οΈ Navigation arrows - Previous/Next buttons
- β« Navigation dots - Click to jump to any slide
- β° Auto-play - Automatically advances every 5 seconds (configurable)
- π± Responsive - Works on all screen sizes
- π¨ Customizable - Easy to style and configure
- β»οΈ Reusable - Create multiple carousels on the same page
npm install @top-submissions/image-carousel<link rel="stylesheet" href="path/to/styles.css">
<script src="path/to/ImageCarousel.js"></script><!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="node_modules/@top-submissions/image-carousel/src/css/styles.css">
</head>
<body>
<div id="my-carousel"></div>
<script src="node_modules/@top-submissions/image-carousel/src/ImageCarousel.js"></script>
<script src="main.js"></script>
</body>
</html>// Import if using modules
// const ImageCarousel = require('@top-submissions/image-carousel');
// Define your images
const images = [
'path/to/image1.jpg',
'path/to/image2.jpg',
'path/to/image3.jpg',
'path/to/image4.jpg'
];
// Initialize the carousel
const carousel = new ImageCarousel(
document.getElementById('my-carousel'),
images
);const carousel = new ImageCarousel(container, images, {
autoPlayDelay: 5000, // Time between slides in ms (default: 5000)
showArrows: true, // Show prev/next arrows (default: true)
showDots: true, // Show navigation dots (default: true)
autoPlay: true // Enable auto-play (default: true)
});| Option | Type | Default | Description |
|---|---|---|---|
autoPlayDelay |
number | 5000 | Milliseconds between automatic slide transitions |
showArrows |
boolean | true | Display previous/next navigation arrows |
showDots |
boolean | true | Display navigation dots below carousel |
autoPlay |
boolean | true | Automatically advance slides |
Advance to the next slide.
carousel.next();Go back to the previous slide.
carousel.previous();Jump to a specific slide by index (0-based).
carousel.goToSlide(2); // Go to third slideStart automatic slide transitions.
carousel.startAutoPlay();Stop automatic slide transitions.
carousel.stopAutoPlay();Clean up the carousel and remove all event listeners.
carousel.destroy();The carousel uses CSS classes that you can override:
/* Container */
.carousel-container { }
/* Frame (viewport) */
.carousel-frame { }
/* Slider (moves horizontally) */
.carousel-slider { }
/* Individual slides */
.carousel-slide { }
/* Navigation arrows */
.carousel-arrow { }
.carousel-arrow-prev { }
.carousel-arrow-next { }
/* Navigation dots */
.carousel-dots { }
.carousel-dot { }
.carousel-dot.active { }const carousel = new ImageCarousel(
document.getElementById('carousel'),
['img1.jpg', 'img2.jpg', 'img3.jpg']
);const carousel = new ImageCarousel(
document.getElementById('carousel'),
images,
{ autoPlay: false }
);const carousel = new ImageCarousel(
document.getElementById('carousel'),
images,
{ autoPlayDelay: 3000 }
);const carousel = new ImageCarousel(
document.getElementById('carousel'),
images,
{
autoPlay: false,
showDots: false
}
);// First carousel
const carousel1 = new ImageCarousel(
document.getElementById('carousel1'),
['set1-img1.jpg', 'set1-img2.jpg']
);
// Second carousel
const carousel2 = new ImageCarousel(
document.getElementById('carousel2'),
['set2-img1.jpg', 'set2-img2.jpg']
);- Node.js (v14.0.0 or higher)
- npm (v6.0.0 or higher)
# Clone the repository
git clone https://github.com/top-submissions/fullstackjs-npmpackage-image-carousel.git
# Navigate to directory
cd fullstackjs-npmpackage-image-carousel
# Install dependencies
npm install# Lint code
npm run lint
# Fix linting issues
npm run lint:fix
# Format code
npm run format
# Check formatting
npm run format:check-
Create npm account (if you don't have one):
npm adduser
-
Login to npm:
npm login
-
Verify you're part of the @top-submissions organization on npmjs.com
-
Update version in
package.json:{ "version": "1.0.1" } -
Commit changes:
git add . git commit -m "Release v1.0.1" git tag v1.0.1 git push origin main --tags
-
Publish to npm:
npm publish --access public
Note: The
--access publicflag is required for scoped packages to be public.
Follow semantic versioning:
- Patch (1.0.x): Bug fixes
- Minor (1.x.0): New features (backward compatible)
- Major (x.0.0): Breaking changes
- Chrome (latest 2 versions)
- Firefox (latest 2 versions)
- Safari (latest 2 versions)
- Edge (latest 2 versions)
ISC License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
Found a bug? Please report it on the GitHub Issues page.
- Built as part of The Odin Project curriculum
- Project: Dynamic User Interface Interactions - Image Carousel
- GitHub: @top-submissions
- Project Link: https://github.com/top-submissions/fullstackjs-npmpackage-image-carousel
Made with β€οΈ as part of The Odin Project