vue 原生图片展示 限制同时展示数量

原因是 ElementUI 没有下面这种模式.

效果如下, 细节可以自己完善
在这里插入图片描述

<template>
  <div class="carousel-container">
    <div class="carousel-wrapper">
      <div class="carousel-slide" v-for="(image, index) in displayedImages" :key="index">
        <img :src="image" />
      </div>
    </div>
    <div class="carousel-arrow carousel-arrow-left" v-if="currentIndex > 0" @click="prev"></div>
    <div class="carousel-arrow carousel-arrow-right" v-if="currentIndex < images.length - 3" @click="next"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      images: [
        "https://via.placeholder.com/300x200/ff0000/ffffff",
        "https://via.placeholder.com/300x200/00ff00/ffffff",
        "https://via.placeholder.com/300x200/0000ff/ffffff",
        "https://via.placeholder.com/300x200/ffff00/000000",
        "https://via.placeholder.com/300x200/ff00ff/ffffff",
      ],
      currentIndex: 0,
    };
  },
  methods: {
    prev() {
      this.currentIndex--;
    },
    next() {
      this.currentIndex++;
    },
  },
  computed: {
    displayedImages() {
      return this.images.slice(this.currentIndex, this.currentIndex + 3);
    },
  },
};
</script>

<style>
.carousel-container {
  position: relative;
  width: 900px;
  height: 200px;
  overflow: hidden;
  margin: 0 auto;
}

.carousel-wrapper {
  display: flex;
  transition: transform 0.5s ease;
}

.carousel-slide {
  width: 300px;
  height: 200px;
  margin-right: 20px;
}

.carousel-slide:last-child {
  margin-right: 0;
}

.carousel-arrow {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 40px;
  height: 40px;
  background-color: rgba(0, 0, 0, 0.5);
  color: #fff;
  border-radius: 50%;
  text-align: center;
  line-height: 40px;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.carousel-arrow-left {
  left: 0;
}

.carousel-arrow-right {
  right: 0;
}

.carousel-arrow:hover {
  background-color: rgba(0, 0, 0, 0.8);
}

.carousel-arrow i {
  font-size: 20px;
}
</style>
Vue 中上传图片和文件可以使用第三方组件库,如 element-ui 和 iview,也可以使用原生的 HTML5 文件上传功能。以下是使用 element-ui 的上传组件实现上传图片和文件的示例代码: ```html <template> <div> <el-upload class="upload-demo" action="/api/upload" :on-success="handleSuccess" :before-upload="beforeUpload" :file-list="fileList" :limit="3" :auto-upload="false" list-type="picture-card" > <i class="el-icon-plus"></i> <div class="el-upload__text">将文件拖到此处,或点击上传</div> <div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div> </el-upload> </div> </template> <script> export default { data() { return { fileList: [] }; }, methods: { handleSuccess(response, file) { console.log(response); console.log(file); }, beforeUpload(file) { const isJPG = file.type === 'image/jpeg' || file.type === 'image/png'; const isLt500K = file.size / 1024 < 500; if (!isJPG) { this.$message.error('上传头像图片只能是 JPG/PNG 格式!'); } if (!isLt500K) { this.$message.error('上传头像图片大小不能超过 500KB!'); } return isJPG && isLt500K; } } }; </script> <style> .upload-demo { display: inline-block; margin-right: 10px; } </style> ``` 在这个示例中,我们使用了 element-ui 的上传组件,设置了上传地址、上传成功回调函数、上传前的处理函数、上传文件列表、上传文件数量限制、是否自动上传等属性。同时,我们还设置了上传文件的展示方式为图片卡片,并且限制了上传文件的类型和大小。 你可以根据自己的需求和使用的组件库进行相应的配置和实现。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值