Swiper5&6使用方式以及一些坑

1.安装

npm install swiper --save

2.swiper引入方式

swiper5:

import Swiper from 'swiper' // js 模块
import 'swiper/css/swiper.css'; // css 模块

swiper6:

import Swiper, { Pagination, Navigation, Autoplay } from 'swiper' // js 模块
import 'swiper/swiper-bundle.css' // css 模块
Swiper.use([Pagination, Navigation, Autoplay])

swiper6只能按需引入

css文件夹路径改变

3.初始化

 // eslint-disable-next-line no-unused-vars
   const swiper = new Swiper('.filmswiper', {
      // direction: 'vertical',
      loop: true,
      centeredSlides: true,
      // 如果需要分页器
      pagination: {
        el: '.swiper-pagination',
        clickable: true // 允许分页点击跳转
      },
      // 自动轮播
      autoplay: {
        delay: 2000,
        disableOnInteraction: false
      },
      //导航小点
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev'
      }
    })

4.一个页面有多个轮播图

需要new多个轮播图实例,为了互相不影响,可以在基础类前面加上父类

 // eslint-disable-next-line no-unused-vars
   const swiper = new Swiper('.filmswiper1', {
      // direction: 'vertical',
      loop: true,
      centeredSlides: true,
      // 如果需要分页器
      pagination: {
        el: '.filmswiper1 .swiper-pagination',
        clickable: true // 允许分页点击跳转
      },
      // 自动轮播
      autoplay: {
        delay: 2000,
        disableOnInteraction: false
      },
      //导航小点
      navigation: {
        nextEl: '.filmswiper1 .swiper-button-next',
        prevEl: '.filmswiper1 .swiper-button-prev'
      }
    })

 // eslint-disable-next-line no-unused-vars
   const swiper = new Swiper('.filmswiper2', {
      // direction: 'vertical',
      loop: true,
      centeredSlides: true,
      // 如果需要分页器
      pagination: {
        el: '.filmswiper2 .swiper-pagination',
        clickable: true // 允许分页点击跳转
      },
      // 自动轮播
      autoplay: {
        delay: 2000,
        disableOnInteraction: false
      },
      //导航小点
      navigation: {
        nextEl: '.filmswiper2 .swiper-button-next',
        prevEl: '.filmswiper2 .swiper-button-prev'
      }
    })

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值