3-12 swiper组件

本文详细介绍了Swiper组件的使用方法,包括其属性如indicator-dots、autoplay等的配置,以及swiper-item的使用。同时提供了示例代码,展示了如何通过bindchange等事件处理用户交互。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

swiper

基础库 1.0.0 开始支持,低版本需做兼容处理

滑块视图容器。其中只可放置swiper-item组件,否则会导致未定义的行为。

属性类型默认值必填说明最低版本
indicator-dotsbooleanfalse是否显示面板指示点1.0.0
indicator-colorcolorrgba(0, 0, 0, .3)指示点颜色1.1.0
indicator-active-colorcolor#000000当前选中的指示点颜色1.1.0
autoplaybooleanfalse是否自动切换1.0.0
currentnumber0当前所在滑块的 index1.0.0
intervalnumber5000自动切换时间间隔1.0.0
durationnumber500滑动动画时长1.0.0
circularbooleanfalse是否采用衔接滑动1.0.0
verticalbooleanfalse滑动方向是否为纵向1.0.0
previous-marginstring"0px"前边距,可用于露出前一项的一小部分,接受 px 和 rpx 值1.9.0
next-marginstring"0px"后边距,可用于露出后一项的一小部分,接受 px 和 rpx 值1.9.0
display-multiple-itemsnumber1同时显示的滑块数量1.9.0
skip-hidden-item-layoutbooleanfalse是否跳过未显示的滑块布局,设为 true 可优化复杂情况下的滑动性能,但会丢失隐藏状态滑块的布局信息1.9.0
easing-functionstring"default"指定 swiper 切换缓动动画类型2.6.5
bindchangeeventhandle current 改变时会触发 change 事件,event.detail = {current, source}1.0.0
bindtransitioneventhandle swiper-item 的位置发生改变时会触发 transition 事件,event.detail = {dx: dx, dy: dy}2.4.3
bindanimationfinisheventhandle 动画结束时会触发 animationfinish 事件,event.detail 同上1.9.0

easing-function 的合法值

说明最低版本
default默认缓动函数 
linear线性动画 
easeInCubic缓入动画 
easeOutCubic缓出动画 
easeInOutCubic缓入缓出动画 

change事件 source 返回值

从 1.4.0 开始,change事件增加 source字段,表示导致变更的原因,可能值如下:

  1. autoplay 自动播放导致swiper变化;
  2. touch 用户划动引起swiper变化;
  3. 其它原因将用空字符串表示。

Bug & Tip

  1. tip: 如果在 bindchange 的事件回调函数中使用 setData 改变 current 值,则有可能导致 setData 被不停地调用,因而通常情况下请在改变 current 值前检测 source 字段来判断是否是由于用户触摸引起。

示例代码

在开发者工具中预览效果

<swiper indicator-dots="{{indicatorDots}}"
  autoplay="{{autoplay}}" interval="{{interval}}" duration="{{duration}}">
  <block wx:for="{{imgUrls}}">
    <swiper-item>
      <image src="{{item}}" class="slide-image" width="355" height="150"/>
    </swiper-item>
  </block>
</swiper>
<button bindtap="changeIndicatorDots"> indicator-dots </button>
<button bindtap="changeAutoplay"> autoplay </button>
<slider bindchange="intervalChange" show-value min="500" max="2000"/>
<slider bindchange="durationChange" show-value min="1000" max="10000"/>
Page({
  data: {
    imgUrls: [
      'https://images.unsplash.com/photo-1551334787-21e6bd3ab135?w=640',
      'https://images.unsplash.com/photo-1551214012-84f95e060dee?w=640',
      'https://images.unsplash.com/photo-1551446591-142875a901a1?w=640'
    ],
    indicatorDots: false,
    autoplay: false,
    interval: 5000,
    duration: 1000
  },
  changeIndicatorDots: function(e) {
    this.setData({
      indicatorDots: !this.data.indicatorDots
    })
  },
  changeAutoplay: function(e) {
    this.setData({
      autoplay: !this.data.autoplay
    })
  },
  intervalChange: function(e) {
    this.setData({
      interval: e.detail.value
    })
  },
  durationChange: function(e) {
    this.setData({
      duration: e.detail.value
    })
  }
})

swiper-item

基础库 1.0.0 开始支持,低版本需做兼容处理

仅可放置在swiper组件中,宽高自动设置为100%。

属性类型默认值必填说明最低版本
item-idstring 该 swiper-item 的标识符1.9.0

 

### 实现 Vue.js 中 vue-awesome-swiper 的一页三列布局与无限循环 要在 Vue.js 使用 `vue-awesome-swiper` 插件实现 Swiper 一页显示三列布局并支持无限循环效果,可以通过配置 Swiper 参数来完成。以下是具体方法: #### 配置参数说明 为了实现一页三列布局以及开启无限循环功能,需要设置以下关键参数: - **slidesPerView**: 设置每页展示的滑动项目数量为 3[^1]。 - **spaceBetween**: 控制幻灯片之间的间距[^1]。 - **loop**: 开启无限滚动模式[^1]。 这些参数可以直接通过 `swiper-options` 属性传递给组件。 #### 安装依赖 如果尚未安装插件及其样式文件,则需先执行如下命令进行安装: ```bash npm install vue-awesome-swiper swiper --save ``` #### 组件代码示例 下面是一个完整的代码示例,展示了如何在 Vue.js 中使用 `vue-awesome-swiper` 来创建一个具有一页三列布局和无限循环特性的轮播图。 ```html <template> <div class="swiper-container"> <!-- 轮播容器 --> <swiper :options="swiperOption" ref="mySwiper"> <!-- 幻灯片内容 --> <swiper-slide v-for="(item, index) in items" :key="index">{{ item }}</swiper-slide> <!-- 如果需要分页器 --> <div class="swiper-pagination" slot="pagination"></div> <!-- 如果需要导航按钮 --> <div class="swiper-button-prev" slot="button-prev"></div> <div class="swiper-button-next" slot="button-next"></div> </swiper> </div> </template> <script> import { Swiper, SwiperSlide } from &#39;vue-awesome-swiper&#39; import &#39;swiper/swiper-bundle.css&#39; export default { components: { Swiper, SwiperSlide }, data() { return { items: [&#39;Item 1&#39;, &#39;Item 2&#39;, &#39;Item 3&#39;, &#39;Item 4&#39;, &#39;Item 5&#39;], // 数据源 swiperOption: { slidesPerView: 3, // 每页显示三个slide spaceBetween: 30, // slide之间间隔像素数 loop: true, // 启用无限循环模式 pagination: { // 分页器选项 el: &#39;.swiper-pagination&#39;, clickable: true }, navigation: { // 导航箭头选项 nextEl: &#39;.swiper-button-next&#39;, prevEl: &#39;.swiper-button-prev&#39; } } } } } </script> <style scoped> .swiper-container { width: 80%; margin: auto; } .swiper-slide { text-align: center; /* 可选 */ font-size: 18px; /* 可选 */ background-color: #f7f7f7; /* 可选 */ height: 150px; /* 自定义高度 */ display: flex; align-items: center; justify-content: center; } </style> ``` 以上代码实现了基本的一屏三列布局,并且开启了无限循环的功能。需要注意的是,实际应用中可以根据需求调整 `slidesPerView`, `spaceBetween` 和其他相关属性以适应不同的屏幕尺寸或设计风格。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值