更新记录
1.0.0(2025-12-07)
下载此版本
appx-gradient-text 是基于 UniAppX 开发的渐变文本组件,通过 background-clip: text 实现文字渐变效果,支持自定义渐变方向、渐变颜色、字体样式等,适配小程序、H5、App 等多端场景,且提供灵活的样式自定义能力。
平台兼容性
uni-app x(4.87)
| Chrome |
Safari |
Android |
iOS |
鸿蒙 |
微信小程序 |
| √ |
√ |
√ |
√ |
√ |
√ |
appx-gradient-text 渐变文本组件文档
组件介绍
appx-gradient-text 是基于 UniAppX 开发的渐变文本组件,通过 background-clip: text 实现文字渐变效果,支持自定义渐变方向、渐变颜色、字体样式等,适配小程序、H5、App 等多端场景,且提供灵活的样式自定义能力。
组件属性(Props)
| 属性名 |
类型 |
默认值 |
说明 |
| text |
string |
'渐变文本' |
渐变文本显示的内容 |
| gradientDirection |
string |
'to right' |
渐变方向,支持 to left/right/top/bottom 方向值,或角度值(如 45deg) |
| gradientColors |
string[] |
['#409eff', '#67c23a'] |
渐变颜色组,数组形式传递,如 ['#ff0000', '#00ff00', '#0000ff'] |
| fontSize |
string / number |
'16px' |
字体大小,支持数字(会自动补 px)或带单位字符串(如 20rpx、1.2rem) |
| fontWeight |
string / number |
'normal' |
字体粗细,支持 normal/bold 或 100-900 数值 |
| lineHeight |
string / number |
'1.5' |
行高,支持数字(相对字体大小)或带单位字符串 |
| letterSpacing |
string / number |
'0px' |
字间距,支持数字(自动补 px)或带单位字符串 |
| textStyle |
object / string / null / undefined |
{} |
文本自定义样式,支持对象形式(如 { padding: '10px' })或合法的 JSON 字符串(单引号可兼容) |
| containerStyle |
object / string / null / undefined |
{} |
容器自定义样式,格式同 textStyle |
| noSelect |
boolean / string |
true |
是否禁止文本选中,支持布尔值或字符串('true'/'false'),默认禁止 |
组件事件
| 事件名 |
说明 |
回调参数 |
| click |
点击渐变文本时触发 |
无 |
组件使用示例
基础使用
<template>
<appx-gradient-text
text="基础渐变文本"
/>
</template>
<script lang="uts" setup>
</script>
自定义渐变方向和颜色
<template>
<appx-gradient-text
text="45度渐变文本"
gradientDirection="45deg"
:gradientColors="['#ff6700', '#ffd100', '#ff0080']"
/>
</template>
<script lang="uts" setup>
</script>
自定义字体样式和交互
<template>
<appx-gradient-text
text="可选中的渐变文本"
fontSize="24px"
fontWeight="bold"
letterSpacing="2px"
:noSelect="false"
:textStyle="{ padding: '10px 20px' }"
:containerStyle="{ backgroundColor: '#f5f5f5', borderRadius: '8px' }"
@click="handleTextClick"
/>
</template>
<script lang="uts" setup>
const handleTextClick = () => {
uni.showToast({ title: '点击了渐变文本', icon: 'none' });
};
</script>
样式传字符串格式(兼容场景)
<template>
<appx-gradient-text
text="样式字符串传参"
textStyle="{ 'color': 'red', 'padding': '5px' }"
containerStyle="'backgroundColor': '#eee', 'margin': '10px'"
/>
</template>
<script lang="uts" setup>
</script>
注意事项
- 渐变文本效果依赖
background-clip: text,部分低版本移动端系统/浏览器可能存在兼容问题,建议做好降级方案;
textStyle/containerStyle 传字符串时,需保证格式接近 JSON(组件已兼容单引号、无引号键名,但复杂格式可能解析失败),优先推荐传对象;
noSelect 属性的 WebkitUserSelect 适配了小程序/移动端,H5 端也可正常生效;
- 组件容器默认是
inline-block 类型,可通过 containerStyle 修改为 block 等类型实现宽度占满。