当前位置:网站首页>NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单
NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单
2022-08-10 08:01:00 【web老猴子】
写在前面
NaiveUI中有着一个非常有意思的组件,就是渐变文字组件,如下图:

有意思的点是这段文字描述_这个东西看起来没啥用,实际上确实没啥用。_
这里我们用Vue3.2+TS来实现这个简单的小组件。
渐变文字
渐变文字的实现比较简单,利用background-clip属性就可以实现,该属性存在一个text属性值,它可以将背景作为文字的前景色,配合渐变就可以实现渐变文字了,示例代码如下:
css
<span class="ywz-gradient-text">渐变文字</span>
html
.ywz-gradient-text {display: inline-block;font-weight: 700;font-size: 32px;background-clip: text;-webkit-background-clip: text;color: transparent;white-space: nowrap;background-image: linear-gradient(252deg,rgba(24, 160, 88, 0.6) 0%,#18a058 100%);
}
代码运行效果如下:

封装渐变组件
我们现在开始使用Vue3+TS来封装这个渐变组件,其实非常简单,就是通过自定义属性和动态class实现不同的文字渐变效果。
定义props
这里我们定义4个props,也就是渐变文字具有4个属性,分别如下:
type:预设的渐变效果
size:渐变文字的大小
weight:渐变文字的粗细
gradient:可以自定义渐变颜色
实现代码如下:
type TextType = 'error' | 'info' | 'warning' | 'success'
type WeightType = 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 'normal' | 'bold'
type RotateType = 'to left' | 'to right' | 'to bottom' | 'to top' | number
interface IGradient {rotate: RotateType // 线性渐变方向start: string // 开始的色值end: string // 结束的色值
}
interface Props {type?: TextTypesize?: stringgradient?: IGradientweight?: WeightType
}
const props = defineProps<Props>()
上面就是我们这个组件中唯一的TS代码,只有这些了,因为这个组件中没有任何的逻辑部分。
实现组件效果
首先我们先将预设的那四个渐变效果的CSS进行定义一下,示例代码如下:
.error { background-image: linear-gradient( 252deg, rgba(208, 48, 80, 0.6) 0%, #d03050 100% );}
.info { background-image: linear-gradient( 252deg, rgba(32, 128, 240, 0.6) 0%, #2080f0 100%);}
.warning { background-image: linear-gradient( 252deg, rgba(240, 160, 32, 0.6) 0%, #f0a020 100% );}
.success { background-image: linear-gradient( 252deg, rgba(24, 160, 88, 0.6) 0%, #18a058 100% ); }
现在我们来定义一下<template>中的内容:
<template><spanclass="ywz-gradient-text":class="[props.type, props.gradient ? 'custom-gradient' : '']":style="{'--size': props.size ?? '16px','--weight': props.weight ?? '400','--rotate':typeof props.gradient?.rotate === 'number'? props.gradient?.rotate + 'deg': props.gradient?.rotate,'--start': props.gradient?.start,'--end': props.gradient?.end,}"><!-- 默认插槽,也就是文字 --><slot></slot></span>
</template>
上面的代码中通过动态class实现不同预设的展示以及自定义渐变的展示。
上面的代码中存在??和?.这两个运算符,这两个是ES2020中增加的新特性,如果不了解可以通过下面这篇文章来了解一下ECMAScript中的所有新特性:
用8000字总结了【ES2015~ES2022】的所有新特性
剩余的CSS代码如下:
.ywz-gradient-text {display: inline-block;font-weight: var(--weight);background-clip: text;font-size: var(--size);-webkit-background-clip: text;color: transparent;white-space: nowrap;
}
.custom-gradient {background-image: linear-gradient(var(--rotate),var(--start) 0%,var(--end) 100%);
}
写在最后
这篇文章到这就写完了,代码量超级少,最后实现了这个有意思的小组件。
这把是娱乐局
边栏推荐
- 自动化测试框架Pytest(二)——前后置处理
- Rust learning: 6.4_ enumeration of composite types
- Rust学习:6.1_复合类型之切片
- 人工神经网络工作原理,神经网络的工作原理
- 手把手教你进行Mysql查询操作
- LaTeX出现错误代码Command \algorithmic already defined
- Is the write performance of raid5 faster than raid10?
- Synchronization lock synchronized traces the source
- 【MySQL】SQL语句
- Based on STC8G2K64S4 single-chip microcomputer to display analog photosensitive analog value through OLED screen
猜你喜欢

iwemeta元宇宙:阿里首任COO:如何打造销售铁军

DGIOT三千万电表集抄压测

每日一题,二叉树中增加一行

WooCommerce 安装和 rest api 使用

navicat for mysql 连接时报错:1251-Client does not support authentication protocol requested by server

CV+Deep Learning——网络架构Pytorch复现系列——classification(三:MobileNet,ShuffleNet)

2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge

Rust learning: 6.1_Slices of composite types

自动化测试框架Pytest(二)——前后置处理

PLSQL学习第一天
随机推荐
关于数据中心的设计方案,数据中心网络规划设计
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
placeholder 1
MySQL索引事务
Rust学习:6.1_复合类型之切片
Complex AB experiment
PLSQL学习第三天
自动化测试框架Pytest(二)——前后置处理
Uni-app develops WeChat applet using local images as background images
PLSQL学习第四天
解决win10win7win8系统找不到指定的模块,注册不了大漠插件的问题
Uni applet Tencent map polygon background transparency
Add spark related dependencies and packaging plugins (sixth bullet)
人工神经网络工作原理,神经网络的工作原理
浅谈C语言整型数据的存储
二叉树 --- 堆
DGIOT三千万电表集抄压测
【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
PLSQL学习第二天
SCS【2】单细胞转录组 之 cellranger