Vant Image 圖片

2022-05-31 11:56 更新

介紹

增強(qiáng)版的 img 標(biāo)簽,提供多種圖片填充模式,支持圖片懶加載、加載中提示、加載失敗提示

引入

import Vue from 'vue';
import { Image } from 'vant';

Vue.use(Image);

代碼演示

基礎(chǔ)用法

基礎(chǔ)用法與原生img標(biāo)簽一致,可以設(shè)置src、width、height、alt等原生屬性

<van-image
  width="100"
  height="100"
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>

填充模式

通過(guò)fit屬性可以設(shè)置圖片填充模式,可選值見(jiàn)下方表格

<van-image
  width="10rem"
  height="10rem"
  fit="contain"
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>

圓形圖片

通過(guò)round屬性可以設(shè)置圖片變圓,注意當(dāng)圖片寬高不相等且fit為contain或scale-down時(shí),將無(wú)法填充一個(gè)完整的圓形。

<van-image
  round
  width="10rem"
  height="10rem"
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>

圖片懶加載

設(shè)置lazy-load屬性來(lái)開(kāi)啟圖片懶加載,需要搭配 Lazyload 組件使用

<van-image
  width="100"
  height="100"
  lazy-load
  src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" 
/>
import Vue from 'vue';
import { Lazyload } from 'vant';

Vue.use(Lazyload);

加載中提示

Image組件提供了默認(rèn)的加載中提示,支持通過(guò)loading插槽自定義內(nèi)容

<van-image src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  <template v-slot:loading>
    <van-loading type="spinner" size="20" />
  </template>
</van-image>

加載失敗提示

Image組件提供了默認(rèn)的加載失敗提示,支持通過(guò)error插槽自定義內(nèi)容

<van-image src="https://img.yzcdn.cn/vant/cat.jpeg" rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow"  rel="external nofollow" >
  <template v-slot:error>加載失敗</template>
</van-image>

API

Props

參數(shù)說(shuō)明類型默認(rèn)值
src圖片鏈接string-
fit圖片填充模式stringfill
alt替代文本string-
width寬度,默認(rèn)單位為pxnumber | string-
height高度,默認(rèn)單位為pxnumber | string-
radius v2.1.6圓角大小,默認(rèn)單位為pxnumber | string0
round是否顯示為圓形booleanfalse
lazy-load是否開(kāi)啟圖片懶加載,須配合 Lazyload 組件使用booleanfalse
show-error v2.0.9是否展示圖片加載失敗提示booleantrue
show-loading v2.0.9是否展示圖片加載中提示booleantrue
error-icon v2.4.2失敗時(shí)提示的 圖標(biāo)名稱 或圖片鏈接stringwarning-o
loading-icon v2.4.2加載時(shí)提示的 圖標(biāo)名稱 或圖片鏈接stringphoto-o

圖片填充模式

名稱含義
contain保持寬高縮放圖片,使圖片的長(zhǎng)邊能完全顯示出來(lái)
cover保持寬高縮放圖片,使圖片的短邊能完全顯示出來(lái),裁剪長(zhǎng)邊
fill拉伸圖片,使圖片填滿元素
none保持圖片原有尺寸
scale-downnonecontain中較小的一個(gè)

Events

事件名說(shuō)明回調(diào)參數(shù)
click點(diǎn)擊圖片時(shí)觸發(fā)event: Event
load圖片加載完畢時(shí)觸發(fā)-
error圖片加載失敗時(shí)觸發(fā)-

Slots

名稱說(shuō)明
loading自定義加載中的提示內(nèi)容
error自定義加載失敗時(shí)的提示內(nèi)容


實(shí)例演示

以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)