Vue3渐变文字(GradientText)

简介: 该文档介绍了一个基于 Vue 的渐变文字组件 `GradientText`,允许用户通过配置参数实现不同样式和尺寸的文字渐变效果。支持自定义起始和结束颜色、渐变角度及多种预设类型(如 `primary`、`info` 等)。

效果如下图:在线预览

60748337dd1b4f708d3b10f28fac84d7.png

APIs

GradientText

参数 说明 类型 默认值
gradient 文字渐变色参数 string /Gradient undefined
size 文字大小,不指定单位时,默认单位 px number / string 14
type 渐变文字的类型 ‘primary’ / ‘info’ / ‘success’ / ‘warning’ / ‘error’ ‘primary’

Gradient Type

名称 说明 类型 默认值
from 起始颜色 string undefined
to 终点颜色 string undefined
deg? 渐变角度,单位 deg number / string 252

创建渐变文字组件GradientText.vue

<script setup lang="ts">
import {
    computed } from 'vue'
interface Gradient {
   
  from: string
  to: string
  deg?: number | string // 渐变角度,默认 252,单位 deg
}
interface Props {
   
  gradient?: string | Gradient // 文字渐变色参数
  size?: number | string // 文字大小,不指定单位时,默认单位 px
  type?: 'primary' | 'info' | 'success' | 'warning' | 'error' // 渐变文字的类型
}
const props = withDefaults(defineProps<Props>(), {
   
  gradient: undefined,
  size: 14,
  type: 'primary'
})
enum TypeStartColor {
   
  primary = 'rgba(22, 199, 255, 0.6)',
  info = 'rgba(22, 199, 255, 0.6)',
  success = 'rgba(82, 196, 26, 0.6)',
  warning = 'rgba(250, 173, 20, 0.6)',
  error = 'rgba(255, 77, 79, 0.6)'
}
enum TypeEndColor {
   
  primary = '#1677FF',
  info = '#1677FF',
  success = '#52c41a',
  warning = '#faad14',
  error = '#ff4d4f'
}
const gradientText = computed(() => {
   
  if (typeof props.gradient === 'string') {
   
    return {
   
      backgroundImage: props.gradient
    }
  }
  return {
   }
})
const rotate = computed(() => {
   
  if (typeof props.gradient === 'object' && props.gradient.deg) {
   
    return isNumber(props.gradient.deg) ? props.gradient.deg + 'deg' : props.gradient.deg
  }
  return '252deg'
})
const colorStart = computed(() => {
   
  if (typeof props.gradient === 'object') {
   
    return props.gradient.from
  } else {
   
    return TypeStartColor[props.type]
  }
})
const colorEnd = computed(() => {
   
  if (typeof props.gradient === 'object') {
   
    return props.gradient.to
  } else {
   
    return TypeEndColor[props.type]
  }
})
const fontSize = computed(() => {
   
  if (typeof props.size === 'number') {
   
    return props.size + 'px'
  }
  if (typeof props.size === 'string') {
   
    return props.size
  }
})
function isNumber(value: string | number): boolean {
   
  return typeof value === 'number'
}
</script>
<template>
  <span
    class="m-gradient-text"
    :style="[
      `--rotate: ${
     rotate}; --color-start: ${
     colorStart}; --color-end: ${
     colorEnd}; --font-size: ${
     fontSize};`,
      gradientText
    ]"
  >
    <slot></slot>
  </span>
</template>
<style lang="less" scoped>
.m-gradient-text {
   
  display: inline-block;
  font-size: var(--font-size);
  font-weight: 500;
  line-height: 1.5714285714285714;
  -webkit-background-clip: text;
  background-clip: text;
  color: #0000;
  white-space: nowrap;
  background-image: linear-gradient(var(--rotate), var(--color-start) 0%, var(--color-end) 100%);
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}
</style>

在要使用的页面引入

其中引入使用了以下组件:

<script setup lang="ts">
import GradientText from './GradientText.vue'
</script>
<template>
  <div>
    <h1>{
   {
    $route.name }} {
   {
    $route.meta.title }}</h1>
    <h2 class="mt30 mb10">基本使用</h2>
    <Space gap="small" vertical>
      <GradientText>basic</GradientText>
      <GradientText type="info">info</GradientText>
      <GradientText type="success">success</GradientText>
      <GradientText type="warning">warning</GradientText>
      <GradientText type="error">error</GradientText>
    </Space>
    <h2 class="mt30 mb10">文字大小</h2>
    <Space gap="small" vertical>
      <GradientText :size="36" type="info">Live Forever</GradientText>
      <GradientText :size="36" type="error">Live Forever</GradientText>
      <GradientText :size="24" type="warning">Married with Children</GradientText>
      <GradientText :size="24" type="success">Back in the USSR</GradientText>
    </Space>
    <h2 class="mt30 mb10">自定义颜色</h2>
    <Space gap="small" vertical>
      <GradientText
        :size="24"
        :gradient="{
   
          from: 'rgb(85, 85, 85)',
          to: 'rgb(170, 170, 170)'
        }"
        >定制颜色</GradientText
      >
      <GradientText
        :size="24"
        :gradient="{
   
          deg: 180,
          from: 'rgb(85, 85, 85)',
          to: 'rgb(170, 170, 170)'
        }"
        >定制颜色</GradientText
      >
      <GradientText
        :size="28"
        :gradient="{
   
          deg: '90deg',
          from: '#09c8ce',
          to: '#eb2f96'
        }"
        >和标题一样的颜色</GradientText
      >
      <GradientText :size="24" gradient="linear-gradient(90deg, red 0%, green 50%, blue 100%)">瞎写的颜色</GradientText>
    </Space>
  </div>
</template>
相关文章
|
12天前
vue3学习(3)
vue3学习(3)
|
12天前
|
资源调度 JavaScript API
vue3封装城市联动组件
vue3封装城市联动组件
|
9天前
|
JavaScript API
Vue3中的计算属性能否动态修改
【9月更文挑战第5天】Vue3中的计算属性能否动态修改
40 10
|
2天前
|
JavaScript
Vue3中路由跳转的语法
Vue3中路由跳转的语法
105 58
|
5天前
|
前端开发
vue3+ts项目中使用mockjs
vue3+ts项目中使用mockjs
198 58
|
12天前
|
JavaScript 前端开发
vue3学习(1)
vue3学习(1)
93 44
|
16天前
|
JavaScript
Vue3搜索框(InputSearch)
这篇文章介绍了如何在Vue 3中创建一个具有搜索、清除、加载状态等多功能的搜索框组件(InputSearch),并提供了组件的配置选项、事件处理和使用示例。
Vue3搜索框(InputSearch)
|
2天前
|
JavaScript 开发工具
vite如何打包vue3插件为JSSDK
【9月更文挑战第10天】以下是使用 Vite 打包 Vue 3 插件为 JS SDK 的步骤:首先通过 `npm init vite-plugin-sdk --template vue` 创建 Vue 3 项目并进入项目目录 `cd vite-plugin-sdk`。接着,在 `src` 目录下创建插件文件(如 `myPlugin.js`),并在 `main.js` 中引入和使用该插件。然后,修改 `vite.config.js` 文件以配置打包选项。最后,运行 `npm run build` 进行打包,生成的 `my-plugin-sdk.js` 即为 JS SDK,可在其他项目中引入使用。
|
3天前
|
JavaScript 开发者
彻底搞懂 Vue3 中 watch 和 watchEffect是用法
彻底搞懂 Vue3 中 watch 和 watchEffect是用法
|
9天前
|
JavaScript API
如何使用Vue3的可计算属性
【9月更文挑战第5天】如何使用Vue3的可计算属性
42 13