錯視文字輪播 carousel
文字作為遮罩裁切圖片後,邊移動移動邊組成圖片。
看不懂說明?沒關係我也看不太懂,來看看就懂啦。ᕕ( ゚ ∀。)ᕗ
靈感來自我在 Envato 路過的一個輪播效果。
覺得非常有趣,想說挑戰在網頁上實現看看,本來想加入模糊效果,但是性能負擔過重,只好忍痛放棄了。 (๑•́ ₃ •̀๑)
使用範例
基本用法
酷酷的錯視文字輪播。(≖‿ゝ≖)✧
CAT
CAT
CAT





查看範例原始碼
vue
<template>
<div class="w-full flex flex-col gap-4">
<div class="flex justify-center">
<carousel-anamorphosis-text
ref="carouselRef"
:src-list
/>
</div>
<div class="w-full flex gap-4">
<base-btn
label="上一張"
class="flex-1"
@click="carouselRef?.prev()"
/>
<base-btn
label="下一張"
class="flex-1"
@click="carouselRef?.next()"
/>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import BaseBtn from '../../base-btn.vue'
import CarouselAnamorphosisText from '../carousel-anamorphosis-text.vue'
type SrcList = InstanceType<typeof CarouselAnamorphosisText>['srcList']
const srcList: SrcList = [
{
url: '/photography-street-cat.jpg',
text: 'CAT',
},
{
url: '/photography-ears-of-rice.jpg',
text: 'RICE',
},
{
url: '/photography-fireworks.jpg',
text: 'FIREWORKS',
},
{
url: '/photography-morning-light-of-rice.jpg',
text: 'LIGHT',
},
{
url: '/photography-big-stupid-bird.jpg',
text: 'BIRD',
},
]
const carouselRef = ref<InstanceType<typeof CarouselAnamorphosisText>>()
</script>
自訂每層文字
可以自由調整文字內容。◝( •ω• )◟
FISH
COD
CHILL



雖然看不太清楚文字寫啥就是了 (́⊙◞౪◟⊙‵)
查看範例原始碼
vue
<template>
<div class="w-full flex flex-col gap-4">
<div class="flex justify-center">
<carousel-anamorphosis-text
ref="carouselRef"
:src-list
height="60vh"
/>
</div>
<div class="w-full flex gap-4">
<base-btn
label="上一張"
class="flex-1"
@click="carouselRef?.prev()"
/>
<base-btn
label="下一張"
class="flex-1"
@click="carouselRef?.next()"
/>
</div>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import BaseBtn from '../../base-btn.vue'
import CarouselAnamorphosisText from '../carousel-anamorphosis-text.vue'
type SrcList = InstanceType<typeof CarouselAnamorphosisText>['srcList']
const srcList: SrcList = [
{
url: '/profile.webp',
text: ['FISH', 'COD', 'CHILL'],
},
{
url: '/profile-2.webp',
text: [
{ value: '肥', class: 'text-[18vmin]' },
{ value: '肥', class: 'text-[24vmin]' },
{ value: '魚', class: 'text-[18vmin]' },
{ value: '魚', class: 'text-[24vmin]' },
],
},
{
url: '/profile-3.webp',
text: ['吃', '爽', '魚'],
},
]
const carouselRef = ref<InstanceType<typeof CarouselAnamorphosisText>>()
</script>
原理
使用 background-clip: text
實現文字遮罩效果,配合 perspective
和 transform
屬性實現 3D 移動效果。
原始碼
API
Props
interface Props {
srcList: Array<{
/** 圖片 url */
url: string;
/** 文字內容。
*
* 如果給 `string[]`,則對應每一層的的文字內容。
*/
text: string | string[] | Array<{
/** 文字內容 */
value: string;
/** 額外的 CSS class */
class?: string;
}>;
/** 為空則以 Props.animationDuration 為主 */
animationDuration?: number;
/** 為空則以 Props.animationDelay 為主 */
animationDelay?: number;
}>;
/** @default 400px */
height?: string;
animationDuration?: number;
animationDelay?: number;
}
Emits
// const emit = defineEmits<{
// 'update:modelValue': [value: Props['modelValue']];
// }>()
Methods
defineExpose({
currentItem: srcItem,
isPlaying: computed(() => isPlaying.value),
next,
prev,
})
Slots
defineSlots<{
default?: (props: { isRunning: boolean }) => unknown;
}>()