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

Install MySQL5.5 database in CentOS7 environment

Table of contents 1. Check whether MySQL has been...

A detailed introduction to JavaScript execution mechanism

Table of contents 1. The concept of process and t...

mysqldump parameters you may not know

In the previous article, it was mentioned that th...

In-depth understanding of JavaScript event execution mechanism

Table of contents Preface The principle of browse...

JS practical object-oriented snake game example

Table of contents think 1. Greedy Snake Effect Pi...

Solution to Incorrect string value in MySQL

Many friends will report the following error when...

Solve the problem of inconsistent MySQL storage time

After obtaining the system time using Java and st...

Vue.js implements tab switching and color change operation explanation

When implementing this function, the method I bor...

MySQL 5.7.33 installation process detailed illustration

Table of contents Installation package download I...

How to modify the IP restriction conditions of MySQL account

Preface Recently, I encountered a requirement at ...

Using css-loader to implement css module in vue-cli

【Foreword】 Both Vue and React's CSS modular s...

How to get the maximum or minimum value of a row in sql

Original data and target data Implement SQL state...