vue头像加白边_Vue头像处理方案小结

个人思路

获取后台返回头像url,判断图片宽度,高度。

如果宽度>高度, 使其高度填充盒子 两边留白。

如果宽度

效果图:

ff163f522cfd5aa61f6fb1bb644ed18e.png

缺陷:懒加载图片 会出现闪烁

b5f3521541575cb4fe62e6b686a42017.png

代码实现

// 外面要给一个div并且限制宽度和高度,text-align center,overflow hidden

// userInfoList.avatar 是后台返回给我的头像URL

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

// 单个头像处理,$nextTick处理去报 数据加载完成后 在进行图

_this.$nextTick(function () {

head(res.data.avatar, 'userhead')

})

// 下拉加载多个头像处理

res.data.item.forEach((item, index) => {

if(_this.listQuery.pg>1){ // 下拉加载时,头像依然要进行处理

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

// 单个头像处理

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%'

}

}

}

// 多个头像处理

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%'

}

}

}

总结

以上所述是小编给大家介绍的Vue头像处理方案小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值