Nuxt Swiper
!重要Nuxt Swiper 使用 Swiper.js 作為基礎,並使用其網頁元件。在使用此模組並回報任何與 Nuxt Swiper 沒有直接關係的問題之前,請務必閱讀 Swiper.js 的文件。如果存在底層錯誤,請將問題提交至 Swiper.js 的 儲存庫。
!注意 如果您想學習如何在 Vue.js 中使用網頁元件,請參考此處的文件:Vue.js 網頁元件
功能特色
- 📖 開放原始碼
- 🪄 TypeScript 支援
- ✅ 啟用自動匯入
- ✨ 開箱即用,像魔法一樣運作
設定
線上試用
步驟 1:安裝模組
npx nuxi@latest module add swiper
使用方式
!資訊 由於這些是網頁元件,它們使用 kebab-case 命名慣例,而不是 Vue 使用的 camelCase 命名慣例。
Swiper 元件 (WebComponent) |
---|
<swiper-container/> |
<swiper-slide /> |
模組選項
interface ModuleOptions {
/**
* Enable custom Swiper composables to help you access Swiper instance.
* @example ```vue
* <script setup>
* const swiperRef = ref<null>(null)
* const swiper = useSwiper(swiperRef, { loop: true, autoplay: { delay: 5000 })
*
* const next = () => swiper.next()
* </script>
*
* <template>
* <swiper-container ref="swiperRef" :init="false">
* <swiper-slide>Slide 1</swiper-slide>
* <swiper-slide>Slide 2</swiper-slide>
* </swiper-container>
* </template>
* ```
* @default true
*/
enableComposables?: boolean
/**
* Bundle Swiper custom elements.
* if disabled, you need to import swiper css and modules manually.
* @see https://swiper.dev.org.tw/element#core-version--modules
* @default true
*/
bundled?: boolean
}
基本用法
<script setup lang="ts">
// Create 10 slides
const containerRef = ref(null)
const slides = ref(Array.from({ length: 10 }))
const swiper = useSwiper(containerRef)
onMounted(() => {
// Access Swiper instance
// Read more about Swiper instance: https://swiper.dev.org.tw/swiper-api#methods--properties
console.log(swiper.instance)
})
</script>
<template>
<ClientOnly>
<swiper-container ref="containerRef">
<swiper-slide
v-for="(slide, idx) in slides"
:key="idx"
style="background-color: rgb(32, 233, 70); color: white;"
>
Slide {{ idx + 1 }}
</swiper-slide>
</swiper-container>
</ClientOnly>
<!-- Go back one slide -->
<button @click="swiper.prev()">
Prev
</button>
<!-- Go forward one slide -->
<button @click="swiper.next()">
Next
</button>
</template>
<style lang="css">
swiper-slide {
display: flex;
justify-content: center;
align-items: center;
font-size: 18px;
height: 20vh;
font-size: 4rem;
font-weight: bold;
font-family: 'Roboto', sans-serif;
}
</style>
進階用法
<script setup lang="ts">
const containerRef = ref(null)
const slides = ref(Array.from({ length: 10 }))
const swiper = useSwiper(containerRef, {
effect: 'creative',
loop: true,
autoplay: {
delay: 5000,
},
creativeEffect: {
prev: {
shadow: true,
translate: [0, 0, -400],
},
next: {
shadow: true,
translate: [0, 0, -400],
},
},
})
onMounted(() => {
console.log(swiper.instance)
})
</script>
<template>
<ClientOnly>
<swiper-container ref="containerRef" :init="false">
<swiper-slide
v-for="(slide, idx) in slides"
:key="idx"
style="background-color: rgb(32, 233, 70); color: white;"
>
Slide {{ idx + 1 }}
</swiper-slide>
</swiper-container>
</ClientOnly>
</template>
<style lang="css">
swiper-slide {
display: flex;
justify-content: center;
align-items: center;
font-size: 18px;
height: 20vh;
font-size: 4rem;
font-weight: bold;
font-family: 'Roboto', sans-serif;
}
</style>
💻 開發
本地開發
貢獻者
Swiper.js
由 @nolimits4web 開發。
Nuxt Swiper
由 @cpreston321 開發。
📧 聯絡方式
X (前身為 Twitter) - @christian_ggg
此外,如果您喜歡我的作品,請隨時請我喝杯咖啡 ☕️