圖片展示是一個內容展示元件。
import Galleria from 'primevue/galleria';
圖片展示需要一個 value 作為圖片集合,item 範本用於較高解析度的圖片,以及 thumbnail 範本用於顯示為縮圖。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" containerStyle="max-width: 640px">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" />
</template>
</Galleria>
圖片展示可以使用與 activeIndex 的綁定來以程式方式控制。
<div>
<Button icon="pi pi-minus" @click="prev" />
<Button icon="pi pi-plus" @click="next" severity="secondary" class="ml-2" />
</div>
<Galleria v-model:activeIndex="activeIndex" :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" containerStyle="max-width: 640px">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" />
</template>
</Galleria>
指示器允許在項目之間快速導航。
指示器預設顯示在底部,透過啟用 showIndicators 屬性並使用點擊事件進行互動。
<Galleria :value="images" :numVisible="5" containerStyle="max-width: 640px"
:showThumbnails="false" :showIndicators="true">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
</Galleria>
如果加入 changeItemOnIndicatorHover,指示器可以在懸停時而不是點擊時啟用。
<Galleria :value="images" :numVisible="5" containerStyle="max-width: 640px"
:showThumbnails="false" :showIndicators="true" :changeItemOnIndicatorHover="true">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
</Galleria>
指示器可以使用 indicatorsPosition 屬性放置在四個不同的側面。此外,啟用 showIndicatorsOnItem 會將指示器移動到圖片區段內。
<Galleria :value="images" :numVisible="5" containerStyle="max-width: 640px" :showThumbnails="false"
:showIndicators="true" :changeItemOnIndicatorHover="true" :showIndicatorsOnItem="inside" :indicatorsPosition="position">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
</Galleria>
指示器內容可以使用 indicator 屬性進行自訂,該屬性接受索引作為參數並預期內容。
<Galleria :value="images" :numVisible="5" containerStyle="max-width: 640px" :showThumbnails="false"
:showIndicators="true" :changeItemOnIndicatorHover="true" :showIndicatorsOnItem="true" indicatorsPosition="left">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #indicator="{ index }">
<span style="color: '#ffffff', cursor: pointer">{{ index + 1 }}</span>
</template>
</Galleria>
縮圖代表實際內容的較小版本。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" :thumbnailsPosition="position" containerStyle="max-width: 640px">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<div class="grid gap-4 justify-center">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block;" />
</div>
</template>
</Galleria>
每個螢幕尺寸的設定透過 responsiveOptions 屬性定義。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" :circular="true" containerStyle="max-width: 640px">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
</Galleria>
在全螢幕模式下,內容會覆蓋模態層上的整個頁面。
全螢幕模式透過加入 fullScreen 屬性啟用,可見度透過與 visible 屬性的綁定控制。
<Galleria v-model:visible="displayBasic" :value="images" :responsiveOptions="responsiveOptions" :numVisible="9" containerStyle="max-width: 50%" :circular="true" :fullScreen="true" :showItemNavigators="true">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</template>
</Galleria>
<Button label="Show" icon="pi pi-external-link" @click="displayBasic = true" />
縮圖也可以在全螢幕模式中隱藏。
<Galleria v-model:visible="displayBasic" :value="images" :responsiveOptions="responsiveOptions" :numVisible="9" containerStyle="max-width: 50%" :circular="true" :fullScreen="true" :showItemNavigators="true" :showThumbnails="false">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</template>
</Galleria>
<Button label="Show" icon="pi pi-external-link" @click="displayBasic = true" />
使用 activeIndex,圖片展示會顯示特定的初始圖片。
<Galleria v-model:activeIndex="activeIndex" v-model:visible="displayCustom" :value="images" :responsiveOptions="responsiveOptions" :numVisible="7"
containerStyle="max-width: 850px" :circular="true" :fullScreen="true" :showItemNavigators="true" :showThumbnails="false">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</template>
</Galleria>
加入 showItemNavigators 以顯示導航器元素以及左右兩側。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" :circular="true" containerStyle="max-width: 640px"
:showItemNavigators="true">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block;" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block;" />
</template>
</Galleria>
僅使用導航器的簡單範例。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" :circular="true" containerStyle="max-width: 640px"
:showItemNavigators="true" :showThumbnails="false">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block;" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block;" />
</template>
</Galleria>
只有在啟用 showItemNavigatorsOnHover 時,導航器才會僅在懸停時顯示。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" :circular="true" containerStyle="max-width: 640px" :showItemNavigators="true" :showItemNavigatorsOnHover="true">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</template>
</Galleria>
導航器和指示器也可以組合使用。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" :circular="true" containerStyle="max-width: 640px"
:showItemNavigators="true" :showThumbnails="false" :showItemNavigatorsOnHover="true" :showIndicators="true">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block;" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block;" />
</template>
</Galleria>
幻燈片放映實作透過加入 circular 和 autoPlay 屬性定義。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" containerStyle="max-width: 640px"
:circular="true" :autoPlay="true" :transitionInterval="2000">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</template>
</Galleria>
圖片的描述透過 caption 屬性指定,該屬性接受顯示的物件並返回內容。
<Galleria :value="images" :responsiveOptions="responsiveOptions" :numVisible="5" containerStyle="max-width: 640px">
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" style="width: 100%; display: block" />
</template>
<template #thumbnail="slotProps">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</template>
<template #caption="slotProps">
<div class="text-xl mb-2 font-bold">{{ slotProps.item.title }}</div>
<p class="text-white">{{ slotProps.item.alt }}</p>
</template>
</Galleria>
具有自訂 UI 的進階圖片展示實作。
<Galleria
ref="galleria"
v-model:activeIndex="activeIndex"
:value="images"
:numVisible="5"
containerStyle="max-width: 640px"
:showThumbnails="showThumbnails"
:showItemNavigators="true"
:showItemNavigatorsOnHover="true"
:circular="true"
:autoPlay="isAutoPlay"
:transitionInterval="3000"
:responsiveOptions="responsiveOptions"
:pt="{
root: {
class: [{ 'flex flex-col': fullScreen }]
},
content: {
class: ['relative', { 'flex-1 justify-center': fullScreen }]
},
thumbnails: 'absolute w-full left-0 bottom-0'
}"
>
<template #item="slotProps">
<img :src="slotProps.item.itemImageSrc" :alt="slotProps.item.alt" :style="[{ width: !fullScreen ? '100%' : '', display: !fullScreen ? 'block' : '' }]" />
</template>
<template #thumbnail="slotProps">
<div class="grid gap-4 justify-center">
<img :src="slotProps.item.thumbnailImageSrc" :alt="slotProps.item.alt" style="display: block" />
</div>
</template>
<template #footer>
<div class="flex items-stretch bg-surface-950 text-white h-10">
<button type="button" @click="onThumbnailButtonClick" class="bg-transparent border-none rounded-none hover:bg-white/10 text-white inline-flex justify-center items-center cursor-pointer px-3">
<i class="pi pi-th-large"></i>
</button>
<button type="button" @click="toggleAutoSlide" class="bg-transparent border-none rounded-none hover:bg-white/10 text-white inline-flex justify-center items-center cursor-pointer px-3"><i :class="slideButtonIcon"></i></button>
<span v-if="images" class="flex items-center gap-4 ml-3">
<span class="text-sm">{{ activeIndex + 1 }}/{{ images.length }}</span>
<span class="font-bold text-sm">{{ images[activeIndex].title }}</span>
<span class="text-sm">{{ images[activeIndex].alt }}</span>
</span>
<button type="button" @click="toggleFullScreen" class="bg-transparent border-none rounded-none hover:bg-white/10 text-white inline-flex justify-center items-center cursor-pointer px-3 ml-auto">
<i :class="fullScreenIcon"></i>
</button>
</div>
</template>
</Galleria>
圖片展示使用 region 角色,由於任何屬性都會傳遞到主要容器元素,因此也可以使用 aria-label 和 aria-roledescription 等屬性。如果圖片展示不在自動播放模式下,則幻燈片容器的 aria-live 屬性設定為「polite」,否則在自動播放時,該值會為「off」。
幻燈片具有 group 角色,並帶有 aria-label,該標籤引用 locale API 的 aria.slideNumber 屬性。同樣,aria.slide 用作項目的 aria-roledescription。非作用中的幻燈片會透過 aria-hidden 從閱讀器中隱藏。
下一個和上一個導航器是按鈕元素,具有 aria-label 屬性,預設情況下分別引用 locale API 的 aria.prevPageLabel 和 aria.nextPageLabel 屬性,您仍然可以使用自己的 aria 角色和屬性,因為任何有效的屬性都會透過使用 nextButtonProps 和 prevButtonProps 隱式傳遞到按鈕元素。
快速導航元素和縮圖遵循標籤模式。它們放置在具有 tablist 角色的元素內,而每個項目都有一個 tab 角色,並帶有 aria-selected 和 aria-controls 屬性。快速導航項目的 aria-label 屬性引用 locale API 的 aria.pageLabel。目前頁面標記為 aria-current。
在全螢幕模式下,模態元素使用啟用 aria-modal 的 dialog 角色。關閉按鈕從 locale API 的 aria.close 屬性中擷取 aria-label。
鍵 | 功能 |
---|---|
tab | 在輪播中移動焦點通過互動式元素。 |
enter | 啟動導航。 |
space | 啟動導航。 |
鍵 | 功能 |
---|---|
tab | 移動焦點通過使用中的幻燈片連結。 |
enter | 啟動焦點幻燈片連結。 |
space | 啟動焦點幻燈片連結。 |
右箭頭 | 將焦點移至下一個幻燈片連結。 |
左箭頭 | 將焦點移至上一個幻燈片連結。 |
home | 將焦點移至第一個幻燈片連結。 |
end | 將焦點移至最後一個幻燈片連結。 |