DEV Community

Cover image for How to Use Swiper JS in Vue Vite
kareem_sulaimon
kareem_sulaimon

Posted on

20

How to Use Swiper JS in Vue Vite

Vue.js is a popular JavaScript framework for building web applications, and Vite is a lightweight development server that makes it easy to get started with Vue.js projects
Swiper is a popular JavaScript library for building touch-enabled, mobile-first carousels and sliders. swiper document here. For more in depth info, click the the below

https://swiperjs.com/vue

Create Vue Vite Application

Image description
In this section we will introduce how to scaffold a Vue Single Page Application on your local machine. The created project will be using a build setup based on Vite and allow us to use Vue Single-File Components (SFCs)



#npm init vue@latest


Enter fullscreen mode Exit fullscreen mode

You will be presented with prompts for several optional features such as TypeScript and testing support.
If you are unsure about an option, simply choose NO by hitting enter for now.
Then follow the instructions.



cd <your-project-name>
npm install
npm run dev


Enter fullscreen mode Exit fullscreen mode

Installing Swiper

Before we can start using Swiper, we need to install it in our project. 
We can install Swiper from NPM



$ npm install swiper

 // import Swiper JS


Enter fullscreen mode Exit fullscreen mode

Setup

Now that you have the Swiper library and its Vue.js wrapper installed, you need to configure your project to use them.
You will have to import the library and the styles in your main.js file.



import {swiper, swiperSlide} from 'swiper/vue';
import swiperCore, (/* { default global options } */) from "swiper";

import "swiper/swiper.min.css";
import "swiper/css/ (/*{ default global options }*/)

swiperCore.use([/* default global options */])



Enter fullscreen mode Exit fullscreen mode

Usage

In your template, you can now use the swiper component to create a slider. The swiper component accepts a number of props that allow you to customize the slider, such as options and slidesPerView.



<div id="app">
    <swiper :options="swiperOptions" :slides-per-view="3">
      <swiper-slide>Slide 1</swiper-slide>
      <swiper-slide>Slide 2</swiper-slide>
      <swiper-slide>Slide 3</swiper-slide>
    </swiper>
  </div>
</template>


Enter fullscreen mode Exit fullscreen mode

And you can define the options in your script part as



<script>
export default {
name: 'App',
data() {
return {
swiperOptions: {
loop: true,
pagination: {
el: '.swiper-pagination',
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
autoplay: {
delay: 5000,
},
},
}
},
}
</script>

Enter fullscreen mode Exit fullscreen mode




Configuring the Slider

There are many options available for configuring the Swiper slider, including options for navigation, pagination, and autoplay. Here are a few examples of options that you can use to customize the slider:

slidesPerView: This option sets the number of slides that are visible at one time.

loop: This option enables the "loop" mode, which allows the slider to loop continuously.

Pagination: This option enables the pagination feature, which allows users to navigate through the slides by clicking on a pagination button.

autoplay: This option enables the autoplay feature, which automatically advances the slides at a set interval.

speed: This option sets the duration of the transition between slides.

You can check more here
https://swiperjs.com/vue

AWS Q Developer image

Your AI Code Assistant

Automate your code reviews. Catch bugs before your coworkers. Fix security issues in your code. Built to handle large projects, Amazon Q Developer works alongside you from idea to production code.

Get started free in your IDE

Top comments (0)

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post

Instrument, monitor, fix: a hands-on debugging session

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️