您可以使用 CSS 或 JavaScript 创建渐变为不透明的渐变吗?

发布于 2024-10-08 06:42:32 字数 330 浏览 0 评论 0原文

WebKit 引入了创建 CSS 渐变的功能。例如,使用以下代码:

-webkit-gradient(linear, left top, left bottom, from(#fff), to(#000));

但是,是否可以使用 CSS 实现不透明度渐变?我希望渐变一侧为单色,另一侧渐变至零不透明度。

跨浏览器兼容性并不重要;重要的是。我只需要它在 Google Chrome 中工作即可。

有没有办法使用 CSS 来做到这一点?如果没有,可以使用 JavaScript(而不是 jQuery)完成类似的事情吗?

感谢您的帮助。

WebKit has introduced the ability to create CSS gradients. For example, with the following code:

-webkit-gradient(linear, left top, left bottom, from(#fff), to(#000));

However, is it possible to have an opacity gradient using CSS? I want the gradient to be a single color on one side, and fade to zero opacity on the other.

Cross-browser compatibility isn't important; I just need it to work in Google Chrome.

Is there some way to do this using CSS? If not, can something similar be done using JavaScript (not jQuery)?

Thanks for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拔了角的鹿 2024-10-15 06:42:32

是的

,对于颜色,使用 rgba(x, y, z, o),其中 o 是不透明度

应该起作用,

例如

background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 1)), to(rgba(0, 0, 0, 0)));

编辑:
对于最终值(不透明度)1 是不透明的,而 1 是不透明的。 0 为透明

Yes

for the colors, use rgba(x, y, z, o) where o is the opacity

should work

e.g.

background-image: -webkit-gradient(linear, left top, left bottom, from(rgba(255, 255, 255, 1)), to(rgba(0, 0, 0, 0)));

Edit:
For the final value (opacity) 1 is opaque & 0 is transparent

两相知 2024-10-15 06:42:32

是的,您应该使用 rgba(red, green, blue, alpha) http://www.w3.org/TR/css3-color/#rgba-color,示例(在 jsFiddle):

/* Webkit */
background: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(1, rgba(0,0,0,0.0)), /* Top */
    color-stop(0, rgba(0,0,0,1.0)) /* Bottom */
);

/* Gecko */
background: -moz-linear-gradient(
    center bottom,
    rgba(0,0,0,1.0) 0%, /* Bottom */
    rgba(0,0,0,0.0) 100% /* Top */
);

Yup, rgba(red, green, blue, alpha) is what you should use http://www.w3.org/TR/css3-color/#rgba-color, example (Try it out on jsFiddle):

/* Webkit */
background: -webkit-gradient(
    linear,
    left bottom,
    left top,
    color-stop(1, rgba(0,0,0,0.0)), /* Top */
    color-stop(0, rgba(0,0,0,1.0)) /* Bottom */
);

/* Gecko */
background: -moz-linear-gradient(
    center bottom,
    rgba(0,0,0,1.0) 0%, /* Bottom */
    rgba(0,0,0,0.0) 100% /* Top */
);
小伙你站住 2024-10-15 06:42:32

未经测试,但这应该可以工作(在 FF 中可以工作(使用不同的语法)-我不确定 safari/webkit 是否知道 rgba):

-webkit-gradient(linear, left top, left bottom, from(rgba(255,255,255,1)), to(rgba(0,0,0,0)));

not tested, but this should work (in FF this works (with a different syntax) - i'm not sure if safari/webkit knows rgba):

-webkit-gradient(linear, left top, left bottom, from(rgba(255,255,255,1)), to(rgba(0,0,0,0)));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文