你需要为
渐变(gradient)创建一个cssHook(例如,jQuery已经为
透明度(opacity)实现了一个hook)。
请参见:
http://api.jquery.com/jQuery.cssHooks/
按要求,以下是检索颜色的示例代码:
(function($){
if ( !$.cssHooks ){
alert("jQuery 1.4.3 or above is required for this plugin to work");
return;
}
div = document.createElement( "div" ),
css = "background-image:gradient(linear,left top,right bottom, from(#9f9), to(white));background-image:-webkit-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-moz-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-o-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-ms-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:-khtml-gradient(linear,left top,right bottom,from(#9f9),to(white));background-image:linear-gradient(left top,#9f9, white);background-image:-webkit-linear-gradient(left top,#9f9, white);background-image:-moz-linear-gradient(left top,#9f9, white);background-image:-o-linear-gradient(left top,#9f9, white);background-image:-ms-linear-gradient(left top,#9f9, white);background-image:-khtml-linear-gradient(left top,#9f9, white);";
div.style.cssText = css;
$.support.linearGradient =
div.style.backgroundImage.indexOf( "-moz-linear-gradient" ) > -1 ? '-moz-linear-gradient' :
(div.style.backgroundImage.indexOf( "-webkit-gradient" ) > -1 ? '-webkit-gradient' :
(div.style.backgroundImage.indexOf( "linear-gradient" ) > -1 ? 'linear-gradient' : false));
if ( $.support.linearGradient)
{
$.cssHooks['linearGradientColors'] = {
get: function(elem){
var currentStyle=$.css(elem, 'backgroundImage'),gradient,colors=[];
gradient=currentStyle.match(/gradient(\(.*\))/g);
if(gradient.length)
{
gradient=gradient[0].replace(/(linear|radial|from|\bto\b|gradient|top|left|bottom|right|\d*%)/g,'');
colors= jQuery.grep(gradient.match(/(rgb\([^\)]+\)|#[a-z\d]*|[a-z]*)/g),function (s) { return jQuery.trim( s )!=''})
}
return colors;
}
};
}
})(jQuery);
正如我所说,这只是一个使用cssHooks的示例,不适用于生产环境。在我的ff、chrome、IE9和safari中运行良好。
如果您遵循RickV发布的链接,可以找到一个set-function。
用法:$('选择器').css('linearGradientColors')
返回:一个包含颜色的数组
background-color
不会告诉你任何关于背景图像的信息。 - robertcbackground: -moz-linear-gradient
而不是background-color: -moz-linear-gradient
的原因。 - robertc