vue3 实现多组商品横向滚动(多组商品轮播)

11 篇文章 0 订阅
实现功能: 点击按钮切换,每页5个商品,当最后商品不满一页时 切换剩余的商品,如果切换到第一个则禁用左边按钮,如果切换到最后一个禁用右边按钮

在这里插入图片描述

实现方式: 这里我们使用 vue-awesome-swiper 默认安装的最新版本 已经支持vue3和ts
  1. 安装
npm install swiper vue-awesome-swiper --save 或者 yarn add swiper vue-awesome-swiper
  1. 引入插件 这里我使用的是全局引入(main.js里面引入插件)
// 引入vue-awesome-swiper
import SwiperClass, { Pagination, Grid, Mousewheel } from 'swiper'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css'
import 'swiper/css/pagination'
// use swiper modules
SwiperClass.use([Pagination, Grid, Mousewheel])
  1. 组件里就可以使用这个插件了
<script setup lang="ts">
import { ref } from 'vue'
import SwiperClass from 'swiper'
// vertical swiper
let vSwiperRef: SwiperClass | null = null
const setVSwiperRef = (swiper: SwiperClass) => {
  vSwiperRef = swiper
}
let vSwiperIndex = ref<number>(0)
const updateVSwiperIndex = () => {
  vSwiperIndex.value = vSwiperRef?.activeIndex || 0
}
// 切换到上一页
const prevVSwiperSlide = () => vSwiperRef?.slidePrev()
// 切换到下一页
const nextVSwiperSlide = () => vSwiperRef?.slideNext()
</script>

<template>
  <div class="box">
    <div class="arrow">
      <a
        href="javascript:;"
        @click="prevVSwiperSlide"
        :class="{ disabled: vSwiperIndex === 0 }"
        class="iconfont prev"
        >&lt;</a
      >
      <a
        href="javascript:;"
        :class="{ disabled: vSwiperIndex === 5 }"
        @click="nextVSwiperSlide"
        class="iconfont next"
        >&gt;</a
      >
    </div>
    <swiper
      :slides-per-view="5"
      :slides-per-group="5"
      :space-between="14"
      wrapper-tag="div"
      @swiper="setVSwiperRef"
      @slide-change="updateVSwiperIndex"
    >
      <swiper-slide v-for="item in 10" :key="item">
        {{ item }}
      </swiper-slide>
    </swiper>
  </div>
</template>

<style lang="less" scoped>
.box {
  width: 1240px;
  margin: 0 auto;
}
.arrow {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 4px;
}
.iconfont {
  width: 20px;
  height: 20px;
  background: #ccc;
  color: #fff;
  display: inline-block;
  text-align: center;
  margin-left: 5px;
  background: @xtxColor;
  &::before {
    font-size: 12px;
    position: relative;
    top: -2px;
  }
  &.disabled {
    background: #ccc;
    cursor: not-allowed;
  }
}
:deep(.swiper-slide) {
  height: 280px;
  background: yellowgreen;
  text-align: center;
  line-height: 280px;
}
</style>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!下面是一个用Vue 3编写的公告横向滚动的示例代码: ```html <template> <div class="announcement"> <div class="announcement-container"> <ul ref="list" class="announcement-list"> <li v-for="(item, index) in announcements" :key="index" class="announcement-item"> {{ item }} </li> </ul> </div> </div> </template> <script> import { ref, onMounted, watch } from 'vue'; export default { name: 'Announcement', data() { return { announcements: [ '公告1', '公告2', '公告3', '公告4', '公告5' ], interval: null, currentIndex: 0, scrollWidth: 0 }; }, mounted() { onMounted(() => { this.scrollWidth = this.$refs.list.scrollWidth; this.startScroll(); }); }, methods: { startScroll() { this.interval = setInterval(() => { this.currentIndex++; if (this.currentIndex >= this.announcements.length) { this.currentIndex = 0; this.$refs.list.scrollLeft = 0; } else { this.$refs.list.scrollLeft += this.scrollWidth; } }, 3000); } }, beforeUnmount() { clearInterval(this.interval); } }; </script> <style scoped> .announcement { width: 100%; overflow: hidden; } .announcement-container { width: 100%; overflow: hidden; } .announcement-list { display: flex; list-style-type: none; padding: 0; margin: 0; animation: scroll 20s infinite linear; } .announcement-item { flex: 0 0 auto; white-space: nowrap; margin-right: 20px; } @keyframes scroll { from { transform: translateX(0); } to { transform: translateX(-100%); } } </style> ``` 在这个示例中,我们使用了Vue的`ref`、`onMounted`和`watch`函数来处理滚动逻辑。`announcements`数组中存储了公告内容,`currentIndex`表示当前显示的公告索引,`scrollWidth`存储了滚动容器的宽度。 在组件的`mounted`生命周期钩子中,我们获取了滚动容器的宽度,并调用`startScroll`函数开始滚动。 `startScroll`函数使用`setInterval`定时器来每隔一段时间更新当前公告索引,并通过修改滚动容器的`scrollLeft`属性实现滚动效果。当滚动到最后一个公告时,重新设置索引为0,并将滚动容器的`scrollLeft`重置为0。 最后,在组件的`beforeUnmount`生命周期钩子中清除了定时器,防止内存泄漏。 你可以根据需要自定义样式和公告内容。希望这个示例能帮到你!

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值