vue3中实现Swiper轮播图

<script lang="ts" setup>
// 引入swiper组件
import { Swiper, SwiperSlide } from "swiper/vue";
import { ref } from "vue";
import { getCompanyNameList } from "@/api/auth";
import { ElMessage } from "element-plus";
// 引入swiper样式(按需导入)
import "swiper/css";
import "swiper/css/pagination"; // 轮播图底面的小圆点
import "swiper/css/navigation"; // 轮播图两边的左右箭头
import "swiper/css/scrollbar"; // 轮播图的滚动条

// 引入swiper核心和所需模块
import { Autoplay, Pagination, Navigation, Scrollbar } from "swiper";
// Scrollbar
const modules = [Autoplay, Pagination, Navigation];
const bgColor = ref([
  "background-color: #fff9c4",
  "background-color: #b3e5fc",
  "background-color: #ffebee",
  "background-color: #bfc",
  "background-color: skyblue",
]);
const listData = ref([]);
const getDetailsList = async () => {
  const { data } = await getCompanyNameList();
  if (data.length > 0) {
    data.forEach((element: { bgColor: string }, i: number) => {
      element.bgColor = bgColor.value[i];
    });
    listData.value = data;
  } else {
    ElMessage({
      message: "暂无数据",
      type: "warning",
    });
  }
};
getDetailsList();
</script>

```html
<template>
  <div class="home">
    <!-- :loop="true" -->
    <!-- :scrollbar="{ draggable: true }"  -->
    <swiper
      :modules="modules"
      :slides-per-view="5"
      :direction="'vertical'"
      :autoplay="{ delay: 3000, disableOnInteraction: false }"
      :pagination="{ clickable: true }"
    >
      <!-- loop可循环轮播,autoplay可自动播放 -->
      <swiper-slide v-for="item in listData" :style="item.bgColor">{{
        item.companyname
      }}</swiper-slide>
    </swiper>
  </div>
</template>
最后别忘了 npm install swiper 安装一下依赖
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值