位置: IT常识 - 正文

vue2项目之swiper.js 的使用(vue中使用swiper6)

编辑:rootadmin
vue2项目之swiper.js 的使用 swiper.js 的使用

推荐整理分享vue2项目之swiper.js 的使用(vue中使用swiper6),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:vue swipper,vue swipper,vue中使用swiper6,vue项目使用swiper,vue-swiper,vue-swiper,vue中使用swiper6,vue-swiper,内容如对您有帮助,希望把文章链接给更多的朋友!

官网 API(部署在 swiper 实例中):https://www.swiper.com.cn/api/index.html

官网轮播图(查看源代码):https://www.swiper.com.cn/demo/index.html

接下来介绍怎么在 vue2 里使用 swiper.js (vue2 使用 swiper5版本)

1、安装、引入css

npm i swiper@5// main.js// 引入 swiper.cssimport "swiper/css/swiper.css";

2、在组件中使用:引入 js 引入 html 结构

import Swiper from 'swiper'

html 结构:

1、开始先放个图片占个位置确定布局,再把图片换成下面的结构

2、注意最外层的 class="swiper-container" 必须!且后面的 swiper 实例也要改!

<div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(img,index) in bannerList" :key="index"> <img :src="img.imgUrl" /> </div> </div> <div class="swiper-button-next"></div> <div class="swiper-button-prev"></div> <div class="swiper-pagination"></div></div>

3、最后关键是创建 swiper 实例! 有两种方式

方式一:

如果图片已经固定(或图片url数组已经确定 )那么直接在 mounted 函数中创建

mounted() { // 下面是普通swiper模板 new Swiper(".swiper-container", { loop: true, mousewheel: true, keyboard: true, navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev", }, pagination: { el: ".swiper-pagination", }, });}

方式二:

vue2项目之swiper.js 的使用(vue中使用swiper6)

用到 v-for 遍历图片url数组(并且该数组是在本组件中通过发请求获取的),那么就要用到 watch + $nextTick

5.11.1 watch+$nextTick

当一个数据发生改变时,此时 DOM 还没有更新,所以在监视属性中的 handle 函数中 写一个 $nextTick 可以实现 数据发生改变且 DOM 更新后执行代码

回到 swiper ,我们在这个时候 创建 swiper 实例

bannerList:图片url数组

watch: { bannerList: { handler() { this.$nextTick(function() { new Swiper(".swiper-container", { loop: true, mousewheel: true, keyboard: true, navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev", }, pagination: { el: ".swiper-pagination", }, }); }) } }},5.11.2 修改分页器样式

1、添加属性

pagination: { el: ".swiper-pagination", clickable: true, bulletClass : 'my-bullet', // 这个 bulletActiveClass: 'my-bullet-active',},

2、在组件里面写 css (不要加 scope)

// 分页器样式.my-bullet{ position: relative; display: inline-block; width: 15px; height: 15px; border-radius: 100%; background: black; opacity: 0.5; margin: 0 4px;}// 选中的分页器样式(会继承上面那个样式).my-bullet-active { background: #ff6600; opacity: 1;}5.11.3 封装轮播图组件

当一个图片需要变为轮播图时,我们把 img 标签 换成 Carousel 组件即可!

1、Carousel 组件需要一个参数:图片 url 数组

imgList = [ {imgUrl: '...'} {imgUrl: '...'}]

2、将 Carousel 组件注册为全局组件

// 在 components 中新建 Carousel 文件夹// main.jsimport Carousel from '@/components/Carousel'Vue.component(Carousel.name,Carousel)

3、Carousel/index.vue (直接照搬即可 样式可自行修改)

<template> <div class="swiper-container"> <div class="swiper-wrapper"> <div class="swiper-slide" v-for="(img,index) in imgList" :key="index"> <img :src="img.imgUrl" /> </div> </div> <div class="swiper-button-next"></div> <div class="swiper-button-prev"></div> <div class="swiper-pagination"></div> </div></template><script> import Swiper from 'swiper' export default { name: 'Carousel', props: ['imgList'], watch: { imgList: { immediate: true, handler() { this.$nextTick(function() { new Swiper(".swiper-container", { loop: true, pagination: { el: ".swiper-pagination", clickable: true, bulletClass : 'my-bullet', bulletActiveClass: 'my-bullet-active', }, navigation: { nextEl: ".swiper-button-next", prevEl: ".swiper-button-prev", }, }); }) } } } }</script><style lang="less"> // 分页器样式 .my-bullet{ position: relative; display: inline-block; width: 15px; height: 15px; border-radius: 100%; background: black; opacity: 0.5; margin: 0 4px; } // 选中的分页器样式(会继承上面那个样式) .my-bullet-active { background: #ff6600; opacity: 1; }</style>

4、组件中使用(传入图片 url 数组即可)

<Carousel :imgList="bannerList" />5.11.4 swiper 属性

1、 <div class="swiper-container">:为轮播图大盒子

2、<div class="swiper-slide">:为装图片的盒子,可以指定大小,那么图片直接适配。或者不指定大小,则需要指定图片大小。

3、slidesPerView:设置 轮播图大盒子 显示 轮播图 张数,轮播图 会被修改宽度适配 轮播图大盒子

4、slidesPerGroup:每次切换 轮播图 张数

5、给 <div class="swiper-slide"> 添加类名 swiper-no-swiping :禁止滑动

本文链接地址:https://www.jiuchutong.com/zhishi/287305.html 转载请保留说明!

上一篇:Gharial野生动物保护区中的两只玫瑰环鹦鹉,印度中央邦 (© Pete Oxford/Minden Pictures)(野生动物huan)

下一篇:vue3+element-plus Dialog对话框的使用 与 setup 写法的使用

  • 航信金税盘时间校准
  • 税收分类方法表格
  • 一般纳税人企业所得税是多少
  • 纳税调整减少额是什么意思
  • 政府补助是否可以加计抵扣
  • 收到公司发来的材料,计入会计分录
  • 公司购买银行理财产品怎么做账
  • 出口退税是按照出口金额吗
  • 企业所得税的利润总额是利润表中的哪个数
  • 国库存款利息收入计入哪个预算收入科目
  • 指定会计科目是
  • 行政事业单位凭证培训课件
  • 机动车临时号牌有效期多久
  • 出租设备应计入什么科目
  • 检测费属于什么税目
  • 资产减值损失和信用损失的区别
  • 增值税专用发票可以开电子发票吗
  • 为什么非征期不允许汇总上传
  • 增值税进项税抵扣规定
  • 公司解散实收资本会计怎么处理
  • 收回购货方前欠货款属于什么
  • 进项税转出如何做分录
  • 记账凭证需要哪些人员签章
  • 支付测量费的补贴费用怎么记账
  • 应收股利的账务处理方法
  • 远期汇票的付款期限可以表示为
  • 无法收回的款项摘要怎么写
  • 国家统计局一套表平台网址
  • 企业发生的亏损不一定会导致所有者权益减少
  • u盘格式化后怎么还原数据
  • 股东投资款验资后可以转出吗
  • elementui ts
  • 补充养老保险费扣除限额
  • php检测是否登录
  • CNN+LSTM+Attention实现时间序列预测(PyTorch版)
  • php ucwords() 函数将字符串中每个单词的首字符转换为大写(实现代码)
  • php分层架构
  • 前端网页设计的三大技术
  • reset fields
  • pytorch go
  • Vue(ref和$refs属性介绍与使用)
  • gpgpu编程技术
  • php的foreach遍历原理
  • 人工费和劳务费怎么算
  • 权益法转换为成本法
  • 子公司收购母公司另一子公司会计处理
  • python的多线程 吃cpu太厉害
  • 首涂第二十一套模板
  • 公司给员工交社保对公司有什么好处
  • 装订会计凭证步骤
  • 社保缴费是当月扣当月的吗
  • 长期借款属于什么会计分录
  • 安装sqlserver2005警告iis
  • sql字符串处理函数
  • 本年利润怎么结转成未分配利润
  • 合作建房土地增值税清算案例
  • 资产减值损失如何计提
  • 土地收储款是谁给谁
  • 怎么解释税收
  • 企业对于发出的货物
  • sql导入和导出数据
  • 如何获取数据库的sid
  • sqlserver查询重复值
  • mssql数据库的账号密码
  • ubuntu怎么把软件放到桌面
  • windows7 记事本
  • CentOS 6.2(32位/64位) 安装步骤图文详解
  • 64位CentOS 6.4安装配置流量监控工具ntopng
  • win7任务栏跑到右边了
  • linux切换到指定目录
  • retrofit提交表单
  • unity音频导入设置
  • android 标签页
  • Python3.6正式版新特性预览
  • zabbix 微信
  • 上季度报表错误怎样更正
  • 垠坤集团是属于国企吗
  • 一般纳税人提供公共交通运输服务免征增值税
  • 小规模纳税人租赁房屋税率
  • 广东个体户年报微信申报
  • 免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

    鄂ICP备2023003026号

    网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

    友情链接: 武汉网站建设