Vue頭像處理方案小結(jié)
個人思路
獲取后臺返回頭像url,判斷圖片寬度,高度。
如果寬度>高度, 使其高度填充盒子 兩邊留白。
如果寬度<高度,使得寬度填充盒子 上下留白。
效果圖:
缺陷:懶加載圖片 會出現(xiàn)閃爍
代碼實現(xiàn)
<template> // 外面要給一個div并且限制寬度和高度,text-align center,overflow hidden <div class="head"> // userInfoList.avatar 是后臺返回給我的頭像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 // 單個頭像處理,$nextTick處理去報 數(shù)據(jù)加載完成后 在進行圖 _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%' } } }
總結(jié)
以上所述是小編給大家介紹的Vue頭像處理方案小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue3如何添加eslint校驗(eslint-plugin-vue)
這篇文章主要介紹了vue3如何添加eslint校驗(eslint-plugin-vue),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01vue-cli多頁面應(yīng)用實踐之實現(xiàn)組件預(yù)覽項目
在最近的項目中遇到了一個需求,找了相關(guān)資料后終于實現(xiàn),這篇文章主要給大家介紹了關(guān)于vue-cli多頁面應(yīng)用實踐之實現(xiàn)組件預(yù)覽項目的相關(guān)資料,需要的朋友可以參考下2022-05-05Vue實現(xiàn)登錄功能全套超詳細(xì)講解(含封裝axios)
這篇文章主要給大家介紹了關(guān)于Vue實現(xiàn)登錄功能(含封裝axios)的相關(guān)資料,Vue是現(xiàn)在前端最流行的框架之一,作為前端開發(fā)人員應(yīng)該要熟練的掌握它,需要的朋友可以參考下2023-10-10在Vue中使用xlsx組件實現(xiàn)Excel導(dǎo)出功能的步驟詳解
在現(xiàn)代Web應(yīng)用程序中,數(shù)據(jù)導(dǎo)出到Excel格式是一項常見的需求,Vue.js是一種流行的JavaScript框架,允許我們構(gòu)建動態(tài)的前端應(yīng)用程序,本文將介紹如何使用Vue.js和xlsx組件輕松實現(xiàn)Excel數(shù)據(jù)導(dǎo)出功能,需要的朋友可以參考下2023-10-10詳解Vue+Element的動態(tài)表單,動態(tài)表格(后端發(fā)送配置,前端動態(tài)生成)
這篇文章主要介紹了Vue+Element動態(tài)表單動態(tài)表格,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04