Vue3使用触摸滑动插件(Swiper)

news2024/10/7 7:24:03

Vue2使用触摸滑动插件(Swiper)

参考文档:

  • Swiper官方

  • Swiper Vue

  • Swiper Demos

本文使用的是最新版本:Swiper@9.2.2

安装插件:yarn add swiper

本文基于Swiper插件进行封装,主要实现两种形式的轮播图展示:

  • 首页轮播图切换展示(type: banner)

  • 走马灯轮播图滚动展示(type: carousel)

可自定义设置以下属性:

  • 轮播图片数组(images),类型:Array<{title: string, link?: string, src: string}>,默认 []

  • 图片宽度(width),类型:number | string,默认 '100%'

  • 图片高度(height),类型:number | string,默认 '100vh'

  • banner轮播图模式 | carousel走马灯模式(type),类型:string,默认 'banner'

  • 是否显示导航(navigation),类型:boolean,默认 true

  • 自动切换的时间间隔(type: banner时生效)(delay),单位ms,类型:number,默认 3000

  • 是否可以鼠标拖动(swipe),类型:boolean,默认 true

  • 预加载时的loading颜色(preloaderColor),类型:string,默认 'theme',可选 theme(主题色) | white | black

效果如下图:

首页轮播图 type: banner

走马灯 type: carousel 

①创建触摸滑动组件Swiper.vue:

<script setup lang="ts">
import { Swiper, SwiperSlide } from 'swiper/vue'
import { Pagination, Navigation, Autoplay, EffectFade } from 'swiper'
import 'swiper/css'
import 'swiper/css/navigation'
import 'swiper/css/pagination'
import 'swiper/css/effect-fade'
import { ref, computed } from 'vue'
interface Image {
  title: string // 图片名称
  link?: string // 图片跳转链接
  src: string // 图片地址
}
interface Props {
  images: Image[] // 轮播图片数组
  width?: number|string // 图片宽度
  height?: number|string // 图片高度
  type?: string // banner轮播图模式 | carousel走马灯模式
  navigation?: boolean // 是否显示导航
  delay?: number // 自动切换的时间间隔(type: banner时生效),单位ms
  swipe?: boolean // 是否可以鼠标拖动
  preloaderColor?: string // 预加载时的loading颜色
}
const props = withDefaults(defineProps<Props>(), {
  images: () => [],
  width: '100%',
  height: '100vh',
  type: 'banner', // 可选 banner | carousel
  navigation: true,
  delay: 3000,
  swipe: true,
  preloaderColor: 'theme' // 可选 theme white black
})
const imgWidth = computed(() => {
  if (typeof props.width === 'number') {
    return props.width + 'px'
  } else {
    return props.width
  }
})
const imgHeight = computed(() => {
  if (typeof props.height === 'number') {
    return props.height + 'px'
  } else {
    return props.height
  }
})
const modulesBanner = ref([Navigation, Pagination, Autoplay, EffectFade])
const pagination = ref({
  clickable: true
})
const autoplayBanner = ref({
  delay: props.delay, 
  disableOnInteraction: false, // 用户操作swiper之后,是否禁止autoplay。默认为true:停止。
  pauseOnMouseEnter: true // 鼠标置于swiper时暂停自动切换,鼠标离开时恢复自动切换,默认false
})

const modulesCarousel = ref([Autoplay])
const autoplayCarousel = ref<object|boolean>({
  delay: 0,
  disableOnInteraction: false
})
function onSwiper (swiper: any) {
  // console.log(swiper)
  if (props.type === 'carousel') {
    swiper.el.onmouseenter = () => { // 移入暂停
      swiper.autoplay.stop()
    }
    swiper.el.onmouseleave = () => { // 移出启动
      swiper.autoplay.start()
    }
  }
}
</script>
<template>
  <swiper
    :class="{'swiper-no-swiping': !swipe}"
    v-if="type==='banner'"
    :modules="modulesBanner"
    :lazy="true"
    :navigation="navigation"
    :pagination="pagination"
    :slides-per-view="1"
    :autoplay="autoplayBanner"
    :loop="true"
    @swiper="onSwiper"
    @slideChange="$emit('change')"
    v-bind="$attrs">
    <swiper-slide v-for="(image, index) in images" :key="index">
      <a :href="image.link ? image.link:'javascript:;'" :target="image.link ? '_blank':'_self'" class="m-link">
        <img
          :src="image.src"
          class="u-img"
          :style="`width: ${imgWidth}; height: ${imgHeight};`"
          :alt="image.title"
          loading="lazy" />
      </a>
      <div :class="`swiper-lazy-preloader swiper-lazy-preloader-${preloaderColor}`"></div>
    </swiper-slide>
  </swiper>
  <swiper
    class="swiper-no-swiping"
    v-if="type==='carousel'"
    :modules="modulesCarousel"
    :lazy="true"
    :autoplay="autoplayCarousel"
    :loop="true"
    @swiper="onSwiper"
    @slideChange="$emit('change')"
    v-bind="$attrs">
    <swiper-slide v-for="(image, index) in images" :key="index">
      <a :href="image.link ? image.link:'javascript:;'" :target="image.link ? '_blank':'_self'" class="m-link">
        <img
          :src="image.src"
          class="u-img"
          :style="`width: ${imgWidth}; height: ${imgHeight};`"
          :alt="image.title"
          loading="lazy" />
      </a>
      <div :class="`swiper-lazy-preloader swiper-lazy-preloader-${preloaderColor}`"></div>
    </swiper-slide>
  </swiper>
</template>
<style lang="less" scoped>
.m-link {
  display: block;
  height: 100%;
}
.u-img {
  object-fit: cover;
  cursor: pointer;
}
.swiper {
  --swiper-theme-color: @themeColor;
}
:deep(.swiper-wrapper) { // 自动切换过渡效果设置
  transition-timing-function: linear; // 线性过渡模拟走马灯效果
  -webkit-transition-timing-function: linear;
}
:deep(.swiper-pagination-bullet) {
  width: 36px;
  height: 4px;
  background: #E3E3E3;
  border-radius: 1px;
  margin-right: 10px;
  cursor: pointer;
}
:deep(.swiper-pagination-bullet-active) {
  background: @themeColor;
}
.swiper-lazy-preloader-theme {
  --swiper-preloader-color: @themeColor;
}
</style>

②在要使用的页面引入:

<script setup lang="ts">
import { Swiper } from './Swiper.vue'
import { ref, onBeforeMount } from 'vue'
import { getImageUrl } from '@/utils/util'

const images = ref<any[]>([])

function loadImages () {
  for (let i = 1; i <= 10; i++) {
    images.value.push({
      title: `image-${i}`,
      link: '',
      src: getImageUrl(i)
    })
  }
  console.log(images.value)
}
onBeforeMount(() => { // 组件已完成响应式状态设置,但未创建DOM节点
  loadImages()
})
function onChange () {
  console.log('slider change')
}
</script>
<template>
  <div>
    <h1>Swiper 参考文档</h1>
    <ul class="m-list">
      <li>
        <a class="u-file" href="https://swiperjs.com/" target="_blank">Swiper官方</a>
      </li>
      <li>
        <a class="u-file" href="https://swiperjs.com/vue" target="_blank">Swiper Vue</a>
      </li>
      <li>
        <a class="u-file" href="https://swiperjs.com/demos" target="_blank">Swiper Demos</a>
      </li>
    </ul>
    <h2 class="mt30 mb10">Swiper 轮播图基本使用 (type: banner)</h2>
    <Swiper
      :images="images"
      type="banner"
      effect="slider"
      width="100%"
      height="100vh"
      navigation
      @change="onChange" />
    <h2 class="mt30 mb10">Swiper 走马灯基本使用 (type: carousel)</h2>
    <Swiper
      :images="images"
      type="carousel"
      width="100%"
      preloaderColor="theme"
      :height="240"
      :slides-per-view="3"
      :space-between="20"
      :speed="2500" />
  </div>
</template>
<style lang="less" scoped>
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/425687.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

电脑自带远程桌面和远程控制软件哪个好?

随着科技的不断发展和普及&#xff0c;越来越多的公司和个人开始关注远程控制软件的使用。我们常常需要在不同的地方工作&#xff0c;但工作需要的文件和软件却只能在一个地方使用&#xff0c;这时候远程控制电脑软件就变得尤为重要咯。但是&#xff0c;许多人分不清楚&#xf…

Windows远程连接工具有哪些

Windows远程连接工具&#xff0c;一般称为远程桌面软件&#xff0c;更准确的叫远程访问软件或远程控制软件&#xff0c;可以让你从一台电脑远程控制另一台电脑。远程桌面软件允许您控制连接的计算机&#xff0c;就好像它就在您面前一样。 远程桌面工具可用于技术支持、远程工作…

单TYPE-C口 可支持快充又可传输USB2.0数据方案

虽然现在有不少厂商也采用了Type-C接口&#xff0c;但是只作为一个充电接口&#xff0c;对于跨时代的type-c接口来说&#xff0c;多少有点大材小用&#xff0c; 那么有没有办法&#xff0c;让一个type-c接口既可以充电&#xff0c;又可以接OTG&#xff1f;比如不充电的时候可以…

Python一行命令搭建HTTP服务器并外网访问 - 内网穿透

文章目录1.前言2.本地http服务器搭建2.1.Python的安装和设置2.2.Python服务器设置和测试3.cpolar的安装和注册3.1 Cpolar云端设置3.2 Cpolar本地设置4.公网访问测试5.结语转载自远程内网穿透的文章&#xff1a;【Python】快速简单搭建HTTP服务器并公网访问「cpolar内网穿透」 1…

第二章:HTML CSS 网页开发基础(二)

CSS概述 CSS全称&#xff1a;Cascading Style Sheet&#xff0c;可以对文字进行重叠&#xff0c;定位。主要实现页面美化。 一、CSS规则 CSS样式表中包括了3部分&#xff1a;选择符、属性、属性值 选择符{属性:属性值;} 选择符&#xff1a;也可以称为选择器&#xff0c;所有…

Java实现输入行数打印取缔字符,打印金字塔三角形的两个代码程序

目录 前言 一、实现输入行数&#xff0c;打印取缔字符 1.1运行流程&#xff08;思想&#xff09; 1.2代码段 1.3运行截图 二、打印金字塔三角形 1.1运行流程&#xff08;思想&#xff09; 1.2代码段 1.3运行截图​​​​​​​ 前言 1.因多重原因&#xff0c;本博文有…

【BlazePose】《BlazePose: On-device Real-time Body Pose tracking》

arXiv-2020 文章目录1 Background and Motivation2 Advantages / Contributions3 Method4 Experiments5 Conclusion&#xff08;own&#xff09;1 Background and Motivation 人体关键点存在的难点&#xff1a;a wide variety of poses, numerous degrees of freedom, and occ…

JavaWeb—Maven

目录 1.什么是Maven 2.Maven的作用 3.Maven概述 3.1Maven介绍 3.2 Maven模型 3.3 Maven仓库 1.什么是Maven Maven是Apache旗下的一个开源项目&#xff0c;是一款用于管理和构建java项目的工具。 官网&#xff1a;Maven – Welcome to Apache Mavenhttps://maven.apache.o…

vscode 终端集成bash

windows 版本的 vs code 终端默认是没有集成bash的&#xff0c;虽然也能在vscode 终端可以提交git&#xff0c;但是没有高亮&#xff0c;没有提示&#xff0c;很不方便&#xff0c;这时候就需要我们将bash集成到vs code的终端&#xff0c;就可以愉快的使用git的分支高亮&#x…

阿里云蔡英华:云智一体,让产业全面迈向智能

4月11日&#xff0c;在2023阿里云峰会上&#xff0c;阿里云智能首席商业官蔡英华表示&#xff0c;算力的飞速发展使数字化成为确定&#xff0c;使智能化成为可能。阿里云将以云计算为基石&#xff0c;以AI为引擎&#xff0c;参与到从数字化迈向智能化的划时代变革中。 基于服务…

资深PM赞不绝口的【9种项目管理图】

好用的项目管理工具可以帮助项目经理掌握项目进度&#xff0c;更好的拆分任务&#xff0c;节约时间。 今天给大家安排上&#xff0c;助力大家在项目交付路上更顺畅&#xff0c;早日以高质量交付结果&#xff0c;找到百万年薪工作。 ​项目管理甘特图扫描Q群二维码下载Q群5330…

MySQL--表的使用--0409

目录 1.表的基本操作 1.1 创建表 2. 查看表结构 3.修改表 3.1 新增一列 3.2 修改列属性 3.3 修改名字 3.3.1 修改表名字 3.3.2 修改表内列名字 3.4删除 3.4.1 删除列 3.4.2 删除表 1.表的基本操作 查看自己目前在哪个数据库里面 mysql> select database(); 1.1 创…

SpringBoot整合 EasyES (八)

一直在坑自己家人&#xff0c;对&#xff0c;说的就是你&#xff0c;大A. 上一章简单介绍了SpringBoot整合ES 实现简单项目(七), 如果没有看过,请观看上一章 Mybatis 有增强性的 MybatisPlus, ES 有增强性的吗? 有的&#xff0c; easy-es ​ Easy-Es&#xff08;简称EE&…

java捕获编译时异常exception和运行时错误error的方法

背景 最近使用jacob的时候&#xff0c;由于编译没问题&#xff0c;运行时报如下&#xff0c;我 查看代码发现是调用jacob文件时&#xff0c;是下面的方法报错&#xff0c; ComThread.Release(); 这个方法编译不报错&#xff0c;是因为doCoUninitialize使用native修饰的&#…

java 通过 spring 官网创建springboot项目

文章java简单一写一个springboot入门案例带大家用idea工具工具创建了一个springboot简单的小案例 但有时 我们idea如果连不上网 就会有点问题 我们可以采用另一种创建方式 但这里的前提肯定就是 你的计算机是要有网的 然后访问 https://spring.io/ 打开spring的官网 在 Project…

去了字节跳动,才知道年薪40W的测试有这么多?

今年大环境不好&#xff0c;内卷的厉害&#xff0c;薪资待遇好的工作机会更是难得。最近脉脉职言区有一条讨论火了&#xff1a; 哪家互联网公司薪资最‘厉害’&#xff1f; 下面的评论多为字节跳动&#xff0c;还炸出了很多年薪40W的测试工程师 我只想问一句&#xff0c;现在的…

数据结构进阶:前缀和与差分

数据结构进阶&#xff1a;前缀和与差分基础前缀和基础差分区间乘积前缀置换经典差分性质题目前缀和变种高次前缀和高维前缀和 (SOSDP)蓝桥杯已经结束&#xff0c;打下来很难受。自己对于算法的掌握还是不够&#xff0c;遂继续开启博客书写&#xff0c;激励自己学习。本系列文章…

FinClip 云开发实践(附小程序demo)

在开发一个小程序时&#xff0c;除了考虑界面功能逻辑外&#xff0c;还需要后端的数据支持&#xff0c;开发者需要提前考虑服务器、存储和数据库等相关需求的支持能力&#xff0c;此外还可能需要花费时间精力在部署应用、和依赖服务的建设上。 ​ 因此&#xff0c;腾讯小程序为…

【Java】类和对象详解

1. 类和对象 1.1 类和对象的理解 客观存在的事物皆为对象 &#xff0c;所以我们也常常说万物皆对象。 类 类的理解 类是对现实生活中一类具有共同属性和行为的事物的抽象类是对象的数据类型&#xff0c;类是具有相同属性和行为的一组对象的集合简单理解&#xff1a;类就是对…

Dva.js(基础、简单例子解读)

简单介绍一下 近期在做react项目时&#xff0c;看到项目中数据的公共存储用的Dva.js&#xff0c;整体的代码结构看起来和vuex差不多&#xff0c;这两天趁着刚忙完&#xff0c;利用工作之余的时间空隙&#xff0c;大致了解了dva的基础理论&#xff0c;代码结构应用&#xff0c;参…