最終效果
安裝VueCropper元件
yarn add vue-cropper@next
上面的安裝值針對Vue3的,如果時Vue2或想使用其他的方式引用,請訪問它的npm官方地址:官方教程。
在元件中引用
使用時也很簡單,只需要引入對應的元件和它的樣式文件,我這裡沒有在全域引用,只在我的元件檔案中引入
<script> import { userInfoByRequest } from '../js/api' import { VueCropper } from 'vue-cropper' import 'vue-cropper/dist/index.css' export default { components: { VueCropper }, }
然後用<vue-cropper></vue-cropper>
標籤來進行使用,需要注意的是它需要有外層容器包裹,並且給外層容器設定指定的高度。
<el-dialog title="头像设置" v-model="showSetAvatarDialog"> <div class="cropperBox"> <vue-cropper ref="cropper" :canMoveBox="false" :img="avatarBase64" :fixedBox="true" :autoCrop="true" autoCropWidth="200" autoCropHeight="200" outputType="png"></vue-cropper> </div> <div class="optionBtn"> <el-button @click="rotateLeft"><i class="fa fa-rotate-left"></i>左旋转</el-button> <el-button @click="rotateRight"><i class="fa fa-rotate-right"></i>右旋转</el-button> <el-button @click="getPickAvatar" type="primary"><i class="fa fa-save"></i>保存</el-button> </div> </el-dialog>
我這裡是將它放到一個Dialog彈框中,當選擇完圖片後會彈出這彈框。
關於它的一些屬性可以參考:官方教學中的詳細描述,上面用到的屬性意義:
canMoveBox:截圖框能否移動,預設是可以的,我設定為false,因為我想讓圖片移動,截圖框不動。
img:圖片來源,可以是圖片網址也可以是base64資料。
fixedBox:不允許改變截圖框的大小,預設是可以的,這裡我設定為了true,因為我想要一個固定長寬的方塊。
autoCrop:是否預設產生截圖框,這裡設定為了true
autoCropWidth:自動產生截圖框的預設寬度
autoCropHeight:自動產生截圖框的預設高度
#outputType:輸出圖片的格式,這裡我設定為了png格式
#取得圖片內容
透過下面這種方式,使用它內建的方法來取得截取圖片的內容
//获取base64格式的截图内容 this.$refs.cropper.getCropData(data => { // do something console.log(data) }) //获取blob格式的截图内容 this.$refs.cropper.getCropBlob(data => { // do something console.log(data) })
更多其他的內建方法請參考官方教學中的詳細描述。
自訂上傳圖片
截圖元件有了,下面需要我們選擇一個圖片,然後透過前端轉換為base64後彈出截圖的元件進行選擇,這裡用原生的input標籤,type為file,但是原生的樣式不是我們想要的,演示中我是點擊一個相機的小圖標後選擇圖片,這個實現過程如下:
首先將中加入hidden屬性,將它隱藏
然後當點擊相機圖示時透過js實作點擊input的事件
在input標籤中定義change事件,進行圖片轉base64的邏輯
然後將轉換的資料設定到全域變數中
##UserCenter .vue
<template> <div class="userCenter"> <input id="avatarFile" type="file" hidden @change="selectFile" /> <header> <div class="avatar"> <div class="back"> <el-tooltip content="返回"> <i @click="back" class="fa fa-arrow-left"></i> </el-tooltip> </div> <div class="topInfo"> <el-avatar :size="120" :src="avatarBlob==null?(userInfo.avatar==null?defaultAvatar:userInfo.avatar):avatarBlob"></el-avatar> <div class="setAvatar"> <el-tooltip content="修改头像"> <i @click="getFile" class="fa fa-camera"></i> </el-tooltip> </div> <div class="username">{{userInfo.nickName}}</div> </div> </div> </header> <el-row justify="center"> <el-col :lg="12" :xl="12" :md="18" :sm="20" :xs="20"> <div class="userInfoBox"> <div class="headerOption"> <el-tooltip content="修改用户基本信息,涉及账号的修改请点击底栏的操作按钮"> <el-link @click="editUserInfo"><i class="fa fa-edit"></i></el-link> </el-tooltip> </div> <ul> <li> <div class="userInfoTitle">用户名:</div> <div class="userInfoValue">{{userInfo.username}}</div> </li> <li> <div class="userInfoTitle">邮箱:</div> <div class="userInfoValue">{{userInfo.email}}</div> </li> <li> <div class="userInfoTitle">昵称:</div> <div class="userInfoValue">{{userInfo.nickName}}</div> </li> <li> <div class="userInfoTitle">性别:</div> <div class="userInfoValue">{{userInfo.gender==3?'保密':(userInfo.gender==0?'男':'女')}}</div> </li> <li> <div class="userInfoTitle">年龄:</div> <div class="userInfoValue">{{userInfo.age}}</div> </li> <li> <div class="userInfoTitle">生日:</div> <div class="userInfoValue">{{userInfo.birthday}}</div> </li> </ul> </div> </el-col> </el-row> <div class="option"> <el-button class="optionBtn" size="large" round><i class="fa fa-user"></i>用户名修改</el-button> <el-button class="optionBtn" size="large" round><i class="fa fa-lock"></i>修改密码</el-button> <el-button class="optionBtn" size="large" round><i class="fa fa-envelope"></i>修改邮箱</el-button> <el-button class="optionBtn" size="large" round><i class="fa fa-toggle-on"></i>激活邮箱</el-button> <el-button class="optionBtn" size="large" round><i class="fa fa-power-off"></i>注销账号</el-button> </div> <el-dialog title="基本信息" v-model="showUserInfoDialog"> <el-form :model="userInfoForm"> <el-form-item label="昵称"> <el-input placeholder="请输入昵称" v-model="userInfoForm.nickName"></el-input> </el-form-item> <el-form-item label="性别"> <el-select v-model="userInfoForm.gender"> <el-option v-for="item in genders" :key="item.id" :label="item.genderName" :value="item.genderCode"></el-option> </el-select> </el-form-item> <el-form-item label="生日"> <el-date-picker v-model="userInfoForm.birthday" :locale="locale" placeholder="选择出生日期" value-format="yyyy-MM-dd"></el-date-picker> </el-form-item> <div class="submitBtn" > <el-button @click="showUserInfoDialog=false" type="info">取消</el-button> <el-button type="primary">确定</el-button> </div> </el-form> </el-dialog> <el-dialog title="头像设置" v-model="showSetAvatarDialog"> <div class="cropperBox"> <vue-cropper ref="cropper" :canMoveBox="false" :img="avatarBase64" :fixedBox="true" :autoCrop="true" autoCropWidth="200" autoCropHeight="200" outputType="png"></vue-cropper> </div> <div class="optionBtn"> <el-button @click="rotateLeft"><i class="fa fa-rotate-left"></i>左旋转</el-button> <el-button @click="rotateRight"><i class="fa fa-rotate-right"></i>右旋转</el-button> <el-button @click="getPickAvatar" type="primary"><i class="fa fa-save"></i>保存</el-button> </div> </el-dialog> </div> </template> <script> import { userInfoByRequest } from '../js/api' import { VueCropper } from 'vue-cropper' import 'vue-cropper/dist/index.css' export default { components: { VueCropper }, data() { return { userInfo: {}, defaultAvatar: 'https://cube.elemecdn.com/9/c2/f0ee8a3c7c9638a54940382568c9dpng.png', userInfoForm: {}, genders: [ { id: 1, genderName: '男性', genderCode: 0, }, { id: 2, genderName: '女性', genderCode: 1, }, { id: 3, genderName: '保密', genderCode: 3, } ], showUserInfoDialog: false, showSetAvatarDialog: false, avatarBase64:'', avatarBlob: null } }, methods: { back() { this.$router.go(-1) }, getUserInfo() { userInfoByRequest().then(res => { this.userInfo = res.data; }) }, editUserInfo() { this.userInfoForm.nickName = this.userInfo.nickName; this.userInfoForm.gender = this.userInfo.gender; this.userInfoForm.birthday = this.userInfo.birthday; this.showUserInfoDialog = true; }, getPickAvatar() { this.$refs.cropper.getCropData(data => { this.avatarBlob = data }) this.showSetAvatarDialog = false; }, rotateLeft() { this.$refs.cropper.rotateLeft(); }, rotateRight() { this.$refs.cropper.rotateRight(); }, getFile() { let fileEle = document.getElementById('avatarFile') fileEle.click() }, selectFile(e) { var file = e.target.files[0]; var filesize = file.size; var filename = file.name; var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = (e) => { var imgcode = e.target.result; this.avatarBase64 = imgcode this.showSetAvatarDialog = true; } } }, mounted() { this.getUserInfo(); } } </script> <style scoped> .userCenter { display: flex; flex-direction: column; justify-content: center; } .username { color: #fff; font-weight: bold; font-size: 19px; padding: 0 10px; } header { text-align: center; padding: 10px; background-color: #333; } .topInfo { animation: userInfoCard 2s; -webkit-animation: userInfoCard 2s; /* Safari and Chrome */ } .back { text-align: left; color: #fff; position: relative; left: 10px; } .back>i { cursor: pointer; } .userInfoBox { box-shadow: 2px 3px 10px #D3d7d4; margin-top: 20px; padding: 20px; background-color: #fff; font-size: 16px; text-align: center; border-radius: 4px; animation: userInfoCard 3s; -webkit-animation: userInfoCard 3s; /* Safari and Chrome */ } .setAvatar { position: relative; top: -20px; font-size: 14px; color: #000; margin: -10px; } .setAvatar>i { cursor: pointer; } ul>li { display: flex; padding: 5px; } .userInfoTitle { font-weight: bold; width: 100px; text-align: right; padding-right: 10px; } .option { width: 100%; text-align: center; position: fixed; bottom: 20px; border: 1px solid gainsboro; padding: 10px; } .optionBtn { font-weight: bold; text-align: center; margin-top: 10px; } .headerOption { text-align: right; } @keyframes userInfoCard { from { opacity: 0; } to { opacity: 1; } } @-webkit-keyframes userInfoCard /* Safari and Chrome */ { from { opacity: 0; } to { opacity: 1; } } .cropperBox { width: 100%; height: 300px; } </style>
以上是Vue3中怎麼實現選取頭像並裁剪的詳細內容。更多資訊請關注PHP中文網其他相關文章!

NetflixusesAcustomFrameworkcalled“ Gibbon” BuiltonReact,notReactorVuedIrectly.1)TeamSperience:selectBasedonFamiliarity.2)ProjectComplexity:vueforsimplerprojects:reactforforforproproject,reactforforforcompleplexones.3)cocatizationneedneeds:reactoffipicatizationneedneedneedneedneedneeds:reactoffersizationneedneedneedneedneeds:reactoffersizatization needefersmoreflexibleise.4)

Netflix在框架選擇上主要考慮性能、可擴展性、開發效率、生態系統、技術債務和維護成本。 1.性能與可擴展性:選擇Java和SpringBoot以高效處理海量數據和高並發請求。 2.開發效率與生態系統:使用React提升前端開發效率,利用其豐富的生態系統。 3.技術債務與維護成本:選擇Node.js構建微服務,降低維護成本和技術債務。

Netflix主要使用React作為前端框架,輔以Vue用於特定功能。 1)React的組件化和虛擬DOM提升了Netflix應用的性能和開發效率。 2)Vue在Netflix的內部工具和小型項目中應用,其靈活性和易用性是關鍵。

Vue.js是一種漸進式JavaScript框架,適用於構建複雜的用戶界面。 1)其核心概念包括響應式數據、組件化和虛擬DOM。 2)實際應用中,可以通過構建Todo應用和集成VueRouter來展示其功能。 3)調試時,建議使用VueDevtools和console.log。 4)性能優化可通過v-if/v-show、列表渲染優化和異步加載組件等實現。

Vue.js適合小型到中型項目,而React更適用於大型、複雜應用。 1.Vue.js的響應式系統通過依賴追踪自動更新DOM,易於管理數據變化。 2.React採用單向數據流,數據從父組件流向子組件,提供明確的數據流向和易於調試的結構。

Vue.js適合中小型項目和快速迭代,React適用於大型複雜應用。 1)Vue.js易於上手,適用於團隊經驗不足或項目規模較小的情況。 2)React的生態系統更豐富,適合有高性能需求和復雜功能需求的項目。

實現 Vue 中 a 標籤跳轉的方法包括:HTML 模板中使用 a 標籤指定 href 屬性。使用 Vue 路由的 router-link 組件。使用 JavaScript 的 this.$router.push() 方法。可通過 query 參數傳遞參數,並在 router 選項中配置路由以進行動態跳轉。

Vue 中實現組件跳轉有以下方法:使用 router-link 和 <router-view> 組件進行超鏈接跳轉,指定 :to 屬性為目標路徑。直接使用 <router-view> 組件顯示當前路由渲染的組件。使用 router.push() 和 router.replace() 方法進行程序化導航,前者保存歷史記錄,後者替換當前路由不留記錄。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

SublimeText3 Linux新版
SublimeText3 Linux最新版

Atom編輯器mac版下載
最受歡迎的的開源編輯器

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)