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:{ //獲取用戶(hù)詳情 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ì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
vue3如何添加eslint校驗(yàn)(eslint-plugin-vue)
這篇文章主要介紹了vue3如何添加eslint校驗(yàn)(eslint-plugin-vue),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01vue父子組件動(dòng)態(tài)傳值的幾種方式及注意問(wèn)題詳解
這篇文章主要介紹了vue父子組件動(dòng)態(tài)傳值的幾種方式及注意問(wèn)題詳解,需要的朋友可以參考下2022-12-12vue-cli多頁(yè)面應(yīng)用實(shí)踐之實(shí)現(xiàn)組件預(yù)覽項(xiàng)目
在最近的項(xiàng)目中遇到了一個(gè)需求,找了相關(guān)資料后終于實(shí)現(xiàn),這篇文章主要給大家介紹了關(guān)于vue-cli多頁(yè)面應(yīng)用實(shí)踐之實(shí)現(xiàn)組件預(yù)覽項(xiàng)目的相關(guān)資料,需要的朋友可以參考下2022-05-05Vue實(shí)現(xiàn)登錄功能全套超詳細(xì)講解(含封裝axios)
這篇文章主要給大家介紹了關(guān)于Vue實(shí)現(xiàn)登錄功能(含封裝axios)的相關(guān)資料,Vue是現(xiàn)在前端最流行的框架之一,作為前端開(kāi)發(fā)人員應(yīng)該要熟練的掌握它,需要的朋友可以參考下2023-10-10在Vue中使用xlsx組件實(shí)現(xiàn)Excel導(dǎo)出功能的步驟詳解
在現(xiàn)代Web應(yīng)用程序中,數(shù)據(jù)導(dǎo)出到Excel格式是一項(xiàng)常見(jiàn)的需求,Vue.js是一種流行的JavaScript框架,允許我們構(gòu)建動(dòng)態(tài)的前端應(yīng)用程序,本文將介紹如何使用Vue.js和xlsx組件輕松實(shí)現(xiàn)Excel數(shù)據(jù)導(dǎo)出功能,需要的朋友可以參考下2023-10-10詳解Vue+Element的動(dòng)態(tài)表單,動(dòng)態(tài)表格(后端發(fā)送配置,前端動(dòng)態(tài)生成)
這篇文章主要介紹了Vue+Element動(dòng)態(tài)表單動(dòng)態(tài)表格,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04