Vue頭像處理方案小結(jié)
個(gè)人思路
獲取后臺(tái)返回頭像url,判斷圖片寬度,高度。
如果寬度>高度, 使其高度填充盒子 兩邊留白。
如果寬度<高度,使得寬度填充盒子 上下留白。
效果圖:

缺陷:懶加載圖片 會(huì)出現(xiàn)閃爍

代碼實(shí)現(xiàn)
<template>
// 外面要給一個(gè)div并且限制寬度和高度,text-align center,overflow hidden
<div class="head">
// userInfoList.avatar 是后臺(tái)返回給我的頭像URL
<img v-lazy="userInfoList.avatar" id="userhead" alt=""/>
</div>
<div class="fl" v-for="(item, index) in matchList" :key="index">
<div class="heads">
<img v-lazy="item.adatar" class="headitem" alt=""/>
</div>
</div >
</template>
<script>
import { head, heads } from '@/assets/js/base' // 存放head,heads目錄引入
export default {
data(){
return {
listQuery:{
pg: 1,
ps: 10
}
},
methods:{
//獲取用戶詳情
getUserInfoList(){
getlist('mobile/user/pers/detail', funciton(res) {
if(data.code == ERR_OK){
_this.userInfoList = res.data
// 單個(gè)頭像處理,$nextTick處理去報(bào) 數(shù)據(jù)加載完成后 在進(jìn)行圖
_this.$nextTick(function () {
head(res.data.avatar, 'userhead')
})
// 下拉加載多個(gè)頭像處理
res.data.item.forEach((item, index) => {
if(_this.listQuery.pg>1){ // 下拉加載時(shí),頭像依然要進(jìn)行處理
let count = (10*(_this.listQuery.pg -1) + index)
_this.$nextTick(function () {
heads(item.adatar, count, 'headitem')
})
}else{
_this.$nextTick(function () {
heads(item.adatar, index, 'headitem')
})
}
}
_this.listQuery.pg++
}
})
}
assets文件js下的base.js
// 單個(gè)頭像處理
export function head (objUrl, id) {
let _userhead = document.getElementById(id)
if(_userhead){
if(objUrl){
let img = new Image()
img.src = objUrl
img.onload = function () {
let _width = img.width
let _height = img.height
if(_width >= _height){
_userhead.style.width = '100%'
}else{
_userhead.style.height = '100%'
}
}
}else{
_userhead.style.width = '100%'
}
}
}
// 多個(gè)頭像處理
export function heads (objUrl, index, className) {
let _heads = document.getElementsByClassName(className)[index]
if(_heads){
if(objUrl){
let img = new Image()
img.src = objUrl
img.onload = function () {
let _width = img.width
let _height = img.height
if(_width >= _height){
_heads.style.width = '100%'
}else{
_heads.style.height = '100%'
}
}
}else{
_heads.style.width = '100%'
}
}
}
總結(jié)
以上所述是小編給大家介紹的Vue頭像處理方案小結(jié),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vuex 如何動(dòng)態(tài)引入 store modules
這篇文章主要介紹了vuex 如何動(dòng)態(tài)引入 store modules,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
axios發(fā)送post請(qǐng)求springMVC接收不到參數(shù)的解決方法
下面小編就為大家分享一篇axios發(fā)送post請(qǐng)求springMVC接收不到參數(shù)的解決方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-03-03
vue阻止重復(fù)請(qǐng)求實(shí)現(xiàn)示例詳解
這篇文章主要為大家介紹了vue阻止重復(fù)請(qǐng)求實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Vue 中 toRefs() 和 toRef() 的使用方法
在 Vue 3 中,toRefs()可以將響應(yīng)式對(duì)象的屬性轉(zhuǎn)換為可響應(yīng)的 refs,主要用于在解構(gòu)響應(yīng)式對(duì)象時(shí),保持屬性的響應(yīng)性,這篇文章主要介紹了Vue 中 toRefs() 和 toRef() 的使用,需要的朋友可以參考下2025-01-01
Vue 后臺(tái)管理類項(xiàng)目兼容IE9+的方法示例
這篇文章主要介紹了Vue 后臺(tái)管理類項(xiàng)目兼容IE9+的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02

