vue.js 實(shí)現(xiàn)圖片本地預(yù)覽 裁剪 壓縮 上傳功能
以下代碼涉及 Vue 2.0 及 ES6 語法。
目標(biāo)
純 javascrpit 實(shí)現(xiàn),兼容ie9及以上瀏覽器,在本地做好文件格式、長寬、大小的檢測,減少瀏覽器交互。
現(xiàn)實(shí)是殘酷的,為了兼容Ie9 還是用上了 flash,第二篇來解釋解釋。
代碼結(jié)構(gòu)
<div id="wrap"> <label> 點(diǎn)我上傳圖片 <input type='file' @change="change" ref="input"> </label> <img :src="src" ref="img"> </div> new Vue({ el: '#wrap', data: { // 一張透明的圖片 src:'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7', elInput: null }, methods: { change(e){ // ... } } })
如何獲取圖片大小
現(xiàn)代瀏覽器中十分簡單
function getSize(e){ return e.target.files[0].size; }
但 ie9 下暫時(shí)沒有找到純 JS 的方案。
因此,在需要判斷大小時(shí),遇到 ie9 直接繞過不去判斷
如何預(yù)覽本地圖片
const refs = this.$refs const elInput = refs.input const elImg = refs.img
現(xiàn)代瀏覽器中,通過調(diào)用 FileReader 讀取本地圖片,將圖片地址轉(zhuǎn)成 Base64 格式實(shí)現(xiàn)預(yù)覽。
function getSrc(){ const reader = new FileReader(); reader.onload = (e) => { const src = e.target.result; elImg.src = src; }; if (elInput.files && elInput.files[0]) { reader.readAsDataURL(elInput.files[0]); } }
但是問題又來了,ie9 并不支持 FileReader,但可以通過 ie 濾鏡顯示。
function getSrc(){ elInput.select(); elInput.blur(); const src = document.selection.createRange().text; document.selection.empty(); elImg.style.filter = `progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod='scale',src='${src}')`; }
濾鏡中 sizingMethod='scale'
的寫法是為了圖片能適應(yīng)內(nèi)容縮放。
由于 IE9 對安全限制有所增強(qiáng),實(shí)踐中會(huì)遇到以下兩個(gè)問題:
如果 file 控件獲得焦點(diǎn),則 document.selection.createRange()
拒絕訪問,因此需要在 elInput.select()
后面加一句 elInput.blur()
即可。
為了讓上傳按鈕更美觀,默認(rèn)給 input[type=file]
的設(shè)置了樣式 visible:hidden
,這樣會(huì)導(dǎo)致 ie9 下報(bào)錯(cuò)。應(yīng)該會(huì)被瀏覽器認(rèn)為欺騙用戶點(diǎn)擊,只好曲線救國。
label{ overflow:hidden; } label input[type='file']{ position:absolute; top:9999px; left:9999px; }
如何獲取圖片長寬
同理,利用 ie 濾鏡和 FileReader 的方案對 ie9 和非 ie9 分別實(shí)現(xiàn)。
ie9 的方案
參數(shù) src 接受的是本地圖片路徑
let tempEl; const getSizeIncompatible = (src, callback) => { if (!tempEl) { tempEl = document.createElement('div'); tempEl.style.position = 'absolute'; tempEl.style.width = '1px'; tempEl.style.height = '1px'; tempEl.style.left = '-9999px'; tempEl.style.top = '-9999px'; tempEl.style.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=image)'; document.body.insertBefore(tempEl, document.body.firstChild); } tempEl.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = src; callback(tempEl.offsetWidth, tempEl.offsetHeight); };
其中 sizingMethod='image'
是為了圖片顯示原始大小。
非 ie9 方案
參數(shù) src 接受的是 base64 編碼后的圖片路徑
const getSize = (src, callback) => { const image = new Image(); image.onload = () => { callback(image.width, image.height); }; image.src = src; };
參考
https://elemefe.github.io/ima...
總結(jié)
以上所述是小編給大家介紹的vue.js 實(shí)現(xiàn)圖片本地預(yù)覽 裁剪 壓縮 上傳功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
圖文講解用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟
本次小編給大家?guī)淼氖顷P(guān)于用vue-cli腳手架創(chuàng)建vue項(xiàng)目步驟講解內(nèi)容,有需要的朋友們可以學(xué)習(xí)下。2019-02-02關(guān)于Vue.js一些問題和思考學(xué)習(xí)筆記(2)
這篇文章主要為大家分享了關(guān)于Vue.js一些問題和思考的學(xué)習(xí)筆記,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12vue 自定義組件 v-model雙向綁定、 父子組件同步通信的多種寫法
父子組件通信,都是單項(xiàng)的,很多時(shí)候需要雙向通信。這篇文章主要介紹了vue 自定義組件 v-model雙向綁定、 父子組件同步通信,需要的朋友可以參考下2017-11-11