详细分析Uniapp中的轮播图基本知识(附Demo)

news2024/9/19 13:09:10

目录

  • 前言
  • 1. 基本知识
  • 2. Demo
    • 2.1 基本
    • 2.2 自定义分页
    • 2.3 自定义动画
  • 3. 扩展

前言

先看代码示例:

实现了一个带有分页指示器的轮播图组件

<template>
  <view class="work-container">
    <!-- 轮播图 -->
    <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="current" field="content">
      <swiper class="swiper-box" :current="current" @change="changeSwiper" autoplay interval="3000">
        <swiper-item v-for="(item, index) in data" :key="index">
          <view class="swiper-item" @click="clickBannerItem(item)">
            <image :src="item.image" mode="aspectFill" :draggable="false" />
          </view>
        </swiper-item>
      </swiper>
    </uni-swiper-dot>
  </view>
</template>

<script>
export default {
  data() {
    return {
      current: 0,
      data: [
        { image: '/static/images/banner/banner01.jpg' },
        { image: '/static/images/banner/banner02.jpg' },
        { image: '/static/images/banner/banner03.jpg' }
      ]
    };
  },
  methods: {
    clickBannerItem(item) {
      console.info('Banner item clicked:', item);
    },
    changeSwiper(e) {
      this.current = e.detail.current;
    }
  }
};
</script>

<style lang="scss">
  .swiper-box {
    height: 150px;
  }

  .swiper-item {
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    height: 300rpx;
    line-height: 300rpx;
  }

  @media screen and (min-width: 500px) {
    .uni-swiper-dot-box {
      width: 400px;
      margin: 0 auto;
      margin-top: 8px;
    }
  }
</style>

截图如下:

在这里插入图片描述

1. 基本知识

在 uni-app 中,<swiper> 组件是一个用于轮播图的基础组件,类似于其他前端框架中的 carousel

配合 <swiper-item>,可以实现多个页面、图片或内容的循环切换
uni-swiper-dot 组件用于为轮播图提供分页指示器(即小点)

<swiper> 组件:用于创建轮播图的组件

主要有以下几个常用属性:

  • current:当前显示的 swiper-item 的索引
  • autoplay:是否自动切换,布尔值
  • interval:自动切换的时间间隔,单位为毫秒
  • duration:滑动动画的时长,单位为毫秒
  • circular:是否采用衔接滑动,即是否循环播放
  • indicator-dots:是否显示面板指示点

事件

  • change:轮播图滑动时触发,返回当前页的索引

<swiper-item> 组件:是 swiper 的子组件,用于定义每个可滑动的内容块
有一个 key 属性,用于唯一标识每一个 swiper-item

<uni-swiper-dot> 组件:一个为 swiper 提供分页指示器(小圆点)的组件,是 uni-app 的扩展组件
通过将分页指示器与 swiper 组件结合,可以为用户提供视觉提示,了解当前处于第几页

2. Demo

2.1 基本

简单的轮播图,包含 3 个图片项,开启了自动播放和指示点,间隔为 3 秒,动画时长为 500 毫秒

<template>
  <swiper
    indicator-dots
    autoplay
    interval="3000"
    duration="500"
  >
    <swiper-item>
      <view class="swiper-item">
        <image src="https://example.com/image1.jpg" mode="aspectFill"></image>
      </view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">
        <image src="https://example.com/image2.jpg" mode="aspectFill"></image>
      </view>
    </swiper-item>
    <swiper-item>
      <view class="swiper-item">
        <image src="https://example.com/image3.jpg" mode="aspectFill"></image>
      </view>
    </swiper-item>
  </swiper>
</template>

<style scoped>
.swiper-item {
  width: 100%;
  height: 300px;
}
</style>

2.2 自定义分页

结合 uni-swiper-dot 实现自定义分页指示器

使用了 uni-swiper-dot 组件,自定义了轮播图的分页指示器。这里通过 @change 监听 swiper 的切换事件,动态更新当前页索引

<template>
  <uni-swiper-dot class="uni-swiper-dot-box" :info="data" :current="currentIndex" field="content">
    <swiper class="swiper-box" :current="currentIndex" @change="changeSwiper" autoplay interval="3000">
      <swiper-item v-for="(item, index) in data" :key="index">
        <view class="swiper-item" @click="clickBannerItem(item)">
          <image :src="item.image" mode="aspectFill" />
        </view>
      </swiper-item>
    </swiper>
  </uni-swiper-dot>
</template>

<script>
export default {
  data() {
    return {
      currentIndex: 0,
      data: [
        { image: 'https://example.com/image1.jpg', content: 'Image 1' },
        { image: 'https://example.com/image2.jpg', content: 'Image 2' },
        { image: 'https://example.com/image3.jpg', content: 'Image 3' },
      ],
    };
  },
  methods: {
    changeSwiper(e) {
      this.currentIndex = e.detail.current;
    },
    clickBannerItem(item) {
      console.log('Banner item clicked:', item);
    },
  },
};
</script>

<style scoped>
.swiper-box {
  height: 300px;
}
.swiper-item {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
</style>

2.3 自定义动画

自定义了指示点的颜色,包括默认颜色和当前页的激活颜色,并且开启了 circular 选项实现循环播放

<template>
  <swiper
    autoplay
    interval="4000"
    duration="600"
    indicator-dots
    indicator-color="rgba(255,255,255,0.6)"
    indicator-active-color="rgba(255,0,0,1)"
    circular
  >
    <swiper-item v-for="(item, index) in images" :key="index">
      <view class="swiper-item">
        <image :src="item" mode="aspectFill"></image>
      </view>
    </swiper-item>
  </swiper>
</template>

<script>
export default {
  data() {
    return {
      images: [
        'https://example.com/image1.jpg',
        'https://example.com/image2.jpg',
        'https://example.com/image3.jpg',
      ],
    };
  },
};
</script>

<style scoped>
.swiper-item {
  height: 250px;
  background-color: #ccc;
}
</style>

3. 扩展

常见的功能扩展如下:

手动切换到指定页面,修改 current 变量可以手动切换到某一页

<template>
  <swiper :current="current" @change="changeSwiper">
    <!-- swiper-item content here -->
  </swiper>
  <button @click="current = 1">切换到第二页</button>
</template>

<script>
export default {
  data() {
    return {
      current: 0,
    };
  },
  methods: {
    changeSwiper(e) {
      this.current = e.detail.current;
    },
  },
};
</script>

自定义指示器样式,通过 indicator-color 和 indicator-active-color 可以修改指示器颜色

<swiper :indicator-dots="true" indicator-color="rgba(0,0,0,.3)" indicator-active-color="#007aff">
  <!-- swiper-item content here -->
</swiper>

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

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

相关文章

鸿蒙Harmony应用开发,数据驾驶舱页面的实现

先来看看我们要实现的驾驶舱的页面是什么样的 对于这种 响应式布局的页面构建&#xff0c;我们的脑子里面要有一个概念&#xff0c;就是"分而治之"。我们把这个页面进行分割&#xff0c;分割成不同的块然后再来逐个实现. 不难发现&#xff0c;我们可以将这个看到的效…

ChartLlama: A Multimodal LLM for Chart Understanding and Generation论文阅读

原文链接&#xff1a;https://arxiv.org/abs/2311.16483 代码与数据集&#xff1a;https://tingxueronghua.github.io/ChartLlama/ 本文启发&#xff1a;文章提出利用GPT-4合成大量图表数据&#xff0c;这些数据包含各种图表类型&#xff0c;包含丰富的instruction data。然后…

Day04_JVM实战

文章目录 一、gc日志和dump快照GC日志是什么,要怎么看?dump快照是什么?要怎么看?二、gc日志和dump快照实战java.lang.OutOfMemoryError:Java heap space1、gc.log怎么看2、heapdump.hprof怎么看?①jvisualvm查看②使用MAT查看java.lang.OutOfMemoryError:Metaspace1、实时…

移动技术开发:登录注册界面

1 实验名称 登录注册界面 2 实验目的 掌握基本布局管理器的使用方法和基本控件的使用方法 3 实验源代码 布局文件代码&#xff1a; <?xml version"1.0" encoding"utf-8"?><LinearLayoutxmlns:android"http://schemas.android.com/apk/…

游戏客服精华回复快捷语大全

以黑神话悟空为代表的国内的游戏行业&#xff0c;最近发展非常迅猛&#xff0c;大量游戏玩家需要足够的游戏客服支持&#xff0c;这里整理了游戏客服精华回复快捷语&#xff0c;涵盖了接待客户&#xff0c;游戏级数&#xff0c;游戏外挂&#xff0c;游戏要求&#xff0c;游戏特…

SAP SPROXY 配置

事务码SPROXY 然后找到目标的地址 然后创建新对象即可

【数据结构】排序算法---计数排序

文章目录 1. 定义2. 算法步骤3. 动图演示4. 性质5. 算法分析6. 代码实现C语言PythonJavaGo 结语 1. 定义 计数排序又称为鸽巢原理&#xff0c;是对哈希直接定址法的变形应用。计数排序不是基于比较的排序算法&#xff0c;其核心在于将输入的数据值转化为键存储在额外开辟的数组…

AIGC时代!AI的“iPhone时刻”与投资机遇

AIGC时代&#xff01;AI的“iPhone时刻”与投资机遇 前言AI的“iPhone时刻”与投资机遇 前言 AIGC&#xff0c;也就是人工智能生成内容&#xff0c;它就像是一股汹涌的浪潮&#xff0c;席卷了整个科技世界。它的出现&#xff0c;让我们看到了人工智能的无限潜力&#xff0c;也…

从北大张泽民院士团队的研究成果中寻找医学AI未来的发展方向|个人观点·24-09-19

小罗碎碎念 如果有人问&#xff0c;“从你熟悉的院士中挑选一个&#xff0c;你最先想到的会是谁&#xff1f;“&#xff0c;我会毫不犹豫的回答&#xff1a;张泽民 昨晚一边在操场锻炼&#xff0c;一边在手机里听着一个哈佛的博士直播做报告。听报告的同时&#xff0c;脑子里在…

Android Studio报错: Could not find pub.devrel:easypermissions:0.3.0, 改用linux编译

在Android studio中去编译开源的仓库&#xff0c;大概率就是各种编译不过&#xff0c;一堆错误&#xff0c;一顿改错&#xff0c;基本上会耗费非常多时间&#xff0c;比如&#xff1a; 这个就是改gradle版本&#xff0c;改成7.2 &#xff0c;修改完成之后&#xff0c;还有其他报…

秋招面试注意了!网络安全工程师面试最怕遇到的问题,很多人都经历过!

《网安面试指南》http://mp.weixin.qq.com/s?__bizMzkwNjY1Mzc0Nw&mid2247484339&idx1&sn356300f169de74e7a778b04bfbbbd0ab&chksmc0e47aeff793f3f9a5f7abcfa57695e8944e52bca2de2c7a3eb1aecb3c1e6b9cb6abe509d51f&scene21#wechat_redirect 《Java代码审…

培养关键职业技能,提升个人竞争力

文章目录 一、为什么要培养职业技能&#xff1f;1、提升个人竞争力2、提高工作效率和质量3、适应职业发展变化4、增加收入 二、关键职业技能概述1、专业技术能力2、问题解决能力3、沟通交流能力4、团队合作能力5、领导意识能力6、适应变化能力 三、结语 在当今快速发展的社会中…

react的组件的概念和使用

文章目录 1. **组件的定义****函数组件****类组件** 2. **组件的生命周期**3. **状态管理****类组件中的状态管理****函数组件中的状态管理** 4. **组件之间的通信****通过 Props 传递数据****上下文&#xff08;Context&#xff09;** 5. **组件的样式**6. **处理表单**7. **错…

[SAP ABAP] 修改内表数据

1.利用关键字修改数据 语法格式 MODIFY TABLE <itab> FTOM <wa> [TRANSPORTING f1 f2...].<itab>&#xff1a;代表内表 <wa>&#xff1a;代表工作区 示例1 内表修改前的数据 将上述数据行中的AGE字段值更改为25&#xff0c;SEX字段值更改为女 输出结…

基于windows下docker安装HDDM并运行

安装主要教程 如何安装HDDM(基于windows下 docker 和 linux) | 传鹏的实验室 (chuan-peng-lab.netlify.app) 安装时遇到的问题 1.下载完docker安装包&#xff0c;安装提示不适合本电脑 解决办法&#xff1a; 第一步&#xff1a;开启CPU虚拟化 Windows电脑如何开启CPU虚拟化…

JS全选反选案例

我们在进行网页制作的时候&#xff0c;通常会用到复选框&#xff0c;而复选框外面往往有一个大的勾选框来&#xff0c;控制里面的框是否全部选择&#xff0c;而里面的小复选框同时也是在控制着外面大的选择框&#xff0c;当里面全选的时候&#xff0c;外面的也会勾选上&#xf…

2018年国赛高教杯数学建模D题汽车总装线的配置问题解题全过程文档及程序

2018年国赛高教杯数学建模 D题 汽车总装线的配置问题 一&#xff0e;问题背景   某汽车公司生产多种型号的汽车&#xff0c;每种型号由品牌、配置、动力、驱动、颜色5种属性确定。品牌分为A1和A2两种&#xff0c;配置分为B1、B2、B3、B4、B5和B6六种&#xff0c;动力分为汽油…

自制数据库迁移工具-C版-04-HappySunshineV1.4-(支持Gbase8a、PG)

目录 一、环境信息 二、简述 三、架构图 四、升级点 五、支持功能 六、安装包下载地址 七、配置参数介绍 八、安装步骤 1、配置环境变量 2、生效环境变量 3、检验动态链接是否正常 4、修改配置文件MigrationConfig.txt &#xff08;1&#xff09;Gbase8a -> Gba…

机器学习模型中特征贡献度分析:预测贡献与错误贡献

在机器学习领域&#xff0c;特征重要性分析是一种广泛应用的模型解释工具。但是特征重要性并不等同于特征质量。本文将探讨特征重要性与特征有效性之间的关系&#xff0c;并引入两个关键概念&#xff1a;预测贡献度和错误贡献度。 核心概念 预测贡献度&#xff1a;衡量特征在…

【w0网页制作】Html+Css网页制作影视主题之庆余年Ⅱ含轮播表单(5页面附源码)

庆余年2HTMLCSS网页开发目录 &#x1f354;涉及知识&#x1f964;写在前面&#x1f367;一、网页主题&#x1f333;二、网页效果效果1、轮播效果图Page1、首页Page2、角色介绍Page3、剧情介绍Page4、剧集评价Page5、留言模块 &#x1f40b;三、网页架构与技术3.1 脑海构思3.2 整…