你需要爲梯度創建cssHook(jQuery的已經實施,例如用於不透明的鉤)。
參見:http://api.jquery.com/jQuery.cssHooks/
按照要求的例子代碼檢索顏色:
(function($){
if (!$.cssHooks){
//if not, output an error message
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發佈的鏈接,可以找到設置功能。
用法:$('selector').css('linearGradientColors')
返回:與顏色
漸變不是顏色,而是背景圖片。檢查'background-color'不會告訴你關於背景圖像的任何信息。 – robertc 2011-05-08 19:23:24
沒有設置背景圖片;這純粹是css3。我懷疑這些漸變存儲爲圖像(或甚至可能存儲爲圖像)。 – Harley 2011-05-08 19:59:02
有一個背景圖像,漸變是背景圖像。這就是爲什麼你的CSS說'背景:-moz-linear-gradient'而不是'background-color:-moz-linear-gradient' – robertc 2011-05-08 20:14:56