vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue element上传多张图片

vue实现element上传多张图片浏览删除功能

作者:小郑

这篇文章主要介绍了vue实现element上传多张图片浏览删除功能,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

vue原生实现element上传多张图片浏览删除

<div class="updata-component" style="width:100%;">
   <div class="demo-upload-box clearfix">
     <div class="demo-upload-image-box" v-if="imageUrlArr && imageUrlArr.length">
       <div class="demo-upload-image" v-for="(item,index) in imageUrlArr" :key="index">
         <img :src="item">
         <div class="demo-upload-box-cover">
          <!-- 点击删除 -->
           <i class="el-icon-delete" style="position: absolute;left: 30%;top: 80%;z-index:2;color:#fff;"
             @click="handleRemoves(index)"></i>
              <!-- 点击浏览 -->
             <i class="el-icon-zoom-in" @click="handleView(index)" style="position: absolute;left: 56%;top: 80%;z-index:2;color:#fff;"></i>
         </div>
       </div>
     </div>
     <div class="demo-upload-btn" v-show="isshowlng">
       <input ref="uploadInput" type="file" class="file" @change="handleSuccess">
       <i slot="default" class="el-icon-plus"></i>
       <input type="button" class="btn" @click="clickFile" />
     </div>
   </div>
   <!-- 查看大图 -->
   <el-dialog :visible.sync="dialogVisible" :modal-append-to-body="false">
       <img width="100%" :src="bigPicSrc" alt="">
     </el-dialog>
 </div>
data(){
	return{
		bigPicSrc: '',
        imageUrlArr: [],//页面展示url数组
        filesData: [],//file数组
        isshowlng:true,//判断上传图片按钮是否显示
	}
},
methods:{
	// 文件上传接收
    handleSuccess (e) {
      console.log('------',e)
      console.log('imgs.lenght',this.imgs.length)
      var lng=6-this.imgs.length
      console.log('lng',lng)
      let file = e.target
      for (let i = 0; i < file.files.length; i++) {
        this.imageUrlArr.push(window.URL.createObjectURL(file.files.item(i)))
        this.filesData.push(file.files[i])
      }
      console.log('this.filesData',this.filesData)
      console.log('this.filesData.length',this.filesData.length)
      if(this.filesData.length>=lng){
        this.isshowlng=false
      }else{
        this.isshowlng=true
      }
    },
    clickFile () {
      const input = this.$refs.uploadInput
      input.click()
    },
    // 删除上传的案例图
    handleRemoves (index) {
      console.log('删除')
      this.imageUrlArr.splice(index, 1)
      this.filesData.splice(index, 1)
      var lng=6;//限制最多上传6张
      if(this.filesData.length>=lng){
        this.isshowlng=false
      }else{
        this.isshowlng=true
      }
      this.$forceUpdate()
    },
    // 查看大图
    handleView (index) {
      console.log('查看大图')
      this.dialogVisible=true
      this.bigPicSrc = this.imageUrlArr[index]
    },
}
<style>
/* ------------------------- */
.demo-upload-image-box{
  height: 150px;
  /* width: 120px; */
  /* padding: 10px; */
  float: left;
}
.demo-upload-btn{
  width: 115px;
  height: 115px;
  background-color: #fbfdff;
  border: 1px dashed #c0ccda;
  border-radius: 6px;
  text-align: center;
  position: relative;
  float: left;
}
.demo-upload-image{
  width: 117px;
  height: 117px;
  margin-right: 5px;
  display: inline-block;
  position: relative;
}
.demo-upload-image img{
  width: 115px;
  height: 115px;
}
.big-pic{
  position: fixed;
    left: 40%;
    top: 20%;
}
.big-pic img{
  width: 400px;
  height: 300px;
}
.file {
  width: 115px;
  height: 115px;
  display: none;
}
.btn {
  position: absolute;
  top: 0;
  left: 0;
  width: 60px;
  height: 60px;
  background: rgba(0, 0, 0, 0);
  z-index: 10;
  border: none;
  cursor: pointer;
}
.demo-upload-btn .el-icon-plus{
  line-height: 110px;
  font-size: 16px;
  /* position: absolute;
    left: 40px; */
}
.el-icon-plus:before{
  font-size: 30px;
  color: #8c939d;
}
.demo-upload-box-cover{
  background: rgba(0,0,0,0.3);
  width: 100%;
  height: 100%;
  position:absolute;
  left:0;
  top:0;
  border-radius:5px;
}
</style>

到此这篇关于vue实现element上传多张图片浏览删除功能的文章就介绍到这了,更多相关vue element上传多张图片内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文