圖片上傳及壓縮:vue+vux

1.html部分

<template>
  <div class="screen">
    <section class="uploader">
      <span>上傳身份證照片(最多可上傳兩張):</span>
      <div class="finish_room">
        <div class="finish_room2">
          <div
            v-for="(item ,index ) in imgs_report.imgSrc1"
            class="room_img"
            @click="changeBig"
            :key="index"
          >
            <img :src="item" class="img">
            <span @click.stop="delete_img_repo(index)">
              <img src="http://img.life.cntaiping.com/lxjk/TPDroad/second/img/del.png" alt>
            </span>
          </div>
          <div class="room_add_img" v-show="isAdd2">
            <span>
              <img src="http://img.life.cntaiping.com/lxjk/TPDroad/second/img/add_img.png">
            </span>
            <input
              v-if="isEvent"
              @change="add_img_repo"
              id="upload2"
              type="file"
              accept="image/png, image/jpg"
              multiple="multiple"
            >
          </div>
          <div class="zoom_img" @touchmove.prevent v-show="isBig" @click.stop="stop">
            <img :src="item_big" alt width="300" height="300">
          </div>
        </div>
      </div>
    </section>
    <!-- 4.0 彈窗 -->
    <attention v-if="showWindow" :information="information"></attention>
    <!-- 7.0 正在上傳 -->
    <div v-transfer-dom>
      <loading :show="showOrNot" :text="text"></loading>
    </div>
  </div>
</template>複製代碼

2.js部分
css

<script>
import attention from "../common/attention.vue";
import lrz from "lrz";
import { Loading, TransferDom } from "vux";
export default {
  components: {
    Loading,
    attention
  },
  directives: {
    TransferDom
  },
  data() {
    return {
      showOrNot: false,
      text: "正在上傳...",
      isEvent: true,
      showWindow: false,
      isAdd2: true,
      imgs_report: { imgSrc1: [], imgSrc2: [] },
      imgs_report_list: [],
      isBig: false,
      item_big: ""
    };
  },
  methods: {
    timeOut() {
      setTimeout(() => {
        this.showWindow = false;
      }, 1500);
    },
    // 上傳函數
    uploadImg(arr, num) {
      let uploaderList = event.target.files;
      var id = event.target.id;
      if (uploaderList.length > num) {
        this.showWindow = true;
        this.information = "您上傳的圖片數超出" + num + "張,請從新上傳。";
        this.timeOut();
      } else {
        for (var i = 0; i < uploaderList.length; i++) {
          var img = event.target.files[i];
          this.showOrNot = true;
          lrz(img, {
            quality: 0.4
          }).then(rst => {
            if (arr.imgSrc1.length < num) {
              arr.imgSrc1.push(rst.base64);
              arr.imgSrc2.push(rst.file);
            }
            if (arr.imgSrc1.length > num) {
              arr.imgSrc1.map((item, index) => {
                if (index > num - 1) {
                  arr.imgSrc1.splice(num - 1, arr.imgSrc1.length - num);
                  arr.imgSrc2.splice(num - 1, arr.imgSrc1.length - num);
                }
              });
            }
            if (arr.imgSrc1.length == num) {
              this.isAdd2 = false;
            }
          });
        }
      }
    },
    // 刪除身份證照片
    delete_img_repo(index) {
      this.imgs_report.imgSrc1.splice(index, 1);
      if (this.imgs_report.imgSrc1.length < 2) {
        this.isAdd2 = true;
      }
      this.imgs_report.imgSrc2.splice(index, 1);
    },
    // 上傳身份證照片
    add_img_repo(event) {
      this.uploadImg(this.imgs_report, 2);
    },
    changeBig(e) {
      this.item_big = e.target.currentSrc;
      this.isBig = true;
      this.isEvent = false;
    },
    stop() {
      this.isBig = false;
      this.isEvent = true;
    }
  },
  mounted() {
    this.$controlShare("hideOptionMenu");
  },
  updated() {
    //判斷圖片是否加載完畢
    var that = this;
    $(".img").load(function() {
      that.showOrNot = false;
    });
  }
};
</script>複製代碼

3.css部分
html

<style lang="less" scoped>
@import "../../common/css/var.css";
.screen {
  width: 100%;
  min-height: 100%;
  background: #f4f4f4;
  .uploader {
    padding: 0.26rem 0 0 0.3rem;
    background: #fff;
    .finish_room {
      width: 100%;
      height: auto;
    }
    .finish_room2 {
      width: 100%;
      height: auto;
      padding-bottom: 0.3rem;
      display: flex;
      align-items: center;
      flex-wrap: wrap;
    }
    .finish_room2 .room_img {
      margin-top: 0.3rem;
      width: 27%; // flex: 1;
      height: 1.76rem;
      margin-right: 0.4rem;
      position: relative; // overflow: hidden;
      border: 1px solid #ccc;
    }
    .finish_room2 .room_img img {
      width: 100%;
      height: 100%;
    }
    .finish_room2 > .room_img span {
      position: absolute;
      width: auto;
      height: auto;
      top: -0.26rem;
      right: -0.28rem;
    }
    .finish_room2 > .room_img span i {
      font-size: 40px;
    }
    .room_add_img {
      margin-top: 0.3rem;
      width: 1.7rem;
      height: 1.7rem;
      border: 1px solid #e1e1e1;
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: space-between;
      position: relative;
    }
    .room_add_img > span:nth-child(1) {
      margin-top: 0.3rem;
      width: 1rem;
      height: 1rem;
      overflow: hidden;
      text-align: center;
      img {
        margin: 0 auto;
      }
    }
    .room_add_img > span:nth-child(2) {
      margin-bottom: 0.2rem;
    }
    .room_add_img input {
      position: absolute;
      top: 0px;
      left: 0px;
      width: 100%;
      height: 100%;
      opacity: 0;
    }
    .zoom_img {
      width: 100%;
      height: 100%;
      margin: 0 auto;
      display: flex;
      display: -webkit-flex;
      align-items: center;
      justify-content: center;
      -webkit-justify-content: center;
      position: fixed;
      left: 0;
      top: 0;
      background: rgba(0, 0, 0, 0.5);
    }
  }
}
</style>
複製代碼

4.頁面效果
vue

相關文章
相關標籤/搜索