|
| 1 | +<script setup lang="ts"> |
| 2 | +import { computed } from 'vue'; |
| 3 | +
|
| 4 | +interface GradientTextProps { |
| 5 | + text: string; |
| 6 | + className?: string; |
| 7 | + colors?: string[]; |
| 8 | + animationSpeed?: number; |
| 9 | + showBorder?: boolean; |
| 10 | +} |
| 11 | +
|
| 12 | +const props = withDefaults(defineProps<GradientTextProps>(), { |
| 13 | + text: '', |
| 14 | + className: '', |
| 15 | + colors: () => ['#ffaa40', '#9c40ff', '#ffaa40'], |
| 16 | + animationSpeed: 8, |
| 17 | + showBorder: false |
| 18 | +}); |
| 19 | +
|
| 20 | +const gradientStyle = computed(() => ({ |
| 21 | + backgroundImage: `linear-gradient(to right, ${ props.colors.join(', ') })`, |
| 22 | + animationDuration: `${ props.animationSpeed }s`, |
| 23 | + backgroundSize: '300% 100%', |
| 24 | + '--animation-duration': `${ props.animationSpeed }s` |
| 25 | +})); |
| 26 | +
|
| 27 | +const borderStyle = computed(() => ({ |
| 28 | + ...gradientStyle.value |
| 29 | +})); |
| 30 | +
|
| 31 | +const textStyle = computed(() => ({ |
| 32 | + ...gradientStyle.value, |
| 33 | + backgroundClip: 'text', |
| 34 | + WebkitBackgroundClip: 'text' |
| 35 | +})); |
| 36 | +</script> |
| 37 | + |
| 38 | +<template> |
| 39 | + <div |
| 40 | + :class="`relative mx-auto flex max-w-fit flex-row items-center justify-center rounded-[1.25rem] font-medium backdrop-blur transition-shadow duration-500 overflow-hidden cursor-pointer ${className}`" |
| 41 | + > |
| 42 | + <div |
| 43 | + v-if="showBorder" |
| 44 | + class="absolute inset-0 bg-cover z-0 pointer-events-none animate-gradient" |
| 45 | + :style="borderStyle" |
| 46 | + > |
| 47 | + <div |
| 48 | + class="absolute inset-0 bg-black rounded-[1.25rem] z-[-1]" |
| 49 | + style="width: calc(100% - 2px); height: calc(100% - 2px); left: 50%; top: 50%; transform: translate(-50%, -50%)" |
| 50 | + /> |
| 51 | + </div> |
| 52 | + |
| 53 | + <div class="inline-block relative z-2 text-transparent bg-cover animate-gradient" :style="textStyle"> |
| 54 | + {{ text }} |
| 55 | + </div> |
| 56 | + </div> |
| 57 | +</template> |
| 58 | + |
| 59 | +<style scoped> |
| 60 | +@keyframes gradient { |
| 61 | + 0% { |
| 62 | + background-position: 0% 50%; |
| 63 | + } |
| 64 | + 50% { |
| 65 | + background-position: 100% 50%; |
| 66 | + } |
| 67 | + 100% { |
| 68 | + background-position: 0% 50%; |
| 69 | + } |
| 70 | +} |
| 71 | +
|
| 72 | +.animate-gradient { |
| 73 | + animation: gradient var(--animation-duration, 8s) linear infinite; |
| 74 | +} |
| 75 | +</style> |
0 commit comments