溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Vue如何實現頭像處理功能

發(fā)布時間:2022-11-22 09:22:56 來源:億速云 閱讀:136 作者:iii 欄目:開發(fā)技術

這篇文章主要介紹了Vue如何實現頭像處理功能的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇Vue如何實現頭像處理功能文章都會有所收獲,下面我們一起來看看吧。

代碼實現

<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處理去報 數據加載完成后 在進行圖
        _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的優(yōu)點

Vue具體輕量級框架、簡單易學、雙向數據綁定、組件化、數據和結構的分離、虛擬DOM、運行速度快等優(yōu)勢,Vue中頁面使用的是局部刷新,不用每次跳轉頁面都要請求所有數據和dom,可以大大提升訪問速度和用戶體驗。

關于“Vue如何實現頭像處理功能”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“Vue如何實現頭像處理功能”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

vue
AI