当前位置:网站首页>The implementation of the seemingly useless component (text gradient) in NaiveUI is so simple
The implementation of the seemingly useless component (text gradient) in NaiveUI is so simple
2022-08-10 08:13:00 【Web old monkey】
preface
NaiveUI has a very interesting component, the gradient text component, as shown below:

The interesting point is that this text describes _ this thing looks useless, in fact it is useless._
Here we use Vue3.2
+TS
to implement this simple widget.
Gradient text
The implementation of gradient text is relatively simple. It can be achieved by using the background-clip
attribute, which has a text
attribute value, which can use the background as the foreground color of the text, the gradient text can be realized with the gradient, the sample code is as follows:
css
gradient text
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%);}
The code runs as follows:

wrapping gradient component
We now start to use Vue3+TS to encapsulate this gradient component, which is actually very simple, that is, implement different text gradient effects through custom attributes and dynamic classes.
define props
Here we define 4 props, that is, the gradient text has 4 properties, as follows:
type
: Preset gradient effect
size
: the size of the gradient text
weight
: the weight of the gradient text
gradient
: You can customize the gradient color
The implementation code is as follows:
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' | numberinterface IGradient {rotate: RotateType // Linear gradient direction start: string // start color value end: string // end color value}interface Props {type?: TextTypesize?: stringgradient?: IGradientweight?: WeightType}const props = defineProps()
The above is the only TS code in our component, that's all, because there is no logic in this component.
Implementing component effects
First of all, let's define the CSS of the four preset gradient effects. The sample code is as follows:
.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% ); }
Now let's define the content of :
In the above code, the display of different presets and the display of custom gradients are realized through dynamic classes.
There are two operators ??
and ?.
in the above code. These two are new features added in ES2020. If you don't understand, you can pass the followingCheck out this article to learn about all the new features in ECMAScript:
Summaries all new features of [ES2015~ES2022] in 8000 words
The remaining CSS code is as follows:
.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%);}
at the end
This article is finished at this point, the amount of code is super small, and finally this interesting widget is implemented.
This is the entertainment bureau
边栏推荐
- Complex AB experiment
- Rust learning: 6.2_ Tuples of composite types
- 每日一题,数组字符串的匹配问题
- Uni applet Tencent map polygon background transparency
- Process management (dynamic)
- 90. (cesium house) cesium height monitoring events
- Introduction to the delta method
- 问下cdc mysql to doris.不显示具体行数,怎么办?
- [Learn Rust together | Advanced articles | RMQTT library] RMQTT message server - installation and cluster configuration
- In the SQL SERVER database, if the data of the table is added, deleted, or modified, will the index of the table be recorded in the ldf log?
猜你喜欢
时序动作定位 | ASM-Loc:弱监督时序动作定位的动作感知片段建模(CVPR 2022)
本地生活商家如何通过短视频赛道,提升销量曝光量?
VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention
ATH10传感器读取温湿度
关于数据中心的设计方案,数据中心网络规划设计
Binary tree --- heap
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节
同步锁synchronized追本溯源
如何设计神经网络结构,神经网络设计与实现
Using the color picker
随机推荐
协同工具满足70%-90%的工作需求,成为企业香饽饽
Rust学习:6.1_复合类型之切片
WooCommerce 安装和 rest api 使用
MySQL设置初始密码—注意版本mysql-8.0.30
【MySQL】SQL语句
【Unity入门计划】2D游戏实现敌人来回移动控制脚本
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
自动化测试框架搭建 ---- 标记性能较差用例
全连接神经网络结构图,神经网络示意图怎么画
PLSQL学习第一天
深度剖析“八大排序”(上)_ 探寻一些不为人知的细节
34. Talk about why you want to split the database?What methods are there?
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
Go-Excelize API源码阅读(十一)—— GetActiveSheetIndex()
【一起学Rust | 进阶篇 | RMQTT库】RMQTT消息服务器——安装与集群配置
[In-depth study of 4G/5G/6G topic-56]: L3 signaling control-5-radio bearer management
30条实用MySQL优化法则
day16--The use of the packet capture tool Charles
Uni-app开发微信小程序使用本地图片做背景图
二叉树 --- 堆