Open In App

How to design mobile touch slider using Swiper.js Plugin ?

In this article, we will learn how to design mobile touch sliders for mobile apps, mobile websites, or web apps using the JavaScript Swiper plugin. 

Approach:



CDN links:

<link rel=”stylesheet” href=”https://unpkg.com/swiper@7/swiper-bundle.min.css” />
<script src=”http://code.jquery.com/jquery-1.9.1.min.js”> </script>
<script src=”https://unpkg.com/swiper@7/swiper-bundle.min.js”></script>



Example 1: The following example demonstrates a basic slider using the Swiper plugin. Other attributes of the plugin can also be set as per the need. The page is designed with HTML div elements with the plugin’s classes namely “swiper-container”, “swiper-wrapper”, “swiper-slide”, “swiper-pagination” and other classes. Use the HTML img element for inserting various images for the slider. The slider is initialized in the script part of the below code with options setting.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
    <title>Implementing Swiper</title>
  
    <!-- Link Swiper's CSS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
  
    <!-- Styles -->
    <style>
        html,
        body {
            position: relative;
            height: 100%;
        }
  
        body {
            font-family: Helvetica Neue;
            color: #000;
            margin: 0;
            padding: 0;
        }
  
        .swiper-container {
            width: 100%;
            height: 100%;
        }
  
        .swiper-slide {
            background: #fff;
            display: -ms-flexbox;
            display: -webkit-flex;
            -ms-flex-pack: center;
            -webkit-justify-content: center;
            justify-content: center;
            -webkit-box-align: center;
            -ms-flex-align: center;
            -webkit-align-items: center;
        }
    </style>
</head>
  
<body>
    <!-- Swiper -->
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">
                <img src=
            </div>
            <div class="swiper-slide">
                <img src=
            </div>
            <div class="swiper-slide">
                <img src=
            </div>
            <div class="swiper-slide">
                <img src=
            </div>
        </div>
  
        <div class="swiper-pagination"></div>
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
        <div class="swiper-scrollbar"></div>
    </div>
  
    <!-- Initialize Swiper -->
    <script>
        var swiper = new Swiper('.swiper-container', {
  
            pagination: {
                el: '.swiper-pagination',
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
            scrollbar: {
                el: '.swiper-scrollbar',
            }
        });
    </script>
</body>
  
</html>

Output:

Example 2: In the following example, we just change or adjust the options values for the attributes in the script part of the code for the above HTML code. The developer can change or add according to the application’s need. Two slides are visible at one time with a progress bar in the top and bottom of the slider.




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
    <title>Implementing Swiper</title>
  
    <!-- Link Swiper's CSS -->
    <link rel="stylesheet" href=
    <script src=
    </script>
    <script src=
    </script>
  
    <!-- Styles -->
    <style>
        html,
        body {
            position: relative;
            height: 100%;
        }
  
        body {
            font-family: Helvetica Neue;
            color: #000;
            margin: 0;
            padding: 0;
        }
  
        .swiper-container {
            width: 100%;
            height: 100%;
        }
  
        .swiper-slide {
            background: #fff;
            display: -ms-flexbox;
            display: -webkit-flex;
            -ms-flex-pack: center;
            -webkit-justify-content: center;
            justify-content: center;
            -webkit-box-align: center;
            -ms-flex-align: center;
            -webkit-align-items: center;
        }
    </style>
</head>
  
<body>
    <!-- Swiper -->
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">
                <img src=
            </div>
            <div class="swiper-slide">
                <img src=
            </div>
            <div class="swiper-slide">
                <img src=
            </div>
            <div class="swiper-slide">
                <img src=
            </div>
        </div>
        <div class="swiper-pagination"></div>
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
        <div class="swiper-scrollbar"></div>
    </div>
  
    <!-- Initialize Swiper -->
    <script>
        var swiper = new Swiper('.swiper-container', {
            slidesPerView: 2,
            centeredSlides: true,
            pagination: {
                el: '.swiper-pagination',
                type: 'progressbar',
                slidesPerView: 'auto',
                initialSlide: 1,
                resistanceRatio: 0,
                clickable: true
            },
            navigation: {
                nextEl: '.swiper-button-next',
                prevEl: '.swiper-button-prev',
            },
            scrollbar: {
                el: '.swiper-scrollbar',
            },
            mousewheel: true,
        });
    </script>
</body>
  
</html>

Output:


Article Tags :