Example of using swiper plugin to implement carousel in Vue

Example of using swiper plugin to implement carousel in Vue

Hello everyone, I am currently working on a project that imitates Ele.me. I will synchronize my project experience here and share it with you!

vue - Use swiper plugin to implement carousel

Download and install: npm install swiper --save

The HTML part of Msite.vue:

<!--Use swiper in the msite_nav navigation section of the page-->
<div class="swiper-container">
	<div class="swiper-wrapper">
        <div class="swiper-slide">1</div>
        <div class="swiper-slide">2</div>
        <div class="swiper-slide">3</div>
    </div>
    <!-- swiper carousel dots-->
    <div class="swiper-pagination"></div>
</div>

The script part is introduced and initialized:

<script>
import Swiper from 'swiper'
//At the same time, import swiper's css file import 'swiper/dist/css/swiper.min.css'
export default {
  //Note that swiper should be initialized after the page is loaded (mounted) mounted () {
    //Create a swiper instance to implement the carousel new Swiper('.swiper-container', {
      autoplay: true,
      // If you need pagination: {
        el: '.swiper-pagination',
        clickable: true
      }
   })
  }
}
</script>

It should be noted that when importing CSS files, the import method is different because of different versions. Otherwise, an error will be reported because the corresponding CSS file cannot be found. For example, the latest version

import 'swiper/swiper-bundle.min.css'

For specific usage, please refer to [Swiper official documentation]

One thing to note is that you need to create a swiper instance after requesting data

Use watch and $nextTick to solve the carousel bug

The paging swiper should actually be initialized after the carousel list is displayed (that is, the categories array has data).

At the beginning, categories is an empty array. The carousel list will be displayed only when there is data. To monitor the data changes of categories, watch is used.

//Create a new watch to monitor categories
watch:
    categories (value) { // There is data in the categories array // But the interface has not been updated asynchronously yet}
}
// Delete the new Swiper...code in mounted

But in fact, changing the state data in the state (categories receiving data) and asynchronously updating the interface (displaying the carousel list) are two steps. Therefore, you need to wait for a while until the interface completes the asynchronous update before you can initialize Swiper.

// Using setTimeout can achieve the effect, but the timing is not accurate setTimeout(() => {
	// Create a Swiper instance object to implement the carousel new Swiper('.swiper-container', {
          autoplay: true,
          // If you need pagination: {
            el: '.swiper-pagination',
            clickable: true
          }
	})
}, 100)

Use vm.$nextTick( [callback] ) to create a Swiper object immediately after waiting for the interface to complete asynchronous update

// Use it immediately after modifying the data, then wait for the DOM to update.
this.$nextTick(() => {
	// Once the interface update is completed, execute the callback immediately new Swiper('.swiper-container', {
    	autoplay: true,
    	pagination:
    	el: '.swiper-pagination',
    	clickable: true
    }
})

The above is the details of the example of how Vue uses the swiper plug-in to implement the carousel. For more information about how Vue uses the swiper plug-in to implement the carousel, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Solution to the failure of swiper loop carousel in vue project
  • Vue encapsulates Swiper to realize the image carousel effect
  • Vue2.0 uses swiper component to achieve carousel effect
  • Vue modifying the style of the small dots of the swiper framework carousel does not work

<<:  Centos7 installation and configuration of Mysql5.7

>>:  Ubuntu 16.04 64-bit compatible with 32-bit programs in three steps

Recommend

How to use async and await in JS

Table of contents 1. async 2. await: 3. Comprehen...

Three ways to implement waterfall flow layout

Preface When I was browsing Xianyu today, I notic...

Sample code for implementing music player with native JS

This article mainly introduces the sample code of...

Analyze the duration of TIME_WAIT from the Linux source code

Table of contents 1. Introduction 2. First, let&#...

Web Design Experience: 5 Excellent Web Design Concepts Full Analysis (Pictures)

Unlike other types of design, web design has been ...

JavaScript to implement the aircraft war game

This article shares with you how to use canvas an...

An exploration of the JS operator in problem

Here's the thing: Everyone knows about "...

How to solve the element movement caused by hover-generated border

Preface Sometimes when hover pseudo-class adds a ...

How to implement communication between Docker containers

Scenario: A laradock development environment (php...

Vue implements a simple magnifying glass effect

This article example shares the specific code of ...

Rainbow button style made with CSS3

Result: Implementation code: html <div class=&...

MySQL 5.7.18 installation and configuration tutorial under Windows

This article shares the installation and configur...

Use overflow: hidden to disable page scrollbars

Copy code The code is as follows: html { overflow...