当前位置:网站首页>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
边栏推荐
- 同步锁synchronized追本溯源
- VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定
- .NET-8.我的思想笔记
- 二叉树 --- 堆
- Quickly enter the current date and time
- 2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN
- 幂函数 指数函数 对数函数
- 数据库公共字段自动填充
- 自动化测试框架Pytest(三)——自定义allure测试报告
- MySQL database monthly growth problem
猜你喜欢
90. (cesium house) cesium height monitoring events
【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
iwemeta元宇宙:阿里首任COO:如何打造销售铁军
day16--The use of the packet capture tool Charles
自动化测试框架Pytest(一)——入门
Binary tree --- heap
NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单
每日一题,数组字符串的匹配问题
同步锁synchronized追本溯源
【MySQL】使用MySQL Workbench软件新建表
随机推荐
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
CV-人脸识别-2018:ArcFace
If the data of the oracle business table is added, deleted, or modified, will the index of the table write redo and undo?
winget包管理器
人工神经网络工作原理,神经网络的工作原理
phpstudy starts automatically
Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
[机缘参悟-65]:《兵者,诡道也》-7-三十六计解读-败战计
第十六天&charles的基本操作
DGIOT 30 million meters set pressure reading
js函数聚合的三种实现方式
基于sklearn的决策树应用实战
uni 小程序腾讯地图polygon背景透明度
iwemeta元宇宙:一个娃娃卖9999元,泡泡玛特认为一点也不贵
Rust learning: 6.1_Slices of composite types
The precise effect of network integration promotion outsourcing in the era of Internet of Things-Shenzhen Win-Win World News
StringUtils的具体操作
模糊查询除了like+ % 还能用什么方式
同步锁synchronized追本溯源
差分、前缀和模板