欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue實現(xiàn)移動端圖片裁剪上傳功能

 更新時間:2020年08月18日 10:35:56   作者:echo008  
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)移動端圖片裁剪上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了vue移動端圖片裁剪上傳的具體代碼,供大家參考,具體內(nèi)容如下

1.安裝cropperjs依賴庫

npm install cropperjs

2.編寫組件SimpleCropper.vue

<template> 
 <div class="v-simple-cropper"> 
 <slot> 
 <button @click="upload">上傳圖片</button> 
 </slot> 
 <input class="file" ref="file" type="file" accept="image/*" @change="uploadChange"> 
 <div class="v-cropper-layer" ref="layer"> 
 <div class="layer-header"> 
 <button class="cancel" @click="cancelHandle">取消</button> 
 <button class="confirm" @click="confirmHandle">裁剪</button> 
 </div> 
 <img ref="cropperImg"> 
 </div> 
 </div> 
</template> 
 
<script> 
import Cropper from 'cropperjs' 
import 'cropperjs/dist/cropper.min.css' 
export default { 
 name: 'v-simple-cropper', 
 props: { 
 initParam: Object, 
 successCallback: { 
 type: Function, 
 default: () => {} 
 } 
 }, 
 data () { 
 return { 
 cropper: {}, 
 filename: '' 
 } 
 }, 
 mounted () { 
 this.init() 
 }, 
 methods: { 
 // 初始化裁剪插件 
 init () { 
 let cropperImg = this.$refs['cropperImg'] 
 this.cropper = new Cropper(cropperImg, { 
 aspectRatio: 1 / 1, 
 dragMode: 'move' 
 }) 
 }, 
 // 點擊上傳按鈕 
 upload () { 
 this.$refs['file'].click() 
 }, 
 // 選擇上傳文件 
 uploadChange (e) { 
 let file = e.target.files[0] 
 this.filename = file['name'] 
 let URL = window.URL || window.webkitURL 
 this.$refs['layer'].style.display = 'block' 
 this.cropper.replace(URL.createObjectURL(file)) 
 }, 
 // 取消上傳 
 cancelHandle () { 
 this.cropper.reset() 
 this.$refs['layer'].style.display = 'none' 
 this.$refs['file'].value = '' 
 }, 
 // 確定上傳 
 confirmHandle () { 
 let cropBox = this.cropper.getCropBoxData() 
 let scale = this.initParam['scale'] || 1 
 let cropCanvas = this.cropper.getCroppedCanvas({ 
 width: cropBox.width * scale, 
 height: cropBox.height * scale 
 }) 
 let imgData = cropCanvas.toDataURL('image/jpeg') 
 let formData = new window.FormData() 
 formData.append('fileType', this.initParam['fileType']) 
 formData.append('img', imgData) 
 formData.append('signId', this.$localStorage('signId')) 
 formData.append('originalFilename', this.filename) 
 window.$axios(this.initParam['uploadURL'], formData, { 
 method: 'post', 
 headers: {'Content-Type': 'multipart/form-data'} 
 }).then(res => { 
 this.successCallback(res.data) 
 this.cancelHandle() 
 }) 
 } 
 } 
} 
</script> 
 
<style lang="less"> 
.v-simple-cropper { 
 .file { 
 display: none; 
 } 
 .v-cropper-layer { 
 position: fixed; 
 top: 0; 
 bottom: 0; 
 left: 0; 
 right: 0; 
 background: #fff; 
 z-index: 99999; 
 display: none; 
 .layer-header { 
 position: absolute; 
 top: 0; 
 left: 0; 
 z-index: 99999; 
 background: #fff; 
 width: 100%; 
 height: .8rem; 
 padding: 0 .2rem; 
 box-sizing: border-box; 
 } 
 .cancel, 
 .confirm { 
 line-height: .8rem; 
 font-size: .28rem; 
 background: inherit; 
 border: 0; 
 outline: 0; 
 float: left; 
 } 
 .confirm { 
 float: right; 
 } 
 img { 
 position: inherit!important; 
 border-radius: inherit!important; 
 float: inherit!important; 
 } 
 } 
} 
</style> 

3.引用組件

<template> 
 <simple-cropper :initParam="uploadParam" :successCallback="uploadHandle" ref="cropper"> 
 <img :src="userImg" @click="upload"> 
 </simple-cropper> 
</template> 
 
<script> 
import SimpleCropper from '@/components/SimpleCropper' 
export default { 
 name: 'info', 
 data () { 
 return { 
 uploadParam: { 
 fileType: 'recruit', // 其他上傳參數(shù) 
 uploadURL: this.$dataURL + 'uploadAction/qcloudImg', // 上傳地址 
 scale: 4 // 相對手機屏幕放大的倍數(shù): 4倍 
 }, 
 userImg: this.$dataURL + 'test.png' 
 } 
 }, 
 methods: { 
 // 上傳頭像 
 upload () { 
 this.$refs['cropper'].upload() 
 }, 
 // 上傳頭像成功回調(diào) 
 uploadHandle (data) { 
 if (data.state === 'SUCCESS') { 
 this.userImg = this.form.headImgUrl = data.fileId 
 } 
 } 
 }, 
 components: { 
 SimpleCropper 
 } 
} 
</script> 

4.示例圖

關(guān)于vue.js組件的教程,請大家點擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

更多vue學(xué)習(xí)教程請閱讀專題《vue實戰(zhàn)教程》

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • vue實現(xiàn)拖拽進(jìn)度條

    vue實現(xiàn)拖拽進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)拖拽進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-03-03
  • Element InfiniteScroll無限滾動的具體使用方法

    Element InfiniteScroll無限滾動的具體使用方法

    這篇文章主要介紹了Element InfiniteScroll無限滾動的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • vue 如何開啟或關(guān)閉熱更新

    vue 如何開啟或關(guān)閉熱更新

    這篇文章主要介紹了vue 如何開啟或關(guān)閉熱更新,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 利用vue + koa2 + mockjs模擬數(shù)據(jù)的方法教程

    利用vue + koa2 + mockjs模擬數(shù)據(jù)的方法教程

    這篇文章主要給大家介紹了關(guān)于利用vue + koa2 + mockjs模擬數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • vue2響應(yīng)式的缺點影響

    vue2響應(yīng)式的缺點影響

    這篇文章主要介紹了vue2響應(yīng)式的缺點影響,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • Vue分頁組件實現(xiàn)過程詳解

    Vue分頁組件實現(xiàn)過程詳解

    Web應(yīng)用程序中資源分頁不僅對性能很有幫助,而且從用戶體驗的角度來說也是非常有用的。在這篇文章中,將了解如何使用Vue創(chuàng)建動態(tài)和可用的分頁組件
    2022-12-12
  • vue中實現(xiàn)上傳文件給后臺實例詳解

    vue中實現(xiàn)上傳文件給后臺實例詳解

    在本文里小編給大家分享了一篇關(guān)于vue中實現(xiàn)上傳文件給后臺的實例內(nèi)容,有需要此功能的可以學(xué)習(xí)參考下。
    2019-08-08
  • vue父組件點擊觸發(fā)子組件事件的實例講解

    vue父組件點擊觸發(fā)子組件事件的實例講解

    下面小編就為大家分享一篇vue父組件點擊觸發(fā)子組件事件的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • vue項目Luckysheet的使用

    vue項目Luckysheet的使用

    這篇文章主要介紹了vue項目Luckysheet的使用,目前Luckysheet不支持使用npm安裝包,所以只能使用CDN引入依賴,在vue項目的public/index.html文件里引入即可,本文通過示例代碼給大家詳細(xì)介紹,需要的朋友可以參考下
    2022-08-08
  • vue:左右過渡展開折疊的組件

    vue:左右過渡展開折疊的組件

    在網(wǎng)上找了好久關(guān)于左右過渡動畫折疊的組件,沒有合適的代碼,效果類似于element UI中的Drawer抽屜組件,只不過ele中的都是懸浮的組件,工作中遇到的很多都是占用空間的展開折疊,網(wǎng)上很多也是上下展開收起的組件,于是就自己寫了一個,分享給大家,感興趣的朋友參考下吧
    2023-11-11

最新評論