基于 vue 實現圖片批量上傳效果

效果

aaa.gif

組件

<style scoped>
.image-wrapper{
  padding:10px 10px;
}
.inputer{
  width:100px;
  height:100px;
  display:none;
}
.img-wrapper{
  display:flex;
  flex-direction: row;
  justify-content: left;
  flex-wrap: wrap;
}
.image-wrapper .upload-icon{
  margin-left:14px;
  margin-bottom:10px;
  /* overflow: hidden; */
  width:20%;
  height:70px;
  text-align: center;
  border:1px solid #dbdbdb;
}
.img-wrapper .item{
  position:relative;
  margin-left:14px;
  margin-bottom:10px;
  /* overflow: hidden; */
  width:20%;
  height:70px;
  text-align: center;
}
.img-wrapper .item .dis-image{
  width:100%;
  height:70px;
}
.image-wrapper .item .delete {
  display:inline-block;
  position:absolute;
  background-color: #dbdbdb;
  width: 20px;
  height: 20px;
  color: #fff;
  font-size: 0.8em;
  border-radius: 50%;
  top:-9px;
  right:-10px;
}

.img-wrapper .item .heng{
  position: absolute;
  display:inline-block;
  width:30px;
  height:3px;
  background:#dbdbdb;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
} 
.img-wrapper .item .su{
  position: absolute;
  display:inline-block;
  width:3px;
  height:30px;
  background:#dbdbdb;
  left: 50%;
  top: 50%;
  transform: translate(-50%,-50%);
} 
</style>
<template>
  <div>
    <div class="image-wrapper">
      <div  class="img-wrapper">
        <div class="item" v-for="(item, index) in imgList"><img :src="item" class="dis-image"><span class="delete" @click="deleteImg(index)">X</span></div>
        <div class="item upload-icon" @click="preSelect" v-if="imgList.length < fileCount">
          <span class="heng"></span>
          <span class="su"></span>
        </div>
      </div>
      <input type="file" class="inputer" ref="inputRef" @change="handleChange" multiple="multiple" >
    </div>
    <alert v-model="showError" title="添加失敗">{{errorMsg}}</alert>
  </div>
</template>
<script>
import { Alert } from 'vux';
export default {
  components: {
    Alert
  },
  name: 'imgUpload',
  props: {
    fileMaxSize: {
      type: Number,
      required: false,
      default: 200
    },
    fileCount: {
      type: Number,
      required: false,
      default: 1
    },
    fileType: {
      type: String,
      required: false,
      default: 'image/png, image/jpeg'
    }
  },
  data () {
    return {
      imgList: [],
      showError: false,
      errorMsg: ''
    };
  },
  methods: {
    preSelect () {
      this.$refs.inputRef.click();
    },

    handleChange (e) {
      /* eslint-disable */
      let BreakException = {};

      let file = e.currentTarget.files;
      if (file.length > this.fileCount || this.imgList.length > this.fileCount) {
        this.showError = true;
        this.errorMsg = '最多上傳' + this.fileCount + '張圖片';
        return;
      }
      try {
        _.forEach(file, element => {
          if (!element.type || this.fileType.indexOf(element.type) === -1) {
            this.showError = true;
            this.errorMsg = '圖片類型錯誤';
            throw BreakException;
          } else if (element.size > this.fileMaxSize * 1024) {
            this.showError = true;
            this.errorMsg = '圖片太大請重新上傳';
            throw BreakException;
          }
        });
      } catch (error) {
        if (e !== BreakException) throw e;
      }

      _.forEach(file, element => {
        let freader = new FileReader();
        freader.readAsDataURL(element);
        freader.onload = (e) => {
          this.imgList.push(e.target.result)
        };
      });
    },

    deleteImg (ind) {
      this.imgList.splice(ind,1);
    }
  }
};
</script>


組件引入

image.png

最后,功能還在完善中,會不定時更新

對學習抱有熱情的開發(fā)小伙伴歡迎加入 qq群685421881,更歡迎熱愛編程的妹子進入,讓我們一起學習 并進步吧!

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容