W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
增強(qiáng)版的 img 標(biāo)簽,提供多種圖片填充模式,支持圖片懶加載、加載中提示、加載失敗提示
import Vue from 'vue';
import { Image } from 'vant';
Vue.use(Image);
基礎(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>
參數(shù) | 說(shuō)明 | 類型 | 默認(rèn)值 |
---|---|---|---|
src | 圖片鏈接 | string | - |
fit | 圖片填充模式 | string | fill |
alt | 替代文本 | string | - |
width | 寬度,默認(rèn)單位為px | number | string | - |
height | 高度,默認(rèn)單位為px | number | string | - |
radius v2.1.6 | 圓角大小,默認(rèn)單位為px | number | string | 0 |
round | 是否顯示為圓形 | boolean | false |
lazy-load | 是否開(kāi)啟圖片懶加載,須配合 Lazyload 組件使用 | boolean | false |
show-error v2.0.9 | 是否展示圖片加載失敗提示 | boolean | true |
show-loading v2.0.9 | 是否展示圖片加載中提示 | boolean | true |
error-icon v2.4.2 | 失敗時(shí)提示的 圖標(biāo)名稱 或圖片鏈接 | string | warning-o |
loading-icon v2.4.2 | 加載時(shí)提示的 圖標(biāo)名稱 或圖片鏈接 | string | photo-o |
名稱 | 含義 |
---|---|
contain | 保持寬高縮放圖片,使圖片的長(zhǎng)邊能完全顯示出來(lái) |
cover | 保持寬高縮放圖片,使圖片的短邊能完全顯示出來(lái),裁剪長(zhǎng)邊 |
fill | 拉伸圖片,使圖片填滿元素 |
none | 保持圖片原有尺寸 |
scale-down | 取none 或contain 中較小的一個(gè) |
事件名 | 說(shuō)明 | 回調(diào)參數(shù) |
---|---|---|
click | 點(diǎn)擊圖片時(shí)觸發(fā) | event: Event |
load | 圖片加載完畢時(shí)觸發(fā) | - |
error | 圖片加載失敗時(shí)觸發(fā) | - |
名稱 | 說(shuō)明 |
---|---|
loading | 自定義加載中的提示內(nèi)容 |
error | 自定義加載失敗時(shí)的提示內(nèi)容 |
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: