Complexlity
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    1
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Building Mobile Touch Slider using SwiperJs Building sliders, as complex as it is, gets more difficult when you factor in mobile compatibility due to the wide difference in the touch mechanics of mobile devices compared to keyboard and mouse click style on desktops. This is why the `SwiperJs` package is often the go-to for all and any type of sliders. *Swiper is the most modern free mobile touch slider with hardware accelerated transitions and amazing native behavior. It is intended to be used in mobile websites, mobile web apps, and mobile native/hybrid apps*. - [SwiperJs Official](https://swiperjs.com/) It focuses on bridging the gap and making them feel as native as possible on mobile as it is on desktops. It also has so many out-of-the-box features which would be very handy to use than having to build them from scratch. In this article, we would explore some of those features by building a slider made of cards. In your application, you could decide to build anything with it (cards, images, buttons, etc). ## Framework Compatibility At the time of writing, the SwiperJs package is only available for the following: - **Vanilla Javascript**: It could be deployed in plain Javascript applications. For this tutorial, we would use this method. - **React**: Having its own JSX components, it could be easily integrated into the React Framework. - **Vue**: Comes with custom Vue components which support all core functionalities. - **Web Components**: Perhaps the most uncommon of all, it could be used as native HTML components. ## Pre-requisite To follow this tutorial, you need to understand the basics of the following: - HTML - CSS - Javascript ## Building A Simple Card Slider To get started, run the following commands on your terminal. ```bash= git clone https://github.com/Complexlity/slider-starter.git cd slider-starter ``` Open the folder in a text editor. You should see the structure below. ![](https://i.imgur.com/5nQF8av.png) The Output: ![](https://i.imgur.com/lxWHT0Z.png) ## Install SwiperJs There are a few ways to get started. We would use the CDN method here but you could as well install via [npm](https://www.npmjs.com/package/swiper) or download the source code from [jsdelivr](https://www.jsdelivr.com/package/npm/swiper?nav=config) and add it to your project files. Add the `Swiper CSS` to the `head` (below the title tag ) of `index.html` and `Swiper Javascript` to the `body`. ```htmlembedded= <!-- Swiper CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.css"/> ``` ```htmlembedded= <!-- Swiper Javascript --> <script src="https://cdn.jsdelivr.net/npm/swiper@8/swiper-bundle.min.js"></script> ``` **Note:** Ensure `Swiper CSS` comes before `CSS` and `Swiper Javascript` comes before `Javascript`. This is needed for some functions and styling to work properly. Refer to the image below to see what it looks like. ![](https://i.imgur.com/7D4umQb.png) *Swiper CSS Added to head* ![](https://i.imgur.com/u5UnwiT.png) *Swiper Javascript Added body* To use these styles and functions, we need to add their pre-defined classes into our HTML elements. Update the `body` of `index.html` in the following ways: - Add `swiper` class to `div.container`. - Add `swiper-wrapper` class to `div.cards-wrapper`. - Add `swiper-slide` class to all `.card` elements. Refer to the image below. ![](https://i.imgur.com/INHVq6W.png) And finally, we create a new Swiper object. Update `main.js` with the code below. ```javascript= var swiper = new Swiper(".content", { slidesPerView: 3, spaceBetween: 20, grabCursor: "true", }); ``` We used the `.content` element to create the slider. `slidesPerView` determines how many are shown each time. `spaceBetween` determines the gap in the cards in pixels(px). `grabCursor` shows the hand-grabbing cursor when you hover over the cards. ![](https://i.imgur.com/kZanBIq.gif) ## Add More SwiperJs Features Right now, the slider works but we could add more features which are supported by the package to improve the user experience. ### Navigation Navigation buttons provide a clear and intuitive way for users to control the content in the slider, enabling them to easily move forwards and backwards through the slides. To create Navigation buttons, add the elements below to bottom of `div.swiper` element. ```htmlembedded= <div class="swiper-button-next swiper-navBtn"></div> <div class="swiper-button-prev swiper-navBtn"></div> ``` ![](https://i.imgur.com/nqGYvrd.png) **Note**: `div.content` in the image above is not empty but has been collapsed. Update `main.js` by adding the following to the swiper object. ```javascript= navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev", } ``` Refer to the image below ![](https://i.imgur.com/zfxJF2l.png) Also, we could style the navigation button differently. Add the following code to the bottom of `style.css`. ```css= /* Swiper Navigation Styles */ .swiper-navBtn::before, .swiper-navBtn::after { font-size: 55px; } .swiper-button-prev { color:red } .swiper-button-next { color:green; } /* Remove navigation button on smaller screens*/ @media screen and (max-width: 800px) { .content { margin: 0 10px; } .swiper-navBtn { display: none; } } ``` ![](https://i.imgur.com/sJdvngV.gif) ### Pagination Pagination is a useful feature in sliders as it offers a clear visual representation of the user's position within the slide sequence, as well as the total number of slides. This enhances the user's ability to navigate through the slider. Add the code below just under the `navigation` markup in `index.html` ```htmlembedded= <div class="swiper-pagination"></div> ``` Refer to the image below for where to add it ![](https://i.imgur.com/4CC030k.png) Add the code below to the bottom of `style.css` to style the pagination bullets. ```css= /* Swiper Pagination Styles */ .swiper-pagination-bullet { background-color: #6e93f7; opacity: 1; } .swiper-pagination-bullet-active { background-color: #4070f4; } ``` Finally, we add the pagination property to the `Swiper` object in `main.js`. ```javascript= pagination: { el: ".swiper-pagination", clickable: true, }, ``` ![](https://i.imgur.com/nWcZsTR.png) The Output: ![](https://i.imgur.com/ZCrE2HT.png) ### Responsive Breakpoints To improve the responsiveness of our sliders, we could make a different number of items be displayed on each slider page depending on screen size.For this, we use the `breakpoints` property. Update the `Swiper` object in `main.js` by adding the `breakpoints` property and giving it our desired values. ```javascript= breakpoints: { 0: { slidesPerView: 1, }, 520: { slidesPerView: 2, }, 950: { slidesPerView: 3, }, }, ``` The `breakpoints` property accepts any screen size (in pixels) and applies the styles defined under them. The style is not limited to `slidesPerView` but could also support `slidesPerGroup`, `spaceBetween` and `grid.rows` which is also a feature of the Swiper object. Refer to the image below to see where to update the code. ![](https://i.imgur.com/TGNOP8L.png) The Output: ![](https://i.imgur.com/cLtNh37.gif) ### Autoplay This is a feature where the slider page changes without any interaction with it. To enable this feature, simply add the `autoplay` property to the swiper object in `main.js`. ```javascript= autoplay: { delay: 1000, }, ``` The `delay` property takes a number in milliseconds. The autoplay objects also take some more values: `disableOnInteraction`, `pauseOnMouseEnter`, `stopOnLastSlide`, `waitOnTransition` and `reverseDirection`. All of which take boolean values (`true` or `false`). ![](https://i.imgur.com/w5YNpin.png) The Output. ![](https://i.imgur.com/grE3K9q.gif) *The slider moves automatically*. ### Infinite Loop One handy feature is making the slider loop continuously. Smoothly transitioning from the last to the first item. Update `main.js` by adding the `loop` property to the `Swiper` object. ```javascript= loop:true, ``` ![](https://i.imgur.com/h38dylw.png) The Output. ![](https://i.imgur.com/vvzK2oJ.gif) *Autoplay now runs smoothly without rounding back to the first card*. There are still so many features the `Swiper` object supports. The comprehensive API documentation can be viewed on the [Swiperjs website](https://swiperjs.com/swiper-api). **See the project working [LIVE](https://swiperjs-slider.netlify.app/)**. ## Usage On Frameworks and Web Components As stated above, the SwiperJs package works on React and Vue frameworks as well as web components. To get started on either of them. Head over to the documentation below. - [React](https://swiperjs.com/react) - [Vue](https://swiperjs.com/vue) - [Web Components](https://swiperjs.com/element) ## Conclusion Building sliders from scratch can be very tasking and in this article, we learnt how to make it easier as well as add some core features using the SwiperJs package. The SwiperJS package provides a flexible and user-friendly tool for designing sliders and carousels that may be customized. Web developers choose it because of the variety of features it offers. Its adaptability and usability make it appropriate for a range of applications, from straightforward picture sliders to intricate, interactive displays. SwiperJS delivers a dependable and uncomplicated solution for your slider needs, regardless of whether you are an experienced developer or just getting started.

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully