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

Vue3中實現(xiàn)選取頭像并裁剪

 更新時間:2023年04月10日 09:12:15   作者:@胡海龍  
這篇文章主要詳細介紹了在vue3中如何選取頭像并裁剪,文章中有詳細的代碼示例,需要的朋友可以參考閱讀

在一個項目中,尤其是個人中心功能中,免不了要有設置頭像的功能,設置時為了最后展示的時候美觀好看,一般我們會采取提前截取用戶想要的部分,截取時支持放大,縮小,旋轉等操作,本文將描述如何在Vue3中實現(xiàn)點擊選取圖片并裁剪出想要的頭像后設置頭像。

最終效果

安裝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>標簽來進行使用,需要注意的是它需要有外層容器包裹,并且給外層容器設置指定的高度。

<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數(shù)據(jù)。
  • 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,但是原生的樣式不是我們想要的,演示中我是點擊一個相機的小圖標后選擇圖片,這個實現(xiàn)過程如下:

  1. 首先將 中加入hidden屬性,將它隱藏
  2. 然后當點擊相機圖標時通過js實現(xiàn)點擊input的事件
  3. 在input標簽中定義change事件,進行圖片轉base64的邏輯
  4. 然后將轉換的數(shù)據(jù)設置到全局變量中

完整的代碼實現(xiàn)

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" style="text-align:right;">
                    <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中實現(xiàn)選取頭像并裁剪的文章就介紹到這了,更多相關Vue3實現(xiàn)頭像并裁剪內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Vue項目設置可以局域網訪問

    Vue項目設置可以局域網訪問

    這篇文章主要介紹了Vue項目設置可以局域網訪問,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vue用復選框實現(xiàn)組件且支持單選和多選操作方式

    vue用復選框實現(xiàn)組件且支持單選和多選操作方式

    這篇文章主要介紹了vue用復選框實現(xiàn)組件且支持單選和多選操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • 解決antd 下拉框 input [defaultValue] 的值的問題

    解決antd 下拉框 input [defaultValue] 的值的問題

    這篇文章主要介紹了解決antd 下拉框 input [defaultValue] 的值的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-10-10
  • vue渲染時閃爍{{}}的問題及解決方法

    vue渲染時閃爍{{}}的問題及解決方法

    v-if和v-show可能是日常開發(fā)中最常用的兩個指令,雖然看上去兩者功能是類似的,但是兩者還是存在很大區(qū)別的。接下來通過本文給大家分享vue渲染時閃爍{{}}的問題及解決方法,感興趣的朋友一起看看吧
    2018-03-03
  • VueRouter路由模式全面解析

    VueRouter路由模式全面解析

    這篇文章主要介紹了VueRouter路由模式的用法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • Vue如何提升首屏加載速度實例解析

    Vue如何提升首屏加載速度實例解析

    這篇文章主要介紹了Vue如何提升首屏加載速度實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • Vue通知提醒框(Notification)圖文詳解

    Vue通知提醒框(Notification)圖文詳解

    最近有個項目需求就是在客戶端的右上角要實時展示提醒消息,下面這篇文章主要給大家介紹了關于Vue通知提醒框(Notification)的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-11-11
  • Vue3中使用匿名函數(shù)的方法實現(xiàn)

    Vue3中使用匿名函數(shù)的方法實現(xiàn)

    Lambda函數(shù),也稱為匿名函數(shù),是Vue3中的一種函數(shù)類型,綁定匿名方法和綁定普通方法主要是寫法上的區(qū)別,其他的區(qū)別并不是很大,本文主要介紹了Vue3中使用匿名函數(shù)的方法實現(xiàn),感興趣的可以了解一下
    2023-12-12
  • vue中input標簽上傳本地文件或圖片后獲取完整路徑的解決方法

    vue中input標簽上傳本地文件或圖片后獲取完整路徑的解決方法

    本文給大家介紹vue中input標簽上傳本地文件或圖片后獲取完整路徑,如E:\medicineOfCH\stageImage\xxx.jpg,本文給大家分享完美解決方案,感興趣的朋友跟隨小編一起看看吧
    2023-04-04
  • 關于Elementui中toggleRowSelection()方法實現(xiàn)分頁切換時記錄之前選中的狀態(tài)

    關于Elementui中toggleRowSelection()方法實現(xiàn)分頁切換時記錄之前選中的狀態(tài)

    這篇文章主要介紹了關于Elementui中toggleRowSelection()方法實現(xiàn)分頁切換時記錄之前選中的狀態(tài),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論

石景山区| 邳州市| 水富县| 宁明县| 平定县| 驻马店市| 徐汇区| 临沧市| 黄平县| 龙岩市| 阜南县| 章丘市| 桐梓县| 梅州市| 定西市| 东港市| 客服| 皋兰县| 彩票| 云阳县| 邛崃市| 榆树市| 沙雅县| 南岸区| 卢氏县| 馆陶县| 惠东县| 吉木萨尔县| 公主岭市| 徐州市| 定安县| 嘉善县| 遵义县| 新闻| 太湖县| 和硕县| 淳安县| 靖西县| 磴口县| 长岛县| 吴旗县|