Skip to content

Instantly share code, notes, and snippets.

@Lavkush-Singh34
Created February 4, 2025 08:37
Show Gist options
  • Save Lavkush-Singh34/0155b5175f51ef3f2949409aba9e0a92 to your computer and use it in GitHub Desktop.
Save Lavkush-Singh34/0155b5175f51ef3f2949409aba9e0a92 to your computer and use it in GitHub Desktop.

Here’s a step-by-step guide to using Swiper.js for creating interactive sliders on your website:

Step 1: Include Swiper.js in Your Project

Option 1: CDN

You can use the CDN to include Swiper.js without installing anything.

In your HTML file, add the following links inside the <head>:

<!-- Swiper CSS -->
<link rel="stylesheet" href="https://unpkg.com/swiper/swiper-bundle.min.css" />

<!-- Swiper JS -->
<script src="https://unpkg.com/swiper/swiper-bundle.min.js"></script>

Option 2: Install via NPM (For local development)

If you are using a bundler like Webpack, you can install Swiper using npm:

npm install swiper

Then import Swiper into your JavaScript file:

import Swiper from 'swiper/bundle';
import 'swiper/swiper-bundle.min.css';

Step 2: Add HTML Structure for the Swiper

Create a simple HTML structure for the swiper component:

<!-- Swiper -->
<div class="swiper-container">
  <div class="swiper-wrapper">
    <!-- Slide 1 -->
    <div class="swiper-slide">Slide 1</div>
    <!-- Slide 2 -->
    <div class="swiper-slide">Slide 2</div>
    <!-- Slide 3 -->
    <div class="swiper-slide">Slide 3</div>
    <!-- Add more slides as needed -->
  </div>

  <!-- Add Pagination (optional) -->
  <div class="swiper-pagination"></div>

  <!-- Add Navigation (optional) -->
  <div class="swiper-button-next"></div>
  <div class="swiper-button-prev"></div>
</div>

Step 3: Initialize Swiper in JavaScript

Next, you need to initialize the Swiper in your JavaScript file:

const swiper = new Swiper('.swiper-container', {
  loop: true, // Optional: Makes the swiper loop infinitely
  slidesPerView: 1, // Number of slides visible at once
  spaceBetween: 10, // Space between slides
  pagination: {
    el: '.swiper-pagination', // Enables pagination
    clickable: true, // Makes the pagination clickable
  },
  navigation: {
    nextEl: '.swiper-button-next', // Next button
    prevEl: '.swiper-button-prev', // Previous button
  },
});

Step 4: Customize Swiper (Optional)

You can adjust the Swiper settings to suit your needs. Here are a few common configuration options:

  • loop: Enable infinite looping.
  • slidesPerView: The number of slides visible at once.
  • spaceBetween: The space between slides (in px).
  • autoplay: Automatically slide after a set interval.
  • speed: The speed of the slide transition (in ms).

Example with autoplay:

const swiper = new Swiper('.swiper-container', {
  autoplay: {
    delay: 2500, // Auto-slide every 2.5 seconds
  },
  loop: true,
  slidesPerView: 1,
});

Step 5: Style Your Swiper (Optional)

You can use CSS to customize the swiper's appearance. For example, you could give the swiper a specific width or height:

.swiper-container {
  width: 100%;
  height: 400px; /* Adjust the height as needed */
}

.swiper-slide {
  background-color: lightblue; /* Customize slide background */
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;
}

Step 6: Test Your Swiper

Once everything is set up, open your HTML file in the browser, and you should see a functioning Swiper component. The slides will move based on your configuration, and the navigation buttons or pagination should work as expected.

That’s it! You’ve successfully set up Swiper.js in your project. You can further explore Swiper’s documentation to add more features like lazy loading, effect transitions, and more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment