当前位置:网站首页>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 texthtml
.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
边栏推荐
- 基于sklearn的决策树应用实战
- SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
- [In-depth study of 4G/5G/6G topic-56]: L3 signaling control-5-radio bearer management
- Rust learning: 6.5_Array of composite types
- CV-人脸识别-2018:ArcFace
- 【Rust指南】使用Cargo工具高效创建Rust项目 | 理解Rust特别的输入输出语句
- Delphi实现的一个文件在线查询显示下载功能
- 差分、前缀和模板
- 2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
- iwemeta metaverse: a doll sells for 9999 yuan, and Bubble Mart thinks it is not expensive at all
猜你喜欢

Delphi实现的一个文件在线查询显示下载功能

DGIOT 30 million meters set pressure reading

34. 谈谈为什么要拆分数据库?有哪些方法?

物联网时代下的网络整合推广外包精准化效果-深圳双赢世讯

Rust学习:6.3_复合类型之元组

day16--抓包工具Charles的使用

【MySQL】使用MySQL Workbench软件新建表

NaiveUI中看起来没啥用的组件(文字渐变)实现原来这么简单

VS2013-debug assembly code-generate asm file-structure memory layout-function parameter stack-calling convention

Solve the problem that the win10win7win8 system cannot find the specified module and cannot register the desert plug-in
随机推荐
CV-人脸识别-2018:ArcFace
ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
【一起学Rust | 进阶篇 | RMQTT库】RMQTT消息服务器——安装与集群配置
Rust学习:6.3_复合类型之元组
.NET-8. My Thought Notes
2022-08-01 Advanced Network Engineering (24) STP Advanced Knowledge
手把手教你进行Mysql查询操作
2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
【转】探秘钉钉的即时消息服务DTIM
详解构建mock服务最方便的神器——Moco
卷积神经网络卷积层公式,卷积神经网络运算公式
Uni-app develops WeChat applet using local images as background images
SQL SERVER 数据库,表的数据发生增删改,该表的索引会在ldf日志中记录吗?
[In-depth study of 4G/5G/6G topic-56]: L3 signaling control-5-radio bearer management
张驰课堂:老板会武术,谁也挡不住!六西格玛培训的魅力
明明加了唯一索引,为什么还是产生重复数据?
Rust学习:6.2_复合类型之元组
短视频同城流量宣传小魔推有何优势?如何给实体商家带来销量?
Unity—UGUI控件
phpstudy开机自启