Logo

dev-resources.site

for different kinds of informations.

A step-by-step guide to creating a beautiful slider with Swiper and React

Published at
2/11/2023
Categories
react
slider
swiper
Author
amiabl_programr
Categories
3 categories in total
react
open
slider
open
swiper
open
Author
15 person written this
amiabl_programr
open
A step-by-step guide to creating a beautiful slider with Swiper and React

When a visitor first arrives at your website, sliders are a fantastic way to showcase your greatest work, especially if photography, fashion, or nature are your main features.
Building a beautiful slider from scratch with javascript can be lengthy and time-consuming, but instead of creating a slider(or carousel) from scratch, we can leverage Swiper to bootstrap the process for us. In this article, we will discuss how we can easily set up a basic slider with Swiper.

Repository

Check out the complete source code here.

Prerequisites

Node.js must be installed on your machine before you can begin. You may download and install these tools from the Node.js website if you don't already have them.
Also, the code editor we will be using is VScode.

What is Swiper?

Swiper is the most cutting-edge free mobile touch slider with incredible native behavior and hardware-accelerated transitions. Mobile web applications, native mobile apps, and web pages can use Swiper. Swiper employs the cutting-edge flexbox style for slide layout, eliminating many issues and saving a lot of time with size calculations. Such a layout also enables the Slides grid's pure CSS configuration. A fairly robust API is included with Swiper. It allows you to make your own navigation buttons, parallax effects, and pagination, among many other things.

Swiper with React.js

Open your preferred code editor, we'll be using VScode in this tutorial. Open your terminal and enter this command to spin up a react project.

npx create-react-app swiper-and-react

Navigate to the new directory now, then launch the development server as follows:



cd swiper-and-react
npm run start


Enter fullscreen mode Exit fullscreen mode

By doing so, you'll launch the development server. Check your terminal for the port your project is assigned to, then open it in your web browser.

launch the development server

Install Swiper with npm

Next, install Swiper by running the command below:

npm i swiper

Clean up your React project, and ensure your directory resembles this:



//App.jsx
import "./App.css";
function App() {
  return <div></div>;
}
export default App;


Enter fullscreen mode Exit fullscreen mode


/* App.css*/
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}


Enter fullscreen mode Exit fullscreen mode

In your App.jsx file, import Swiper React components



// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/css";


Enter fullscreen mode Exit fullscreen mode

Update your CSS file with the default Swiper styling using the code below(this code is optional):




* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
#root {
  height: 100%;
}
html,
body {
  position: relative;
  height: 100%;
}
body {
  background: #eee;
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: #000;
  margin: 0;
  padding: 0;
}
.swiper {
  width: 100%;
  height: 100%;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  background: #fff;
  /* Center slide text vertically */
  display: -webkit-box;
  display: -ms-flexbox;
  display: -webkit-flex;
  display: flex;
  -webkit-box-pack: center;
  -ms-flex-pack: center;
  -webkit-justify-content: center;
  justify-content: center;
  -webkit-box-align: center;
  -ms-flex-align: center;
  -webkit-align-items: center;
  align-items: center;
}
.swiper-slide img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
}



Enter fullscreen mode Exit fullscreen mode

Building the Slider

Add the Swiper component.




//App.jsx
// Import Swiper React components
import { Swiper, SwiperSlide } from "swiper/react";

// Import Swiper styles
import "swiper/css";
import "./App.css";

function App() {
  return (
    <Swiper spaceBetween={50} slidesPerView={3}>
      <SwiperSlide>Slide 1</SwiperSlide>
      <SwiperSlide>Slide 2</SwiperSlide>
      <SwiperSlide>Slide 3</SwiperSlide>
      <SwiperSlide>Slide 4</SwiperSlide>
    </Swiper>
  );
}

export default App;


Enter fullscreen mode Exit fullscreen mode

Go to Unsplash to get some beautiful images for your slider and add them to your React project. In the src folder, create an image folder and put all the photos there.

Unsplash

Import the images in the App.jsx file.

folder structure




// Import Swiper React components
import React from "react";
import { Swiper, SwiperSlide } from "swiper/react";
import leaf from "../src/images/leaf.jpg";
import clouds from "../src/images/clouds.jpg";
import mountains from "../src/images/mountains.jpg";
import sunset from "../src/images/sunset.jpg";

// Import Swiper styles
import "swiper/css";
import "./App.css";

function App() {
  return (
    <Swiper spaceBetween={50} slidesPerView={3}>
      <SwiperSlide>
        <img src={leaf} alt="" />
      </SwiperSlide>
      <SwiperSlide>
        <img src={sunset} alt="" />
      </SwiperSlide>
      <SwiperSlide>
        <img src={mountains} alt="" />
      </SwiperSlide>
      <SwiperSlide>
        <img src={clouds} alt="" />
      </SwiperSlide>
    </Swiper>
  );
}

export default App;


Enter fullscreen mode Exit fullscreen mode

Your demo application will look like this when you complete it:
Demo App

Conclusion

Congratulations, you have a basic slider set up already. You can visit the documentation where you can find more examples of how you can add more features to the slider.

Resources

In this post, I covered the basic aspects of the Swiper library. You may use this resource to learn how to add extra features to your slider, such as navigation and pagination.
Swiper React Documentation

slider Article's
30 articles in total
Favicon
infinity slider
Favicon
Build A Stunning Animated Travel Image Slider Using Html, Css, And JavaScript!
Favicon
Slider in Flutter
Favicon
SLIDER ANIMATION HELP
Favicon
Making a slider puzzle in Java Script
Favicon
#LearnedToday: Slider in pure CSS
Favicon
Flutter - Triangular CAROUSEL Slider
Favicon
looking for a slider package for angular
Favicon
Creating a Slider using Next.js and CSS
Favicon
Create Dynamic Image Slider with PHP and MySQL
Favicon
A step-by-step guide to creating a beautiful slider with Swiper and React
Favicon
Animating using sliders in JavaFX and SceneBuilder
Favicon
Product Gallery Slider for WooCommerce
Favicon
What is a slider and why you should slider on your website?
Favicon
Kaydırıcı robot
Favicon
Basic JavaScript slider ready source
Favicon
Pure CSS Vertical Image Slider Using Only HTML & CSS
Favicon
How to make a simple slider component in React
Favicon
Responsive Image slider using Owl Carousel | HTML & CSS
Favicon
Simple Slider on a pure JavaScript
Favicon
Image Slideshow HTML CSS
Favicon
Ngx Slick Carousel In Angular Material Card with Custom Arrows
Favicon
How to make a Full Slider with javascript in 10 mins
Favicon
Let's build a slider from scratch in React Native
Favicon
Elegant & Beautiful Testimonial Sliders
Favicon
Choppy Slider: The Styles
Favicon
Flutter. A Quarter Round Slider.
Favicon
VueJS: Double range slider component
Favicon
Styling Range Sliders with CSS
Favicon
AWS – Using Multiple (Staging, UAT and Production) Accounts Under AWS Organization

Featured ones: