[element ui] 自定义 Carousel 走马灯组件

彩蛋:后台管理系统一站式平台模板

vue-next-admin,这是基于 vue3.x + CompositionAPI + typescript + vite + element plus + vue-router-next + next.vuex,适配手机、平板、pc 的后台开源免费模板库

1、效果图

在这里插入图片描述

2 代码

<template>
  <div class="upload">
    <el-carousel :autoplay="false" type="card" height="260px" indicator-position="none" ref="carousel" @change="onCarouselChange" v-loading="uploadLoading" element-loading-text="上传中...">
      <el-carousel-item v-for="(item, index) in uploadList" :key="index">
        <img :src="item" alt style="width: 100%;height: 260px;" />
      </el-carousel-item>
      <div class="no-img" v-if="uploadList.length <= 0">暂无图片,请选择图片进行上传!</div>
    </el-carousel>
    <div class="upload-warp">
      <div class="upload-warp-left" @click="handlePrev">
        <i class="el-icon-arrow-left"></i>
      </div>
      <div class="upload-warp-center">
        <div v-for="(item, index) in uploadList" :key="item" class="upload-warp-center-img">
          <img :src="item" alt :class="imgActiveIndex == index ? 'img-br' : ''" @click="handleImgClick(index)" />
          <i class="el-icon-close" @click="handleDelImg(index)"></i>
        </div>
        <el-upload :action="uploadUrl" :show-file-list="false" list-type="picture-card" :on-progress="handleAvatarProgress" :on-success="handleAvatarSuccess" :on-error="handleAvatarError" accept=".jpg, .jpeg, .png, .gif, .pdf, .doc, .docx, .xls, .xlsx, .JPG, .JPEG, .PBG, .GIF, .PDF, .DOC, .DOCX, .XLS, .XLSX" v-if="uploadList.length < 5">
          <i class="el-icon-plus"></i>
        </el-upload>
      </div>
      <div class="upload-warp-left" @click="handleNext">
        <i class="el-icon-arrow-right"></i>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "carouselUpload",
  data() {
    return {
      uploadList: [],
      imgActiveIndex: 0,
      uploadLoading: false,
      uploadUrl: "https://jsonplaceholder.typicode.com/posts/"
    };
  },
  methods: {
    // carousel 改变时
    onCarouselChange(index) {
      this.imgActiveIndex = index;
    },

    // 文件上传时的钩子
    handleAvatarProgress() {
      this.uploadLoading = true;
    },

    // 文件上传成功时的钩子
    handleAvatarSuccess(res, file) {
      this.uploadList.push(URL.createObjectURL(file.raw));
      this.uploadLoading = false;
      this.$message.success("图片上传成功");
    },

    // 文件上传失败时的钩子
    handleAvatarError() {
      this.$message.warning("图片上传失败");
      this.uploadLoading = false;
    },

    // 底部图片导航列表
    handleImgClick(index) {
      this.imgActiveIndex = index;
      this.$refs.carousel.setActiveItem(index);
    },

    // 底部图片导航列表当前项删除
    handleDelImg(index) {
      this.uploadList.splice(index, 1);
    },

    // 底部图片导航列表上一张
    handlePrev() {
      if (!this.uploadList.length) return;
      if (this.imgActiveIndex === 0) {
        this.imgActiveIndex = this.uploadList.length;
      }
      this.imgActiveIndex--;
      this.$refs.carousel.setActiveItem(this.imgActiveIndex);
    },

    // 底部图片导航列表下一张
    handleNext() {
      if (!this.uploadList.length) return;
      if (this.imgActiveIndex >= this.uploadList.length - 1) {
        this.imgActiveIndex = 0;
      } else {
        this.imgActiveIndex++;
      }
      this.$refs.carousel.setActiveItem(this.imgActiveIndex);
    }
  }
};
</script>

<style lang="scss" scoped>
.upload {
  .upload-warp {
    display: flex;
    height: 60px;
    margin-top: 20px;
    &-left {
      flex-basis: 36px;
      line-height: 60px;
      text-align: center;
      font-size: 28px;
      color: #cccccc;
      cursor: pointer;
      transition: all 0.3s ease;
      &:hover {
        color: gray;
        transition: all 0.3s ease;
      }
    }
    &-center {
      flex-basis: calc(100% - 72px);
      display: flex;
      &-img {
        width: 20%;
        position: relative;
        border-radius: 2px;
        margin: 0 4px 0;
        img {
          width: 100%;
          height: 60px;
          box-sizing: border-box;
          border-radius: 2px;
          border: 1px solid transparent;
        }
        .img-br {
          border: 1px solid #09f;
        }
        i {
          position: absolute;
          right: -5px;
          top: -10px;
          width: 20px;
          height: 20px;
          line-height: 20px;
          text-align: center;
          background: #09f;
          color: white;
          border-radius: 100%;
          cursor: pointer;
          transition: all 0.3s ease;
          opacity: 0;
        }
        &:hover i {
          opacity: 1;
          transition: all 0.3s ease;
        }
      }
    }
  }
}
</style>

<style scoped>
.upload-warp-center >>> .el-upload--picture-card {
  width: 60px;
  height: 60px;
  line-height: 60px;
}
.upload >>> .no-img {
  height: 260px;
  line-height: 260px;
  color: #c0c4cc;
  background: #f5f7fa;
  text-align: center;
}
</style>
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要实现Element Plus Carousel走马灯图片的自适应,你可以使用以下代码作为参考: ``` <template> <el-carousel :interval="5000" arrow="always" :height="bannerHeight + 'px'" style="border: 1px solid red;"> <el-carousel-item v-for="item in 4" :key="item"> <el-row :gutter="12"> <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" style="height:340px;margin-bottom: 20px;"> <img ref="bannerHeight" src="../image/banner1.png" alt="" @load="imgLoad" style="width: 100%"> </el-col> </el-row> </el-carousel-item> </el-carousel> <div id="test-div" style="border: 5px solid red;background-color: antiquewhite;">当前高度:{{bannerHeight}}</div> </template> <script> export default { data() { return { bannerHeight: 0, }; }, methods: { imgLoad() { const imgElement = this.$refs.bannerHeight; this.bannerHeight = imgElement.clientHeight; }, }, }; </script> ``` 这段代码中,我们使用`imgLoad`方法监听图片加载事件,通过`this.$refs.bannerHeight`获取图片元素的实际高度,然后将该高度赋值给`bannerHeight`变量。最后,在`el-carousel`的`:height`属性中使用`bannerHeight`作为高度值,实现了图片的自适应效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [【最简便方法】element-plus/element-ui走马灯配置图片以及图片自适应](https://blog.csdn.net/m0_62742402/article/details/127837417)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [element-ui走马灯实现图片自适应](https://blog.csdn.net/qq_40976321/article/details/104513316)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值