使用 JS 获取元素 CSS3 背景颜色渐变

Har*_*ley 5 css jquery gradient background find

目前我使用以下 JS (jQuery) 来查找其他几个 div 的背景颜色(作为 rgb):

$theColor = $(this).css("background-color");
Run Code Online (Sandbox Code Playgroud)

它工作得很好,除了 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 */
Run Code Online (Sandbox Code Playgroud)

jQuery 似乎什么也没有。

如何使用 jQuery 找到至少一种在 css3 渐变中使用的颜色?我对 JS 比较陌生,所以请多多包涵..

谢谢你。

Dr.*_*lle 2

您需要为渐变创建一个 cssHook (例如 jQuery 有一个为opacity实现的钩子)。

请参阅: 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);
Run Code Online (Sandbox Code Playgroud)

正如我所说,这只是一个如何使用 cssHooks 的示例,并不意味着用于生产用途。适用于我的 ff、chrome、IE9、safari。如果您点击 RickV 发布的链接,可以找到设置函数。

用法:$('selector').css('linearGradientColors')
返回:包含颜色的数组