当前位置: 首页 > news >正文

element plus组件Carousel组件3个卡片平铺样式改造

1.设置二级卡片的缩放量为1

在这里插入图片描述

2.动态计算每个卡片的偏移量,

<script setup>
import { ref,useTemplateRef } from 'vue'

const carouselRef = useTemplateRef("carouselRef");

const activeIndex = ref(0);
// 监听轮播图切换事件
const handleCarouselChange = (index) => {
    activeIndex.value = index;
};

const processIndex = (index, activeIndex, length)=> {
    const lastItemIndex = length - 1;
    const prevItemIndex = activeIndex - 1;
    const nextItemIndex = activeIndex + 1;
    const halfItemIndex = length / 2;

    if (activeIndex === 0 && index === lastItemIndex) {
        return -1;
    } else if (activeIndex === lastItemIndex && index === 0) {
        return length;
    } else if (index < prevItemIndex && activeIndex - index >= halfItemIndex) {
        return length + 1;
    } else if (index > nextItemIndex && index - activeIndex >= halfItemIndex) {
        return -2;
    }
    return index;
}

const computeTranslate = (idx) => {
    const index = processIndex(idx, activeIndex.value, lhcardList.value.length);

    const inStage = Math.round(Math.abs(index - activeIndex.value)) <= 1;
    const parentWidth = 1725;  //容器宽度

    if (inStage) {
        return (parentWidth * ((2 - 1) * (index - activeIndex.value) + 1)) / 3;
    } else if (index < activeIndex.value) {
        return (-(1 + 1) * parentWidth) / 3;
    } else {
        return ((3 + 1) * parentWidth) / 3;
    }
};

// 动态计算卡片的偏移量
const getItemStyle = (index) => {
    const offset = computeTranslate(index);

    return {
        transform: `translateX(${offset}px) scale(1)`,
        transition: "transform 0.3s ease-in-out",
    };
};


</script>
<template>
    <el-carousel
                ref="carouselRef"
                :autoplay="false"
                height="313px"
                trigger="click"
                type="card"
                class="custom-carousel"
                @change="handleCarouselChange"
            >
                <el-carousel-item
                    v-for="(item, key) in cardList"
                    :key="item.id"
                    :style="getItemStyle(key)"
                >
                    <div class="el-carousel-box carousel-card">
                        <!--卡片内容-->
                    </div>
                </el-carousel-item>
            </el-carousel>
</template>
<style lang="scss" scoped>

</style>

相关文章:

  • docker nginx
  • A - 整数的简单问题/A - A Simple Problem with Integers
  • 《算法笔记》8.2小节——搜索专题->广度优先搜索(BFS)问题 E: 【宽搜入门】巧妙取量
  • 基于 Prometheus + Grafana 监控微服务和数据库
  • 职业教育五金建设改革解析
  • C# 一文读懂委托与事件
  • Web Component 教程(二):如何有效管理和使用自定义属性
  • ✎ 一次有趣的经历
  • Cross-Silo Prototypical Calibration for Federated Learning with Non-IID Data
  • 【操作系统安全】任务6:Linux 系统文件与文件系统安全 学习指南
  • 【项目合集】基于ESP32的智能化妆柜
  • Linux进程信号(上)
  • Python第五章03:函数返回值和None类型
  • 网络编程知识预备阶段
  • 东隆科技携手PRIMES成立中国校准实验室,开启激光诊断高精度新时代
  • 【免费】2004-2017年各地级市实际利用外资数据
  • Grokking System Design 系统设计面试问题
  • 从零开始实现一个HTML5飞机大战游戏
  • java 中散列表(Hash Table)和散列集(Hash Set)是基于哈希算法实现的两种不同的数据结构
  • 【渗透测试】webpack对于渗透测试的意义
  • 政府效率部效果不佳?马斯克有意寻求支持,含糊表态部门未来
  • 天津航空一航班盘旋14圈才降落,客服:因天气影响
  • 孙一凡的东欧狂想音乐会:一场穿越东欧的听觉绮梦
  • 商务部新闻发言人就中美经贸对话磋商情况答记者问
  • 增诉滥用职权罪,尹锡悦遭韩国检方追加起诉
  • 来上海喝云南咖啡!上海国际咖啡文化节助力咖啡产业破圈出海