程序员scholar 程序员scholar
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
首页
  • Java 基础

    • JavaSE
    • JavaIO
    • JavaAPI速查
  • Java 高级

    • JUC
    • JVM
    • Java新特性
    • 设计模式
  • Web 开发

    • Servlet
    • Java网络编程
  • Web 标准

    • HTML
    • CSS
    • JavaScript
  • 前端框架

    • Vue2
    • Vue3
    • Vue3 + TS
    • 微信小程序
    • uni-app
  • 工具与库

    • jQuery
    • Ajax
    • Axios
    • Webpack
    • Vuex
    • WebSocket
    • 第三方登录
  • 后端与语言扩展

    • ES6
    • Typescript
    • node.js
  • Element-UI
  • Apache ECharts
  • 数据结构
  • HTTP协议
  • HTTPS协议
  • 计算机网络
  • Linux常用命令
  • Windows常用命令
  • SQL数据库

    • MySQL
    • MySQL速查
  • NoSQL数据库

    • Redis
    • ElasticSearch
  • 数据库

    • MyBatis
    • MyBatis-Plus
  • 消息中间件

    • RabbitMQ
  • 服务器

    • Nginx
  • Spring框架

    • Spring6
    • SpringMVC
    • SpringBoot
    • SpringSecurity
  • SpringCould微服务

    • SpringCloud基础
    • 微服务之DDD架构思想
  • 日常必备

    • 开发常用工具包
    • Hutoll工具包
    • IDEA常用配置
    • 开发笔记
    • 日常记录
    • 项目部署
    • 网站导航
    • 产品学习
    • 英语学习
  • 代码管理

    • Maven
    • Git教程
    • Git小乌龟教程
  • 运维工具

    • Docker
    • Jenkins
    • Kubernetes
  • 算法笔记

    • 算法思想
    • 刷题笔记
  • 面试问题常见

    • 十大经典排序算法
    • 面试常见问题集锦
关于
GitHub (opens new window)
npm

(进入注册为作者充电)

  • 原生微信小程序

  • uniapp多端开发

    • 快速入门

    • 内置组件

      • 视图容器

        • 视图容器组件
        • 局部滚动容器组件
        • 轮播图组件
          • 1. 什么是 swiper 组件?
          • 2. swiper 组件的常用属性
            • 2.1 显示指示点 indicator-dots
            • 2.2 自动播放 autoplay
            • 2.3 循环滑动 circular
            • 2.4 设置动画时长 duration
          • 3. 自动播放与指示点效果案例
        • 媒体查询组件
        • 可移动区域组件
        • 可移动视图组件
        • 覆盖视图组件
        • 覆盖图片组件
      • 基础内容

      • 表单组件

      • 路由页面跳转

      • 媒体组件

      • 地图

      • 画布

      • 网页嵌入

      • 页面属性配置节点

      • nvue组件

      • 小程序组件

    • 扩展组件

  • 小程序开发
  • uniapp多端开发
  • 内置组件
  • 视图容器
scholar
2024-10-21
目录

轮播图组件

# 轮播图组件

swiper 组件是 Uniapp 中用于创建滑块视图切换的核心组件,广泛应用于轮播图、Banner 广告等场景。与滚动组件不同,swiper 以屏幕为单位进行滑动切换,支持横向和纵向滑动,并可以通过多个属性配置自动播放、循环播放、指示点等功能。

# 1. 什么是 swiper 组件?

swiper 组件用于在固定区域内展示一组滑块内容,支持横向或纵向滑动切换。每个滑块都由 swiper-item 组件表示。通过结合属性配置,swiper 可以轻松实现自动轮播、循环切换、指示点提示等功能,是移动端界面中非常常见的交互方式。

使用场景

  • 轮播图展示:用于广告展示、图片轮播,用户可以通过滑动或自动播放查看多个内容。
  • 页面切换:实现页面级别的滑动切换效果,适合类似于 Tab 切换或全屏页面展示。
  • 多项内容展示:在一屏内显示多项内容,并通过滑动切换查看全部内容。

# 2. swiper 组件的常用属性

swiper 组件提供了多种属性,用于控制滑块的显示方式和交互效果。以下是常用属性的详细说明及使用示例:

属性名 类型 默认值 说明
indicator-dots Boolean false 是否显示滑块面板下方的指示点。指示点用于提示用户当前所在滑块位置。
indicator-color Color rgba(0, 0, 0, .3) 指示点的默认颜色。
indicator-active-color Color #000000 当前选中滑块的指示点颜色。
autoplay Boolean false 是否自动切换滑块。启用后,滑块会按指定的时间间隔自动切换。
current Number 0 当前显示的滑块索引。
interval Number 5000 自动切换的时间间隔,单位为毫秒。
duration Number 500 滑动动画的时长,单位为毫秒。
circular Boolean false 是否启用循环滑动。若为 true,滑动到最后一个滑块时会自动返回第一个。
vertical Boolean false 设置滑动方向为纵向。默认情况下为横向滑动。
previous-margin String 0px 设置前边距,用于控制前一项滑块部分显示,单位为 px 或 rpx。
next-margin String 0px 设置后边距,用于控制后一项滑块部分显示,单位为 px 或 rpx。
display-multiple-items Number 1 设置同时显示的滑块数量。
skip-hidden-item-layout Boolean false 是否跳过未显示的滑块布局。设为 true 时,可以优化滑动性能。
easing-function String default 指定滑块切换时的动画效果,包括 default、linear、easeInCubic 等。

# 2.1 显示指示点 indicator-dots

  • 说明:是否显示滑块下方的指示点,用于指示当前滑块所在的位置。
  • 类型:Boolean
  • 默认值:false
<template>
  <swiper indicator-dots="true">
    <swiper-item>
      <view class="swiper-item">A</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">B</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">C</view>
    </swiper-item>
  </swiper>
</template>

<style scoped>
.swiper-item {
  height: 300rpx;
  background-color: lightblue;
  text-align: center;
  line-height: 300rpx;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 2.2 自动播放 autoplay

  • 说明:是否启用自动切换功能。启用后,滑块将按照 interval 时间间隔自动切换。
  • 类型:Boolean
  • 默认值:false
<template>
  <swiper :autoplay="true" :interval="3000">
    <swiper-item>
      <view class="swiper-item">Slide 1</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">Slide 2</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">Slide 3</view>
    </swiper-item>
  </swiper>
</template>

<style scoped>
.swiper-item {
  height: 300rpx;
  background-color: lightcoral;
  text-align: center;
  line-height: 300rpx;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 2.3 循环滑动 circular

  • 说明:是否启用循环滑动。启用后,滑动到最后一个滑块时会自动切换到第一个滑块,实现循环播放。
  • 类型:Boolean
  • 默认值:false
<template>
  <swiper :circular="true">
    <swiper-item>
      <view class="swiper-item">1</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">2</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">3</view>
    </swiper-item>
  </swiper>
</template>

<style scoped>
.swiper-item {
  height: 300rpx;
  background-color: lightgreen;
  text-align: center;
  line-height: 300rpx;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 2.4 设置动画时长 duration

  • 说明:设置滑块切换时的动画时长,单位为毫秒。
  • 类型:Number
  • 默认值:500
<template>
  <swiper :autoplay="true" :duration="1000">
    <swiper-item>
      <view class="swiper-item">1</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">2</view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">3</view>
    </swiper-item>
  </swiper>
</template>

<style scoped>
.swiper-item {
  height: 300rpx;
  background-color: lightyellow;
  text-align: center;
  line-height: 300rpx;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

# 3. 自动播放与指示点效果案例

以下案例展示了 swiper 组件的实际应用,包括自动播放、循环滑动和指示点的设置。通过组合多个属性,可以灵活实现符合需求的滑动效果。

<template>
  <view>
    <!-- 实现自动播放和循环滑动 -->
    <swiper class="swiper" :autoplay="true" :circular="true" :interval="3000" :indicator-dots="true">
      <swiper-item>
        <view class="swiper-item uni-bg-red">A</view>
      </swiper-item>
      <swiper-item>
        <view class="swiper-item uni-bg-green">B</view>
      </swiper-item>
      <swiper-item>
        <view class="swiper-item uni-bg-blue">C</view>
      </swiper-item>
    </swiper>
  </view>
</template>

<style scoped>
.swiper {
  height: 300rpx;
}

.swiper-item {
  display: block;
  height: 300rpx;
  line-height: 300rpx;
  text-align: center;
  font-size: 36rpx;
}

.uni-bg-red {
  background-color: red;
  color: white;
}

.uni-bg-green {
  background-color: green;
  color: white;
}

.uni-bg-blue {
  background-color: blue;
  color: white;
}
</style>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

recording

编辑此页 (opens new window)
上次更新: 2025/02/01, 02:18:15
局部滚动容器组件
媒体查询组件

← 局部滚动容器组件 媒体查询组件→

Theme by Vdoing | Copyright © 2019-2025 程序员scholar
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式