国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解

瀏覽:183日期:2022-06-01 14:17:04
目錄
  • 正文
  • 方法
  • 復(fù)制文本
  • 復(fù)制圖片

正文

復(fù)制文字和圖片是我們經(jīng)常會(huì)使用到的需求,我們這篇文章主要介紹使用navigator.clipboard.write()來實(shí)現(xiàn)復(fù)制文字和圖片。不過這個(gè)屬性是需要考慮瀏覽器的兼容性的,可以參考MDN

document.execCommand('copy')

在很久之前我們是使用document.execCommand('copy')來實(shí)現(xiàn)復(fù)制文本的,但是現(xiàn)在mdn上已經(jīng)將這個(gè)命令廢棄了。

當(dāng)一個(gè) HTML 文檔切換到設(shè)計(jì)模式時(shí),document暴露 execCommand 方法,該方法允許運(yùn)行命令來操縱可編輯內(nèi)容區(qū)域的元素。如果傳入copy命令,那么就能實(shí)現(xiàn)復(fù)制的功能。

比如像下面這樣

  // 復(fù)制文字
  copyText(link, cb) {
    let input = document.createElement("textarea");
    input.style.cssText = "position: absolute; top: 0; left: 0; opacity: 0; z-index: -10;";
    input.value = link;
    document.body.appendChild(input);
    input.select();
    document.execCommand("copy");
    document.body.removeChild(input);
    if (typeof cb === "function") {
      cb();
    }
  }

Clipboard

Clipboard 接口實(shí)現(xiàn)了 Clipboard API,如果用戶授予了相應(yīng)的權(quán)限,其就能提供系統(tǒng)剪貼板的讀寫訪問能力。在 Web 應(yīng)用程序中,Clipboard API 可用于實(shí)現(xiàn)剪切、復(fù)制和粘貼功能。

方法

Clipboard提供了以下方法,方便我們讀取剪切板的內(nèi)容。

  • read():從剪貼板讀取數(shù)據(jù)(比如圖片),返回一個(gè) Promise對(duì)象。在檢索到數(shù)據(jù)后,promise 將兌現(xiàn)一個(gè) ClipboardItem對(duì)象的數(shù)組來提供剪切板數(shù)據(jù)。
  • readText():從操作系統(tǒng)讀取文本;返回一個(gè) Promise,在從剪切板中檢索到文本后,promise 將兌現(xiàn)一個(gè)包含剪切板文本數(shù)據(jù)的 DOMString
  • write(): 寫入任意數(shù)據(jù)至操作系統(tǒng)剪貼板。這是一個(gè)異步操作,在操作完成后,返回的 promise 的將被兌現(xiàn)。
  • writeText(): 寫入文本至操作系統(tǒng)剪貼板。返回一個(gè) Promise,在文本被完全寫入剪切板后,返回的 promise 將被兌現(xiàn)。

復(fù)制文本

復(fù)制文本的方法很簡(jiǎn)單,就是使用navigator.clipboard.writeText()方法。

具體代碼實(shí)現(xiàn)如下:

copyTextToClipboard(text) {
  return new Promise((resolve, reject) => {
    navigator.clipboard.writeText(text).then(
      () => {
resolve(true)
      },
      () => {
reject(new Error("復(fù)制失敗"))
      }
    )
  })
}

復(fù)制圖片

復(fù)制圖片主要用到navigator.clipboard.write()方法。 因?yàn)槲覀冊(cè)陧撁嬷型ǔJ且鶕?jù)一個(gè)img元素復(fù)制圖片,主要實(shí)現(xiàn)思路如下:

  • 先將img元素轉(zhuǎn)成base64
  • 再將base64轉(zhuǎn)成blob對(duì)象
  • 根據(jù)blob對(duì)象new一個(gè)ClipboardItem對(duì)象
  • 最后再根據(jù)navigator.clipboard.writeText()將內(nèi)容寫入剪貼板中

具體代碼實(shí)現(xiàn)如下:

  // 圖片元素轉(zhuǎn)base64
  getBase64Image(img) {
    let canvas = document.createElement("canvas");
    canvas.width = img.width;
    canvas.height = img.height;
    let ctx = canvas.getContext("2d");
    ctx?.drawImage(img, 0, 0, img.width, img.height);
    let dataURL = canvas.toDataURL("image/png");
    return dataURL;
  },
  // base64圖片轉(zhuǎn)為blob
  getBlobImage(dataurl) {
    let arr = dataurl.split(",");
    let mime = arr[0].match(/:(.*?);/)[1];
    let bstr = atob(arr[1]);
    let n = bstr.length;
    let u8arr = new Uint8Array(n);
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n);
    }
    return new Blob([u8arr], { type: mime });
  },
  // 復(fù)制圖片
  copyImage(dom, cb) {
    let dataurl = this.getBase64Image(dom);
    let blob = this.getBlobImage(dataurl);
    navigator.clipboard.write([
      new window.ClipboardItem({
[blob.type]: blob
      })
    ]).then(function() {
      if (typeof cb === "function") {
cb();
      }
    }, function() {
      console.log("圖片復(fù)制失敗!");
    });
  }

以上就是vue實(shí)現(xiàn)復(fù)制文字復(fù)制圖片實(shí)例詳解的詳細(xì)內(nèi)容,更多關(guān)于vue復(fù)制文字圖片的資料請(qǐng)關(guān)注其它相關(guān)文章!

標(biāo)簽: JavaScript
主站蜘蛛池模板: 云南省| 交口县| 张家界市| 沛县| 靖远县| 泰来县| 平果县| 黑河市| 永春县| 通化市| 永胜县| 大余县| 桂阳县| 广宗县| 吴旗县| 石台县| 鄂托克前旗| 长春市| 滨州市| 黑龙江省| 固原市| 平武县| 家居| 东城区| 贺兰县| 乌海市| 巴楚县| 馆陶县| 拉孜县| 灌阳县| 永新县| 宁夏| 台山市| 宣汉县| 丽水市| 仪征市| 皋兰县| 阜康市| 高碑店市| 神农架林区| 莲花县|