Swiper、vue-awesome-swiper 插件使用

2023-11-14

Swiper在PC端和移动端都适用

官方网站:Swiper中文网首页-官方

vue2 配合 swiper5或6版本

vue3 可以使用 swiper8最新版本

一、Swiper插件(Vue、React、Angular框架都可以使用)

1、使用方法

Swiper使用方法(一个简单轮播的实现)-官方

2、版本简介

swiper简介和swiper各版本兼容性-官方

3、版本下载(以swiper5为例:点击下载swiper-5.4.5.zip

下载Swiper包-官方

4、具体实现 

4-1、下载至本地之后,根据 swiper5 版本需要,保留swiper.min.js和swiper.min.css文件

4-2、根据下方链接进行配置

Swiper使用方法-官方

<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Swiper使用方法里的官方代码</title>
  <link rel="stylesheet" href="./css/swiper.min.css">
  <script src="./js/swiper.min.js"></script>
  <style>
    .swiper-container {
      width: 600px;
      height: 300px;
      border: 1px solid red;
    }
  </style>
</head>

<body>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide">Slide 1</div>
      <div class="swiper-slide">Slide 2</div>
      <div class="swiper-slide">Slide 3</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>
  <script>
    var mySwiper = new Swiper('.swiper', {
      direction: 'vertical', // 垂直切换选项
      loop: true, // 循环模式选项

      // 如果需要分页器
      pagination: {
        el: '.swiper-pagination',
      },

      // 如果需要前进后退按钮
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },

      // 如果需要滚动条
      scrollbar: {
        el: '.swiper-scrollbar',
      },
    })        
  </script>
</body>

</html>

要注意:Swiper7的默认容器是'.swiper',Swiper6之前是'.swiper-container'

操作完以上步骤,此时swiper轮播已经是能正常切换了。其他功能可以结合API文档完成。 

Swiper参数选项API文档-官方

5、个人优化

在上面轮播基础上添加了鼠标移入,轮播切换停止;鼠标移出,轮播切换继续效果。

鼠标移入移出效果-官方

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 引包 -->
    <script src="./js/swiper.min.js"></script>
    <link rel="stylesheet" href="./css/swiper.min.css">
    <style>
        .swiper-container {
            width: 600px;
            height: 400px;
            margin: 100px auto;
            border: 1px solid red;
        }
    </style>
</head>

<body>
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide">Slide 1</div>
            <div class="swiper-slide">Slide 2</div>
            <div class="swiper-slide">Slide 3</div>
        </div>
        <!-- 如果需要分页器 -->
        <div class="swiper-pagination"></div>

        <!-- 如果需要导航按钮 -->
        <div class="swiper-button-prev"></div>
        <div class="swiper-button-next"></div>
    </div>
</body>

</html>
<script>
    //swiper:对外暴露Swiper构造函数
        //第一个参数:模板根节点是CSS选择器或真实DOM节点
        //第二个参数:轮播图配置对象
    let myswiper = new Swiper(".swiper-container", {
        //前进后退按钮的配置项
        navigation: {
            nextEl: '.swiper-button-next',
            prevEl: '.swiper-button-prev',
        },
        //自动切换
        autoplay: {
            delay: 1000,//延迟时间
            stopOnLastSlide: false, //是否到最后一张轮播就停止循环
            disableOnInteraction: true,
        },
        //无缝衔接 无限循环
        loop: true,
        //分页器
        pagination: {
            el: '.swiper-pagination',
            clickable: true,//点击分页器切换图片
        },
    });

    //鼠标进入
    myswiper.el.onmouseover = function () {
        myswiper.autoplay.stop();
    }
    myswiper.el.onmouseout = function () {
        myswiper.autoplay.start();
    }
</script>

二、vue-awesome-swiper插件(仅供Vue框架使用)

vue-awesome-swiper插件在swiper插件基础上进行的封装,封装成了vue组件。

所以在使用vue-awesome-swiper插件前,要先安装swiper。

vue3 + vue-awesome-swiper 文档

vue2 + vue-awesome-swiper 文档

 1、Vue2项目使用swiper、vue-awesome-swiper插件,具体实现:

1-1、安装swiper、vue-awesome-swiper
npm i swiper@5 vue-awesome-swiper@4

安装完成后在package.json文件中,如下图:

1-2、代码配置 main.js入口文件中 
import "swiper/css/swiper.min.css"
import { Swiper, SwiperSlide } from 'vue-awesome-swiper';
Vue.component('Swiper', Swiper);
Vue.component('SwiperSlide', SwiperSlide);
// Swiper 代表最外侧容器
// SwiperSlide 代表每一张轮播图
 1-3、代码配置 pages/test/index.vue
<template>
  <Swiper class="box" :options="options" ref="swiper">
    <SwiperSlide v-for="item in 10">
      <p>{{ item }}</p>
    </SwiperSlide>
    <!-- 左箭头 -->
    <div slot="button-prev" class="swiper-button-prev"></div>
    <!-- 右箭头 -->
    <div slot="button-next" class="swiper-button-next"></div>
    <!-- 分页器 -->
    <div slot="pagination" class="swiper-pagination"></div>
  </Swiper>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      // options配置项: 使用swiper官网的配置项即可
      options: {
        //导航前进后退按钮
        navigation: {
          nextEl: ".swiper-button-next",
          prevEl: ".swiper-button-prev",
        },
        //自动轮播图
        autoplay: {
          delay: 1000,
          stopOnLastSlide: false,
          disableOnInteraction: true,
        },
        //无缝衔接
        loop: true,
        //分页器配置项
        pagination: {
          el: ".swiper-pagination",
          clickable: true, // 点击分页器小球进行轮播图切换
        },
        //切换效果
        effect: "cube",
      },
    };
  },
  mounted() {
    //鼠标进入-暂停
    this.$refs.swiper.$el.onmouseenter = () => {
      this.$refs.swiper.$swiper.autoplay.stop();
    };
    //鼠标离开-开始
    this.$refs.swiper.$el.onmouseleave = () => {
      this.$refs.swiper.$swiper.autoplay.start();
    };
  },
};
</script>

<style scoped>
.box {
  width: 600px;
  height: 400px;
  border: 1px solid red;
  margin: 10px auto;
}
p {
  width: 100%;
  height: 100%;
  background: cyan;
}
</style>

操作完以上步骤,此时vue-awesome-swiper轮播已经是能正常切换了。其他功能可以结合swiper的API文档完成(是的,没有看错,swiper的api文档)。

vue-awesome-swiper API文档

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Swiper、vue-awesome-swiper 插件使用 的相关文章

随机推荐

  • Mysql的mysql_store_result/mysql_use_result,mysql_field_count/mysql_num_fields/mysql_num_rows函数区别

    Mysql的mysql store result mysql use result mysql field count mysql num fields mysql num rows函数区别 1 先参考以下文章 或者先看下面再回来理解 my
  • list.clear()与=null以及new ArrayList<E>()的差异(Map类似)

    1 使用clear 方法 List
  • linux下载命令wget命令详解

    wget是linux最常用的下载命令 一般的使用方法是 wget 空格 要下载文件的url路径 例如 wget http www linuxsense org xxxx xxx tar gz 简单说一下 c参数 这个也非常常见 可以断点续传
  • Angular自定义表单验证

    前端表单验证 为年龄输入框添加了两个验证 并分情况填写了提示语
  • Android开发 之 指纹识别

    指纹识别 在android6 0的时候谷歌对指纹识别开始支持了 现在市面的基本上指纹识别都是在api23以上的版本 涉及到一个系统服务 FingerPrintManager 在使用FingerPrintManager这个类实现的时候发现了很
  • np.meshgrid()函数

    文章目录 1 自己理解 2 官方解释 3 参数 3 1 x1 x2 xn array like 3 2 sparse bool optional 默认false 3 3 copy bool optional 1 自己理解 np meshgr
  • 用GCC开发STM32,正点原子开发板的一个库函数版本例程示例

    GCC环境搭建 首先下载交叉编译环境GCC 这个网上有很多 百度一下就能找到下载 比如 Sourcery G for ARM EABI 我的CSDN资源里 有相关下载 熟悉下Makefile 强力推荐熟悉网络牛人李云的51cto博客 有一篇
  • java 数组的长度_Java初学者:数组,得到数组长度

    得到数组的长度在java里是很简单的 那就是 数组名 length 我们来看一个简单的程序就可以了 如下 这个就是上次的那个程序 我只是把for后面那个4改成了a length 也许有人要问了 那为什么非要求出数组的长度呢 数组在起始的时候
  • Redraiment的遭遇

    描述 Redraiment的老家住在工业区 日耗电量非常大 是政府的眼中钉肉中刺 但又没办法 这里头住的可都是纳税大户呀 今年7月 又传来了不幸的消息 政府要在7 8月对该区进行拉闸限电 但迫于压力 限电制度规则不会太抠门 政府决定从7月1
  • 【CUDA】cuda安装 (windows版)

    CUDA cuda安装 windows10版 一 前言 官方教程 二 安装工具的准备 1 CUDA toolkit Download 2 cuDNN Download 三 CUDA 安装与配置过程 测试环境是否安装成功 四 cuDNN配置
  • JAVA简介

    JAVA简介 java是一种高级的面向对象的程序设计语言 使用java编写的的程序可以在任何计算机 操作系统和支持java的硬件设备上运行 什么是java java语言的发展历程 Java是于1995年由Sun公司推出的一种极富创造力的面向
  • 手撸算法-计算表达式

    牛客原题 描述 请写一个整数计算器 支持加减乘三种运算和括号 示例1 输入 1 2 返回值 3 示例2 输入 2 3 4 5 返回值 10 示例3 输入 3 2 3 4 1 返回值 26 思路 从左向右遍历字符串 1 遇到数字则入栈 注意数
  • C#Winform窗体实现服务端和客户端通信例子(TCP/IP)

    Winform窗体实现服务端和客户端通信的例子 是参考这个地址 http www cnblogs com longwu archive 2011 08 25 2153636 html 进行了一些异常处理 提示信息的补充 还有新增获取本地IP
  • 【docker】docker 实现 的基础

    1 概述 还不懂Docker 一个故事安排的明明白白
  • 大数相加(c++)算法

    有没有想过100位数加100数的数字该如何计算出结果吗 一般计算机是无法直接计算那么大的数字 这个时候我们得模拟我们手算加法的进制过程 如何用代码把它实现 这样子就能实现大数相加了 首先我很感谢汤健同学给我分享的这串代码 我写笔记的初衷是为
  • R 中的with() 函数和 by()函数 的简单使用

    with data expr 函数用于在一个从data构建出的环境中运行R表达式 by data INDICES FUN simplify TRUE 函数用于将data中的数据 按照INDICES里面的内容拆分成若干个小的data fram
  • 提高ubuntu下访问github的速度\加速git clone速度

    这个是真的有用 https blog csdn net hn tzy article details 88903642
  • 实战中的 Promise 和 Future

    上一章介绍了 Future 类型 以及如何用它来编写高可读性 高组合性的异步执行代码 Future 只是整个谜团的一部分 它是一个只读类型 允许你使用它计算得到的值 或者处理计算中出现的错误 但是在这之前 必须得有一种方法把这个值放进去 这
  • Python正则表达式re模块学习遇到的问题

    Python正则表达式处理的组是什么 Python正则表达式处理中的匹配对象是什么 Python匹配对象的groups groupdict和group之间的关系 Python正则表达式re match r a1b2c3 匹配结果为什么是 c
  • Swiper、vue-awesome-swiper 插件使用

    Swiper在PC端和移动端都适用 官方网站 Swiper中文网首页 官方 vue2 配合 swiper5或6版本 vue3 可以使用 swiper8最新版本 一 Swiper插件 Vue React Angular框架都可以使用 1 使用