如何在JavaScript中将矩形的css更改为渐变



所以基本上我正在开发梯度生成器,但梯度不会出现。我目前有

<rect id="preview" class="preview" width="300" height="50" style="fill:rgb(0,0,0)" />

在html和中

document.getElementById("preview").style.property = "fill: linear-gradient(90deg, rgba(2,0,36,1) 0%, rgba(9,121,103,1) 50%, rgba(0,212,255,1) 100%);"

在javascript中(应该给出一个很好的绿色到青色的渐变(,但它就是不起作用,它显示为黑色,仅此而已,不知道该做什么以及如何修复它

正如Robert所说,现在您需要使用SVG<linearGradient>元素。

document.getElementById("preview").style = "fill: url(#grad);"
<svg>
<defs>
<linearGradient id="grad">
<stop offset="0%" stop-color="rgba(2,0,36,1)"/>
<stop offset="50%" stop-color="rgba(9,121,103,1)"/>
<stop offset="100%" stop-color="rgba(0,212,255,1)"/>
</linearGradient>
</defs>
<rect id="preview" class="preview" width="300" height="50" style="fill:rgb(0,0,0)" />
</svg>

最新更新