el-image改造,添加下载方法

项目中有个需求,图片预览的时候需要旋转并下载,市面上的插件并没有提供此方法,于是我便改造封装了一下。

组件

<template>
  <el-image
    :style="'width:' + width + '; height: ' + height"
    :src="src"
    :preview-src-list="imglist"
    :z-index="zIndex"
    :fit="fit"
    :alt="alt"
    :referrerPolicy="referrerPolicy"
    @click="clickimg"
  ></el-image>
</template>

 js部分

export default {
  name: "imageprint",
  data() {
    return {};
  },
  props: {
    src: {
      type: String,
      default: "",
    },
    imglist: {
      type: Array,
      default: function () {
        return [];
      },
    },
    width: {
      type: String,
      default: "100px",
    },
    height: {
      type: String,
      default: "100px",
    },
    fit: {
      type: String,
      default: "",
    },
    alt: {
      type: String,
      default: "",
    },
    referrerPolicy: {
      type: String,
      default: "",
    },
    zIndex: {
      type: Number,
      default: 3000,
    },
  },
  methods: {
    clickimg() {
      this.$nextTick(() => {
        let wrapper = document.getElementsByClassName(
          "el-image-viewer__actions__inner"
        );
        let p = document.createElement("i");
        p.setAttribute("class", "el-icon-printer");
        wrapper[0].appendChild(p);
        if (wrapper.length > 0) {
          this.wrapperElem = wrapper[0];
          this.clickHandler();
        }
      });
    },
    clickHandler() {
      this.wrapperElem.addEventListener("click", this.printBtn);
    },
    printBtn(e) {
      let className = e.target.className;
      if (className === "el-icon-printer") {
        document.getElementsByClassName("el-image-viewer__canvas")[0].children[0].setAttribute("id", "printimg");
        this.printImage();
      }
    },
    printImage() {

			let iframe = document.createElement("iframe");
      let doc = null;
      iframe.setAttribute("style","position:absolute;width:0px;height:0px;left:-500px;top:-500px")
      document.body.appendChild(iframe);
      doc = iframe.contentWindow.document;
      let img = document.getElementById("printimg")
			let clnimg = img.cloneNode(true)

      doc.body.appendChild(clnimg);
      doc.close();
      iframe.contentWindow.focus();
      iframe.contentWindow.print();
      setTimeout(() => {
        document.body.removeChild(iframe);
      }, 1000);
    },
  },
};

引用

import imageprint from '@/components/imageprint'
 components: {imageprint}
<imageprint :src="tem.link" :imglist="imglist"></imageprint>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值