最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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' 
 }) 
 }, 
 // 點(diǎn)擊上傳按鈕 
 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 // 相對手機(jī)屏幕放大的倍數(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組件的教程,請大家點(diǎn)擊專題vue.js組件學(xué)習(xí)教程進(jìn)行學(xué)習(xí)。

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

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

相關(guān)文章

  • 關(guān)于element-ui的隱藏組件el-scrollbar的使用

    關(guān)于element-ui的隱藏組件el-scrollbar的使用

    這篇文章主要介紹了關(guān)于element-ui的隱藏組件el-scrollbar的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • vue3封裝簡易的vue-echarts問題

    vue3封裝簡易的vue-echarts問題

    這篇文章主要介紹了vue3封裝簡易的vue-echarts問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • 使用elementUI實現(xiàn)將圖片上傳到本地的示例

    使用elementUI實現(xiàn)將圖片上傳到本地的示例

    今天小編就為大家分享一篇使用elementUI實現(xiàn)將圖片上傳到本地的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • vant-ui組件庫中如何修改NavBar導(dǎo)航欄的樣式

    vant-ui組件庫中如何修改NavBar導(dǎo)航欄的樣式

    這篇文章主要介紹了vant-ui組件庫中如何修改NavBar導(dǎo)航欄的樣式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • Electron+vue從零開始打造一個本地播放器的方法示例

    Electron+vue從零開始打造一個本地播放器的方法示例

    這篇文章主要介紹了Electron+vue從零開始打造一個本地播放器的方法示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-10-10
  • vue3+TS 實現(xiàn)自定義指令長按觸發(fā)綁定的函數(shù)

    vue3+TS 實現(xiàn)自定義指令長按觸發(fā)綁定的函數(shù)

    這篇文章主要介紹了vue3+TS實現(xiàn)自定義指令長按觸發(fā)綁定的函數(shù),文中給大家分享了編寫自定義指令時遇到的幾個難點(diǎn),本文結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • Vue3中Cesium地圖初始化及地圖控件配置方法

    Vue3中Cesium地圖初始化及地圖控件配置方法

    本文中,我們主要介紹Cesium在Vue3運(yùn)行環(huán)境的配置,及Cesium實例中控件的顯隱設(shè)置,本項目基于pnpm安裝,也可使用其他包管理器進(jìn)行安裝,如npm或yarn,本文通過示例代碼對vue初始化Cesium地圖相關(guān)知識介紹的非常詳細(xì),需要的朋友參考下吧
    2023-07-07
  • vue 返回上一頁,頁面樣式錯亂的解決

    vue 返回上一頁,頁面樣式錯亂的解決

    今天小編就為大家分享一篇vue 返回上一頁,頁面樣式錯亂的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-11-11
  • vue.js與后臺數(shù)據(jù)交互的實例講解

    vue.js與后臺數(shù)據(jù)交互的實例講解

    今天小編就為大家分享一篇vue.js與后臺數(shù)據(jù)交互的實例講解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-08-08
  • vue中 router.beforeEach() 的用法示例代碼

    vue中 router.beforeEach() 的用法示例代碼

    導(dǎo)航守衛(wèi)主要是通過跳轉(zhuǎn)或取消的方式守衛(wèi)導(dǎo)航,本文通過示例代碼講解vue中 router.beforeEach() 的用法,感興趣的朋友跟隨小編一起看看吧
    2023-12-12

最新評論

喜德县| 黔西县| 金堂县| 唐海县| 清涧县| 五原县| 高尔夫| 信阳市| 屯昌县| 星子县| 翼城县| 龙岩市| 新余市| 蕲春县| 南皮县| 济源市| 皮山县| 西乌珠穆沁旗| 清苑县| 台前县| 昂仁县| 永嘉县| 于都县| 天柱县| 金塔县| 焉耆| 乌拉特前旗| 陵川县| 集贤县| 大悟县| 晋中市| 剑河县| 太仆寺旗| 文登市| 东阿县| 额济纳旗| 寻乌县| 南投县| 合水县| 铁力市| 延边|