用JS获取元素CSS3背景色渐变
目前,我使用以下 JS (jQuery) 来查找其他几个 div 的背景颜色(作为 rgb):
$theColor = $(this).css("background-color");
它工作得很好,除了 CSS3 渐变。
作为示例,我使用以下 css 使 div 的背景看起来类似于便利贴:
background: #FFFAAD; /* old browsers */
background: -moz-linear-gradient(top, #FFFAAD 0%, #FFF47D 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFAAD), color-stop(100%,#FFF47D)); /* webkit */
background: gradient(linear, left top, left bottom, color-stop(0%,#FFFAAD), color-stop(100%,#FFF47D));
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#FFFAAD', endColorstr='#FFF47D',GradientType=0 ); /* ie */
jQuery 似乎没有拾取任何内容。
如何使用 jQuery 查找 css3 渐变中使用的至少一种颜色? 我对 JS 比较陌生,所以请耐心等待..
谢谢。
At the moment I use the following JS (jQuery) to find the background color (as rgb) of several other divs:
$theColor = $(this).css("background-color");
It works perfectly, except with CSS3 gradients.
As an example, I have the following css to make the background of a div look similar to a post-it note:
background: #FFFAAD; /* old browsers */
background: -moz-linear-gradient(top, #FFFAAD 0%, #FFF47D 100%); /* firefox */
background: -webkit-gradient(linear, left top, left bottom, color-stop(0%,#FFFAAD), color-stop(100%,#FFF47D)); /* webkit */
background: gradient(linear, left top, left bottom, color-stop(0%,#FFFAAD), color-stop(100%,#FFF47D));
filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='#FFFAAD', endColorstr='#FFF47D',GradientType=0 ); /* ie */
jQuery doesn't seem to pick up anything.
How can I use jQuery to find at least one of the colors used in a css3 gradient?
I am relatively new to JS, so please bear with me..
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
就像指出的那样,使用 CSS Hooks 来做到这一点。
您可以在这里找到满足您需求的示例:http://www.webmuse。 co.uk/articles/csshooks-in-jquery/。
Like pointed, use CSS Hooks to do it.
You will find a sample with your need here: http://www.webmuse.co.uk/articles/csshooks-in-jquery/.
您需要为渐变创建一个cssHook(例如,jQuery有一个为不透明度实现的挂钩)。
请参阅: http://api.jquery.com/jQuery.cssHooks/
根据要求的示例- 用于检索颜色的代码:
正如我所说,这只是一个如何使用 cssHooks 的示例,并不意味着用于生产用途。适用于 ff、chrome、IE9、safari。
如果您点击 RickV 发布的链接,可以找到设置函数。
用法:
$('selector').css('linearGradientColors')
返回:包含颜色的数组
You'll need to create an cssHook for gradient (jQuery has for example an hook implemented for opacity).
See: http://api.jquery.com/jQuery.cssHooks/
As requested an example-code for retrieving the colors:
As I said it's just an example how to work with cssHooks, not meant for production usage. Works for me in ff, chrome, IE9, safari.
A set-function can be found if you follow the link posted by RickV.
Usage:
$('selector').css('linearGradientColors')
Return: an array with the colors
您可以通过查看元素的
background-image
属性来提取渐变中使用的颜色,然后提取列出的颜色。 这是一个示例,它使用 CSS 颜色匹配正则表达式来自这篇文章。我刚刚将代码绑定到带有背景的元素的onclick
事件:请注意,RegEx 用于 CSS2 颜色,因此它不会匹配任何
rgba()
或hsla()
颜色,但如果需要,您应该可以扩展它。You can extract the colours used in the gradient by looking at the
background-image
property of the element and then extracting the listed colours. Here's an example, it's using the CSS colour matching RegEx from this post. I've just bound the code to theonclick
event of the elements with the background:Note that the RegEx is for CSS2 colours, so it won't match any
rgba()
orhsla()
colours but it ought to be possible for you to extend it if necessary.