使用element-plus实现轮播图和缩略图联动

使用的是element-plus的走马灯组件加一些脚本实现。

swiper是有这个deom的,算是仿swiper功能。没有搞无限轮播,无限轮播可以用swiper。

以下是效果:

下面是代码:

模板部分

<template>
  <div class="gallery">
    <div class="carousel">
      <el-carousel
        ref="carousel"
        :v-model="2"
        trigger="click"
        height="280px"
        indicator-position="none"
        :loop="false"
        :autoplay="false"
        @change="handleChange"
        >
        <el-carousel-item v-for="item in imgs" :key="item">
          <img :src="item" alt="" />
        </el-carousel-item>
      </el-carousel>
    </div>
    <div style="width: 280px;overflow: hidden;">
      <ul class="thumbs" :style="`transform: translateX(-${triggerOffsetX}px)`">
        <li v-for="(url, index) in imgs" :key="index"  @click="setCarouselItem(index)" :class="activeIndex === index ? 'active' : ''">
          <img :src="url" />
        </li>
      </ul>
    </div>
  </div>
</template>

脚本部分

<script setup lang="ts">
import { ref } from 'vue'
import type { CarouselInstance } from 'element-plus'

const carousel = ref<CarouselInstance | null>(null)
//换成你自己的图片list我这里是直接填充了
const imgs = ref(new Array(7).fill('/assets/img/op_1718863151.png'))
const triggerOffsetX = ref(0)
const activeIndex = ref(0)

const handleChange = (current: number) => {
    //保存轮播索引
  activeIndex.value = current
    //索引大于等于5就触发缩略模块滑动
 if(current >= 5) { 
    //5是缩略图最多可展示几张,48是通过缩略图的宽度和边距计算出,具体数值看自己需求
    triggerOffsetX.value = (imgs.value.length - 5 - 1) * 48 
    //小于5回到初始位置
 }else if(current < 5) {
  triggerOffsetX.value = 0
 }
}
    //点击缩略图同步轮播图索引
const setCarouselItem = (index: number) => {
  carousel.value?.setActiveItem(index)
}
</script>

样式部分


<style scoped lang="scss">
   .gallery {
      width: 100%;
      overflow: auto;
      .carousel{
        border-radius: 12px;
        width: 100%;
        height: 280px;
        overflow: hidden;
      }
      .el-carousel__arrow{
        width: 24px;
        height: 36px;
        border-radius: 12px;
      }
      .el-carousel__item{
        img{
          width: 100%;
          height: 100%;
        }
      }
      .thumbs{
        overflow: hidden;
        zoom: 1;
        width: 1000px;
        margin-top: 7px;
        transition: all 0.5s;
        padding: 1px 0;
        li{
          width: 40px;
          height: 40px;
          margin-right: 8px;
          float: left;
          border-radius: 6px;
          overflow: hidden;
          cursor: pointer;
          img{
            width: 100%;
            height: 100%;
          }
          &.active{
            outline: 1px solid #32dfe2;
          }
        }
      }
    }
</style>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值