vue3+element plus组件库中el-carousel组件走马灯特效,当图片变动时下面数字也随着图片动态变化

文章描述了使用Vue.js构建的一个网页应用,包含HTML部分的图片和标题,以及使用JavaScript中的Vue响应式特性管理数据和图片轮播。JS部分展示了如何导入图片和处理回收垃圾数量的动态显示,CSS则负责样式设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.效果图

2.html

<section style="height:30%">
                        <div class="left-img1-title">
                            <img src="../assets/img/title.png"
                                 alt=""
                                 srcset="">
                            <div class="text">回收垃圾数量</div>
                        </div>
                        <div style="">
                            <el-carousel :interval="4000"
                                         indicator-position="none"
                                         type="card"
                                         height="100px"
                                         @change="swch">
                                <el-carousel-item v-for="item in carouselImg"
                                                  :key="item">
                                    <h3 text="2xl"
                                        justify="center"><img :src="item.img"
                                             alt=""
                                             srcset=""
                                             class="carousel-image"
                                             style="height: 100px;"
                                             justify="center"></h3>
                                </el-carousel-item>
                            </el-carousel>
                        </div>
                        <div class="carousel-text">
                            <div v-for="(item,index) in count"
                                 :key="index">
                                <div>{{item.name}}</div>
                                <div class="text">{{item.data}}kg</div>
                            </div>
                        </div>
                    </section>

3.js 

 // 准备数据 
// 1.vue3采用import的方式引入图片,不然会报错,引入方法之一
    import bl from '@/assets/img/bl.png'
    import js from '@/assets/img/js.png'
    import sl from '@/assets/img/sl.png'
    import zl from '@/assets/img/zl.png'
    import zw from '@/assets/img/zw.png'
    import pie from '@/components/pie.vue' 
 //2.本页面采用第二种方式,结合html代码
 // 引入本地图片
    let carouselImg = reactive([
        {img: bl},
        {img: js},
        {img: sl},
        {img: zl},
        {img: zw},
    ])
 //回收列表
    let countList = reactive([])
    //回收垃圾数量
    let count = reactive([
            { name: '今日回收数量', data: 0 },
            { name: '累计回收总数', data: 0 },
            { name: '户均回收量'  , data: 0 }
        ])
// 填充回收量
    const getNum = (data) => {
            let list = [1,2,3,4,5]
            let allCount = 0
            let allDay = 0
            data.map(item=> {
                allCount += (item.weight*100)
                allDay += (item.weightDay*100)
                item.typeId=="1026"?list[0]=item:''
                item.typeId=="4"?list[1]=item:''
                item.typeId=="2"?list[2]=item:''
                item.typeId=="1017"?list[3]=item:''
                item.typeId=="1121"?list[4]=item:''
            })
            allCount = (allCount/100).toFixed(2)
            allDay = (allDay/100).toFixed(2)

            countList = list
            // 回收量初始化
            count[0].data = list[0].weightDay
            count[1].data = list[0].weight
            count[2].data = list[0].households
        }

        // 幻灯片切换(回收量切换)
        const  swch = (index) => {
            let list = countList
            count[0].data = list[index].weightDay
            count[1].data = list[index].weight
            count[2].data = list[index].households
        }

4.css

.left-img1-title-two {
					width: 27vw;
					margin: 0 1.875rem;
					.content {
						display: flex;
						justify-content: space-around;
						margin-bottom: 1.25rem;
						.bg1 {
							width: 14.375rem;
							height: 6.25rem;
							background: url('../assets/img/1.png') no-repeat;
							background-size: 14.375rem 6.25rem;
							display: flex;
							justify-content: space-around;
							align-items: center;
							text-align: center;
							img {
								width: 3.125rem;
								height: 3.125rem;
							}
							.shou {
								color: white;
							}
							.text {
								color: aqua;
							}
						}
						.bg2 {
							width: 14.375rem;
							height: 6.25rem;
							background: url('../assets/img/2.png') no-repeat;
							background-size: 14.375rem 6.25rem;
							display: flex;
							justify-content: space-around;
							align-items: center;
							text-align: center;
							img {
								width: 3.125rem;
								height: 3.125rem;
							}
							.shou {
								color: white;
							}
							.text {
								color: aqua;
							}
						}
					}
					.content1 {
						display: flex;
						justify-content: space-around;
						margin-bottom: 1.875rem;
						.bg1 {
							width: 14.375rem;
							height: 6.25rem;
							background: url('../assets/img/3.png') no-repeat;
							background-size: 14.375rem 6.25rem;
							display: flex;
							justify-content: space-around;
							align-items: center;
							text-align: center;
							img {
								width: 3.125rem;
								height: 3.125rem;
							}
							.shou {
								color: white;
							}
							.text {
								color: aqua;
							}
						}
						.bg2 {
							width: 14.375rem;
							height: 6.25rem;
							background: url('../assets/img/4.png') no-repeat;
							background-size: 14.375rem 6.25rem;
							display: flex;
							justify-content: space-around;
							align-items: center;
							text-align: center;
							img {
								width: 3.125rem;
								height: 3.125rem;
							}
							.shou {
								color: white;
							}
							.text {
								color: aqua;
							}
						}
					}
				}

### 如何在 Vue3 中使用 Element Plus 实现包含图片Carousel 组件 为了实现在 Vue3 项目中利用 Element Plus 的 `el-carousel` 和 `el-carousel-item` 来构建一个可以展示多张图片走马灯效果,下面提供了一个具体的实现方法。 #### HTML 结构定义 通过 `<template>` 标签来编写模板部分,在这里引入了 `el-carousel` 并设置了高度属性为 "500px"。接着循环渲染四个 `el-carousel-item` 子项,每一个子项内部放置了一张相同大小的图片作为演示[^1]。 ```html <template> <div class="home-banner"> <el-carousel height="500px"> <el-carousel-item v-for="(item, index) in items" :key="index"> <!-- 假设每一张图片都有不同的 URL --> <img :src="item.imageUrl" alt=""/> </el-carousel-item> </el-carousel> </div> </template> ``` #### JavaScript 部分配置 在脚本区域声明数据对象 `items` 数组用于存储各个幻灯片的信息,比如这里的 `imageUrl` 属性表示要显示的图像链接地址。这使得可以通过修改数组中的元素轻松更改或扩展轮播的内容而无需改动其他地方的代码逻辑。 ```javascript <script setup lang="ts"> import { ref } from &#39;vue&#39;; const items = ref([ { imageUrl: &#39;http://yjy-xiaotuxian-dev.oss-cn-beijing.aliyuncs.com/picture/2021-04-15/6d202d8e-bb47-4f92-9523-f32ab65754f4.jpg&#39; }, // 添加更多 item 对象以增加更多的轮播图... ]); </script> ``` #### 样式调整建议 考虑到样式可能会影响最终呈现的效果,如果遇到任何视觉上的问题,如图片变形等问题,则应该检查是否有不当使用的 CSS 转换(transform)样式作用于 `el-image` 或者其父级容器上,并考虑移除这些可能导致异常表现的样式设置[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值