使用 jQuery 应用 css3 渐变
最终,我希望根据不同的事物动态改变渐变,但是如何让 jquery 应用 css3 渐变?
//works
$(element).css("background-image", "url(http://www.google.co.uk/images/logos/ps_logo2.png)");
//doesn't work
$(element).css("background-image","-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255))");
//doesn't work
$(element).css("background-image","-moz-linear-gradient(left center,rgb(194,231,255) 28%,rgb(255,255,255) 28%");
我缺少什么?我也尝试过
$(element).css({background: "-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255)"});
这些方法无效吗?
Eventually, I wish to dynamically alter gradients based on different things, but how do I get jquery to apply a css3 gradient?
//works
$(element).css("background-image", "url(http://www.google.co.uk/images/logos/ps_logo2.png)");
//doesn't work
$(element).css("background-image","-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255))");
//doesn't work
$(element).css("background-image","-moz-linear-gradient(left center,rgb(194,231,255) 28%,rgb(255,255,255) 28%");
What am I missing? I've also tried
$(element).css({background: "-webkit-gradient(linear,left bottom,right bottom,color-stop(0.50, rgb(194,231,255)),color-stop(0.50, rgb(255,255,255)"});
Are these approaches not valid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
你的第二种方法看起来不错...也许你也需要非 webkit 浏览器的 css 样式... 跨浏览器 CSS 渐变
这个对我有用在 Chrome 中
也可以看看位于:http://www.colorzilla.com/gradient-editor/
Your second approach looks OK... Maybe you need to css styles for non-webkit browsers as well... Cross-Browser CSS Gradient
This works for me in Chrome
Also have a look at: http://www.colorzilla.com/gradient-editor/
另一种选择是使用 jQuery
addClass
方法。这允许您动态添加和删除类以及与该类关联的任何格式,包括渐变。Another option is to use the jQuery
addClass
method. This allows you to dynamically add and remove classes and therefore any formatting associated with that class including gradients.这是一个小例子......
Here is a small piece of example...
我在 JSON 格式中使用连字符语法(为了保持一致,我始终使用 JSON 格式)。而且它在 Chrome 和 Firefox 中都运行良好。
例如:
I'm using the hyphenated syntax in the JSON format (I always use the JSON format to be consistent). And it's working fine in both Chrome and Firefox.
For example:
试试这个
try this
在 jquery 中使用 .css() 时,我相信您必须使用属性的缩短版本。例如,margin-left 将是 marginLeft
When using .css() in jquery, I believe you have to use the shortened versions of the attributes. For example, margin-left would be marginLeft