天天看点

【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考

 JS方式实现文本或按钮背景渐变色

我们可以参考HarmonyOS ​​linear-gradient​​ 

【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考

如下是动态设置代码

hml

<div class="container">
    <text class="title"
          style="background:{{mbackground}}">
        {{ $t('strings.hello') }} {{ title }}
    </text>
</div>      
【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考

css

.container {
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width:100%;
    height:100%;
    background-color: white;
}
 
.title {
    font-size:40px;
    color:#000000;
    opacity:0.9;
}      
【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考

Js

export default {

    data: {

        title: "",

        mbackground:"{ \"values\":[{\"type\":\"linearGradient\",\"directions\":[\"to\",\"bottom\"],\"values\":[\"rgb(255,0,0)\",\"rgb(0,0,255)\"]}]}"

    },

    onInit() {

        this.title = this.$t('strings.world');

    },

}      
【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考

效果图如下

【HarmonyOS】【ArkUI】鸿蒙 linear-gradient 来实现渐变色,怎么动态设置呢?尝试了一下,供大家参考