当前位置:网站首页>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
边栏推荐
- CV+Deep Learning - network architecture Pytorch recurrence series - classification (3: MobileNet, ShuffleNet)
- Quickly enter the current date and time
- 【NeRF】原始论文解读
- Rust learning: 6.4_ enumeration of composite types
- UGUI—事件,iTween插件
- Compilation failure:找不到符号
- Using the color picker
- Rust学习:6.2_复合类型之元组
- 2022-08-01 网工进阶(二十三) VLAN高级技术-VLAN聚合、MUX VLAN
- 90. (cesium house) cesium height monitoring events
猜你喜欢

ATH10 sensor reads temperature and humidity

张驰课堂:老板会武术,谁也挡不住!六西格玛培训的魅力

2022-08-01 Advanced Network Engineering (23) Advanced VLAN Technology - VLAN Aggregation, MUX VLAN

明明加了唯一索引,为什么还是产生重复数据?

js函数聚合的三种实现方式

解决win10win7win8系统找不到指定的模块,注册不了大漠插件的问题

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

C# 获取PCI等设备的插槽位置信息

同步锁synchronized追本溯源

CV+Deep Learning - network architecture Pytorch recurrence series - classification (3: MobileNet, ShuffleNet)
随机推荐
Complex AB experiment
TensorFlow 2.9的零零碎碎(一)
上课笔记(7)(1)——#647. 找树根和孩子(root)
探索神经网络架构教程视频,设计神经网络的步骤
Binary tree --- heap
关于数据中心的设计方案,数据中心网络规划设计
foreach遍历删除元素问题总结
Day36 LeetCode
placeholder 1
2022-08-01 网工进阶(二十四) STP进阶知识
初使jest 单元测试
人工神经网络工作原理,神经网络的工作原理
VS2013-调试汇编代码-生成asm文件-结构体内存布局-函数参数压栈-调用约定
机器人控制器编程实践指导书旧版-实践二 传感器(模拟量)
进程管理(动态的)
ABAP Data Types 和XSD Type 映射关系以及XSD Type属性
幂函数 指数函数 对数函数
Is the write performance of raid5 faster than raid10?
Using the color picker
人工神经网络模型的特点,人工神经网络模型定义