国产gaysexchina男同gay,japanrcep老熟妇乱子伦视频,吃奶呻吟打开双腿做受动态图,成人色网站,国产av一区二区三区最新精品

Vant4 ImagePreview 圖片預(yù)覽

2023-02-16 17:56 更新

介紹

圖片放大預(yù)覽,支持組件調(diào)用和函數(shù)調(diào)用兩種方式。

引入

通過(guò)以下方式來(lái)全局注冊(cè)組件,更多注冊(cè)方式請(qǐng)參考組件注冊(cè)。

import { createApp } from 'vue';
import { ImagePreview } from 'vant';

const app = createApp();
app.use(ImagePreview);

函數(shù)調(diào)用

為了便于使用 ?ImagePreview?,Vant 提供了一系列輔助函數(shù),通過(guò)輔助函數(shù)可以快速喚起全局的圖片預(yù)覽組件。

比如使用 ?showImagePreview? 函數(shù),調(diào)用后會(huì)直接在頁(yè)面中渲染對(duì)應(yīng)的圖片預(yù)覽組件。

import { showImagePreview } from 'vant';

showImagePreview(['https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg']);

代碼演示

基礎(chǔ)用法

在調(diào)用 ?showImagePreview? 時(shí),直接傳入圖片數(shù)組,即可展示圖片預(yù)覽。

import { showImagePreview } from 'vant';

showImagePreview([
  'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
  'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
]);

指定初始位置

?showImagePreview? 支持傳入配置對(duì)象,并通過(guò) ?startPosition? 選項(xiàng)指定圖片的初始位置(索引值)。

import { showImagePreview } from 'vant';

showImagePreview({
  images: [
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
  ],
  startPosition: 1,
});

展示關(guān)閉按鈕

設(shè)置 ?closeable? 屬性后,會(huì)在彈出層的右上角顯示關(guān)閉圖標(biāo),并且可以通過(guò) ?close-icon? 屬性自定義圖標(biāo),使用?close-icon-position? 屬性可以自定義圖標(biāo)位置。

import { showImagePreview } from 'vant';

showImagePreview({
  images: [
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
  ],
  closeable: true,
});

監(jiān)聽關(guān)閉事件

通過(guò) ?onClose? 選項(xiàng)監(jiān)聽圖片預(yù)覽的關(guān)閉事件。

import { showToast, showImagePreview } from 'vant';

showImagePreview({
  images: [
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
  ],
  onClose() {
    showToast('關(guān)閉');
  },
});

異步關(guān)閉

通過(guò) ?beforeClose? 屬性可以攔截關(guān)閉行為。

import { showImagePreview } from 'vant';

const instance = showImagePreview({
  images: [
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
    'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
  ],
  beforeClose: () => false,
});

setTimeout(() => {
  // 調(diào)用實(shí)例上的 close 方法手動(dòng)關(guān)閉圖片預(yù)覽
  instance.close();
}, 2000);

使用 ImagePreview 組件

如果需要在 ImagePreview 內(nèi)嵌入組件或其他自定義內(nèi)容,可以直接使用 ImagePreview 組件,并使用 ?index? 插槽進(jìn)行定制。使用前需要通過(guò) ?app.use? 等方式注冊(cè)組件。

<van-image-preview v-model:show="show" :images="images" @change="onChange">
  <template v-slot:index>第{{ index }}頁(yè)</template>
</van-image-preview>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const index = ref(0);
    const images = [
      'https://fastly.jsdelivr.net/npm/@vant/assets/apple-1.jpeg',
      'https://fastly.jsdelivr.net/npm/@vant/assets/apple-2.jpeg',
    ];
    const onChange = (newIndex) => {
      index.value = newIndex;
    };

    return {
      show,
      index,
      images,
      onChange,
    };
  },
};

使用 image 插槽

當(dāng)以組件調(diào)用的方式使用 ImagePreview 時(shí),可以通過(guò) ?image? 插槽來(lái)插入自定義的內(nèi)容,比如展示一個(gè)視頻內(nèi)容。

<van-image-preview v-model:show="show" :images="images">
  <template #image="{ src }">
    <video style="width: 100%;" controls>
      <source :src="src" />
    </video>
  </template>
</van-image-preview>
import { ref } from 'vue';

export default {
  setup() {
    const show = ref(false);
    const images = [
      'https://www.w3school.com.cn/i/movie.ogg',
      'https://www.w3school.com.cn/i/movie.ogg',
      'https://www.w3school.com.cn/i/movie.ogg',
    ];
    return {
      show,
      images,
    };
  },
};

API

方法

Vant 中導(dǎo)出了以下 ImagePreview 相關(guān)的輔助函數(shù):

 方法名 說(shuō)明  參數(shù)  返回值 
showImagePreview    展示圖片預(yù)覽 ? string[] | ImagePreviewOptions?  imagePreview 實(shí)例

ImagePreviewOptions

調(diào)用 ?showImagePreview? 方法時(shí),支持傳入以下選項(xiàng):

參數(shù)名 說(shuō)明 類型 默認(rèn)值
images 需要預(yù)覽的圖片 URL 數(shù)組 string[] []
startPosition 圖片預(yù)覽起始位置索引 number | string 0
swipeDuration 動(dòng)畫時(shí)長(zhǎng),單位為 ms number | string 300
showIndex 是否顯示頁(yè)碼 boolean true
showIndicators 是否顯示輪播指示器 boolean false
loop 是否開啟循環(huán)播放 boolean true
onClose 關(guān)閉時(shí)的回調(diào)函數(shù) Function -
onChange 切換圖片時(shí)的回調(diào)函數(shù),回調(diào)參數(shù)為當(dāng)前索引 Function -
onScale 縮放圖片時(shí)的回調(diào)函數(shù),回調(diào)參數(shù)為當(dāng)前索引和當(dāng)前縮放值組成的對(duì)象 Function -
beforeClose 關(guān)閉前的回調(diào)函數(shù),返回 false 可阻止關(guān)閉,支持返回 Promise (active: number) => boolean | Promise<boolean> -
closeOnPopstate 是否在頁(yè)面回退時(shí)自動(dòng)關(guān)閉 boolean true
className 自定義類名 string | Array | object -
maxZoom 手勢(shì)縮放時(shí),最大縮放比例 number | string 3
minZoom 手勢(shì)縮放時(shí),最小縮放比例 number | string 1/3
closeable 是否顯示關(guān)閉圖標(biāo) boolean false
closeIcon 關(guān)閉圖標(biāo)名稱或圖片鏈接 string clear
closeIconPosition 關(guān)閉圖標(biāo)位置,可選值為 top-left
bottom-left bottom-right
string top-right
transition v3.0.8 動(dòng)畫類名,等價(jià)于 transition 的 name 屬性 string van-fade
overlayClass v3.2.8 自定義遮罩層類名 string | Array | object -
overlayStyle v3.0.8 自定義遮罩層樣式 object -
teleport 指定掛載的節(jié)點(diǎn),等同于 Teleport 組件的 to 屬性 string | Element -

Props

通過(guò)組件調(diào)用 ?ImagePreview? 時(shí),支持以下 Props:

參數(shù) 說(shuō)明 類型 默認(rèn)值
v-model:show 是否展示圖片預(yù)覽 boolean false
images 需要預(yù)覽的圖片 URL 數(shù)組 string[] []
start-position 圖片預(yù)覽起始位置索引 number | string 0
swipe-duration 動(dòng)畫時(shí)長(zhǎng),單位為 ms number | string 300
show-index 是否顯示頁(yè)碼 boolean true
show-indicators 是否顯示輪播指示器 boolean false
loop 是否開啟循環(huán)播放 boolean true
before-close 關(guān)閉前的回調(diào)函數(shù),返回 false 可阻止關(guān)閉,支持返回 Promise (active: number) => boolean | Promise<boolean> -
close-on-popstate 是否在頁(yè)面回退時(shí)自動(dòng)關(guān)閉 boolean true
class-name 自定義類名 string | Array | object -
max-zoom 手勢(shì)縮放時(shí),最大縮放比例 number | string 3
min-zoom 手勢(shì)縮放時(shí),最小縮放比例 number | string 1/3
closeable 是否顯示關(guān)閉圖標(biāo) boolean false
close-icon 關(guān)閉圖標(biāo)名稱或圖片鏈接 string clear
close-icon-position 關(guān)閉圖標(biāo)位置,可選值為 top-left
bottom-left bottom-right
string top-right
transition v3.0.8 動(dòng)畫類名,等價(jià)于 transition 的 name 屬性 string van-fade
overlay-class v3.2.8 自定義遮罩層類名 string | Array | object -
overlay-style v3.0.8 自定義遮罩層樣式 object -
teleport 指定掛載的節(jié)點(diǎn),等同于 Teleport 組件的 to 屬性 string | Element -

Events

通過(guò)組件調(diào)用 ?ImagePreview? 時(shí),支持以下事件:

事件 說(shuō)明 回調(diào)參數(shù)
close 關(guān)閉時(shí)觸發(fā) { index: number, url: string }
closed 關(guān)閉且且動(dòng)畫結(jié)束后觸發(fā) -
change 切換當(dāng)前圖片時(shí)觸發(fā) index: number
scale 縮放當(dāng)前圖片時(shí)觸發(fā) { index: number, scale: number }
long-press 長(zhǎng)按當(dāng)前圖片時(shí)觸發(fā) { index: number }

方法

通過(guò)組件調(diào)用 ?ImagePreview? 時(shí),通過(guò) ref 可以獲取到 ImagePreview 實(shí)例并調(diào)用實(shí)例方法,詳見組件實(shí)例方法。

方法名 說(shuō)明 參數(shù) 返回值
swipeTo 2.9.0 切換到指定位置 index: number, options?: SwipeToOptions -

類型定義

組件導(dǎo)出以下類型定義:

import type {
  ImagePreviewProps,
  ImagePreviewOptions,
  ImagePreviewInstance,
  ImagePreviewScaleEventParams,
} from 'vant';

?ImagePreviewInstance? 是組件實(shí)例的類型,用法如下:

import { ref } from 'vue';
import type { ImagePreviewInstance } from 'vant';

const imagePreviewRef = ref<ImagePreviewInstance>();

imagePreviewRef.value?.swipeTo(1);

Slots

通過(guò)組件調(diào)用 ?ImagePreview? 時(shí),支持以下插槽:

名稱 說(shuō)明 參數(shù)
index 自定義頁(yè)碼內(nèi)容 { index: 當(dāng)前圖片的索引 }
cover 自定義覆蓋在圖片預(yù)覽上方的內(nèi)容 -
image v3.6.5 自定義圖片內(nèi)容 { src: 當(dāng)前資源地址 }

onClose 回調(diào)參數(shù)

參數(shù)名 說(shuō)明 類型
url 當(dāng)前圖片 URL string
index 當(dāng)前圖片的索引值 number

onScale 回調(diào)參數(shù)

參數(shù)名 說(shuō)明 類型
index 當(dāng)前圖片的索引值 number
scale 當(dāng)前圖片的縮放值 number

主題定制

樣式變量

組件提供了下列 CSS 變量,可用于自定義樣式,使用方法請(qǐng)參考 ConfigProvider 組件。

名稱 默認(rèn)值 描述
--van-image-preview-index-text-color var(--van-white) -
--van-image-preview-index-font-size var(--van-font-size-md) -
--van-image-preview-index-line-height var(--van-line-height-md) -
--van-image-preview-index-text-shadow 0 1px 1px var(--van-gray-8) -
--van-image-preview-overlay-background rgba(0, 0, 0, 0.9) -
--van-image-preview-close-icon-size 22px -
--van-image-preview-close-icon-color var(--van-gray-5) -
--van-image-preview-close-icon-margin var(--van-padding-md) -
--van-image-preview-close-icon-z-index 1 -

常見問(wèn)題

在桌面端無(wú)法操作組件?

參見桌面端適配。

引用 showImagePreview 時(shí)出現(xiàn)編譯報(bào)錯(cuò)?

如果引用 ?showImagePreview? 方法時(shí)出現(xiàn)以下報(bào)錯(cuò),說(shuō)明項(xiàng)目中使用了 ?babel-plugin-import? 插件,導(dǎo)致代碼被錯(cuò)誤編譯。

These dependencies were not found:

* vant/es/show-image-preview in ./src/xxx.js
* vant/es/show-image-preview/style in ./src/xxx.js

Vant 從 4.0 版本開始不再支持 ?babel-plugin-import? 插件,請(qǐng)參考 遷移指南 移除該插件。


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

掃描二維碼

下載編程獅App

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

編程獅公眾號(hào)