Working with Swiper.js? Here's What You Need to Know

Installation Methods:

npm install swiper

Basic Stucture

<template>
  <swiper
    :slides-per-view="3"
    :space-between="50"
    @swiper="onSwiper"
    @slideChange="onSlideChange"
  >
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
  </swiper>
</template>

Core Configuration Options

Discuss frequently used options:

  • loop: Enables continuous sliding.

  • slidesPerView: Number of visible slides at a time.

  • spaceBetween: Spacing between slides.

  •   <template>
        <swiper
            loop: true,
            slides-perView: 3,
            space-between: 10,
        >
          <swiper-slide>Slide 1</swiper-slide>
          <swiper-slide>Slide 2</swiper-slide>
          <swiper-slide>Slide 3</swiper-slide>
        </swiper>
      </template>
    

    Responsive Design with Breakpoints

    breakpoints: { 640: { slidesPerView: 1 }, 768: { slidesPerView: 2 }, 1024: { slidesPerView: 3 }, }

      <Swiper
                  :pagination="{ clickable: true }"
                  :space-between="30"
                  class="dynamic-class"
                  :modules="[Autoplay, Navigation, Pagination]"
                  :navigation="{ prevEl: '.prev', nextEl: '.next' }"
                  :slides-per-view="3"
                  style="--swiper-pagination-bullet-size: 12px"
                  :breakpoints="{
                    100: {
                      slidesPerView: 1,
                      spaceBetween: 12
                    },
                    768: {
                      slidesPerView: 2,
                      spaceBetween: 12
                    }
                  }"
                >
          <swiper-slide>Slide 1</swiper-slide>
          <swiper-slide>Slide 2</swiper-slide>
          <swiper-slide>Slide 3</swiper-slide>
      </Swiper>
    

    Enhancements with Modules

      import { Navigation, Pagination } from 'swiper/modules';
    

    Advanced Use Cases

    • Dynamic Content Loading: Fetch slides dynamically using APIs.

    • Custom Animations: Implement custom effects using CSS or JavaScript.

    • Nested Swipers: How to handle multiple Swipers within a page.

Custom Navigation

    <Swiper
                :modules="[Autoplay, Navigation, Pagination]"
                :navigation="{ prevEl: '.prev', nextEl: '.next' }" // this can controll navigation
    >    
                <SwiperSlider>
                    1
                </SwiperSlider>
                <SwiperSlider>
                    2
                </SwiperSlider>
                <SwiperSlider>
                    3
                </SwiperSlider>            
    </Swiper>

    // outside of the swiper
    <button class="next">Next</button>
    <button class="prev">Previous</button>

in navigation there are properties prevEl and NextEl you can assign class names by the class name if you click the button it will custom control on the slider.

  • Note: If you have two sliders on the same page you should change the class name differently for that way it won’t create a navigation problem otherwise which slider has fewer children it will work for it.

    Example

  • ```typescript // slider 1


    1 2 3
    // slider 2
    2.1 2.2 2.3
    2.4

// outside of the swiper 1 Next Previous

// outside of the swiper 2 Next Previous


    ***<mark>Note: If you see the code closely both of the button class has diff name otherwise it will create a problem with navigation</mark>***


### **Classes will help you to add custom style**

```typescript
--swiper-pagination-bullet-size: 12px
  <Swiper
            style="--swiper-pagination-bullet-size: 12px"
    >
          </Swiper>

There are a lot of classes that you can access over the Swiper component

Thank you

0
Subscribe to my newsletter

Read articles from Md Abdul Ahad Linkon directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Md Abdul Ahad Linkon
Md Abdul Ahad Linkon

Md Abdul Ahad Linkon Software Engineer | Researcher in Machine Learning Im a dedicated software engineer from Bangladesh, specializing in the MERN stack and proficient in both front-end and back-end development. With a BSc in Computer Science and Engineering (CSE) from IUBAT - International University of Business Agriculture and Technology, I have a solid foundation in software engineering principles. Their expertise extends to database management with experience in SQL and NoSQL. Currently, I'm conducting research on Convolutional Neural Networks (CNNs) for drought detection, exploring innovative machine-learning techniques to address environmental challenges. Combining practical software development skills with advanced research in machine learning, Linkon is committed to leveraging technology for impactful solutions.