【问题标题】:jQuery: HEX to RGB calculation different between browsers?jQuery:浏览器之间的 HEX 到 RGB 计算不同?
【发布时间】:2011-05-14 19:52:15
【问题描述】:

以下代码在所有浏览器中都能完美运行,除了 IE。照常。 这就是需要发生的事情:

  1. 将鼠标悬停在链接上时,获取该链接 链接颜色。
  2. 获取该颜色的RGB值, 将始终视为基本 CSS 设置为十六进制值;
  3. 使用新的 RGB 值并进行计算以确定该颜色的较浅色调
  4. 在 0.5 秒内为新的较浅阴影设置动画
  5. 将鼠标移开时,恢复 颜色恢复到原来的值

正如我所说,到目前为止,代码运行良好,除了在 IE 中。任何有一双新眼睛(和完整的发际线)的人都可以看看这个吗?可以不一样吗?

function getRGB(color) {
    // Function used to determine the RGB colour value that was passed as HEX
    var result;

    // Look for rgb(num,num,num)
    if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];

    // Look for rgb(num%,num%,num%)
    if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55];

    // Look for #a0b1c2
    if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];

    // Look for #fff
    if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)];
}


var $oldColour;
// Get all the links I want to target
$('a').not('aside.meta a.notes_link, aside.meta ul li a, section.social_media a, footer a').hover(function() {
    //code when hover over
    //set the old colour as a variable so we can animate to that value when hovering away
    $oldColour = $(this).css('color');

    //run the getRGB function to get RGB value of the link we're hovering over
    var rgb = getRGB($(this).css('color'));

    for (var i = 0; i < rgb.length; i++)
        //for each of the 3 HEX values, determine if the value + an increment of 30 (for a lighter colour) is lighter than the max (255)
        //if it is, use the HEX value plus the increment, else use the max value
        rgb[i] = Math.min(rgb[i] + 30, 255);

        //join the new three new hex pairs together to form a sinle RGB statement
        var newColor = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';

    //animate the text link color to the new color.
    $(this).animate({'color': newColor}, 500);

}, function() {
    //code when hovering away
    //animate the colour back using the old colour determined above
    $(this).animate({'color': $oldColour}, 500);
});

期待大师的来信。

【问题讨论】:

    标签: jquery colors hex jquery-animate rgb


    【解决方案1】:

    在 icyrock 的帮助下,最终的代码如下所示:

    function getRGB(color) {
        var result;
    
        // Look for rgb(num,num,num)
        if (result = /rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(color)) return [parseInt(result[1]), parseInt(result[2]), parseInt(result[3])];
    
        // Look for rgb(num%,num%,num%)
        if (result = /rgb\(\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*,\s*([0-9]+(?:\.[0-9]+)?)\%\s*\)/.exec(color)) return [parseFloat(result[1]) * 2.55, parseFloat(result[2]) * 2.55, parseFloat(result[3]) * 2.55];
    
        // Look for #a0b1c2
        if (result = /#([a-fA-F0-9]{2})([a-fA-F0-9]{2})([a-fA-F0-9]{2})/.exec(color)) return [parseInt(result[1], 16), parseInt(result[2], 16), parseInt(result[3], 16)];
    
        // Look for #fff
        if (result = /#([a-fA-F0-9])([a-fA-F0-9])([a-fA-F0-9])/.exec(color)) return [parseInt(result[1] + result[1], 16), parseInt(result[2] + result[2], 16), parseInt(result[3] + result[3], 16)];
    }
    
    
    var $oldColour;
    
    $('a').not('aside.meta a.notes_link, aside.meta ul li a, section.social_media a, footer a').hover(function() {
        //code when hover over
        $(this).stop(true, true);
        $oldColour = $(this).css('color');
    
        var rgb;
    
        function hex2rgb(hexStr){
            // note: hexStr should be #rrggbb
            var hex = parseInt(hexStr.substring(1), 16);
            var r = (hex & 0xff0000) >> 16;
            var g = (hex & 0x00ff00) >> 8;
            var b = hex & 0x0000ff;
            return [r, g, b];
        }
    
    
        if($oldColour.substring(0, 3) == 'rgb') {
           rgb = getRGB($oldColour);
        } else { // it's a hex
           rgb = hex2rgb($oldColour);
        }
    
        for (var i = 0; i < rgb.length; i++)
            rgb[i] = Math.min(rgb[i] + 30, 255);
            var newColor = 'rgb(' + rgb[0] + ',' + rgb[1] + ',' + rgb[2] + ')';
    
        $(this).animate({'color': newColor}, 500);
    
    }, function() {
        //code when hovering away
        $(this).stop(true, true);
        $(this).animate({'color': $oldColour}, 500);
    });
    

    【讨论】:

      【解决方案2】:

      没有 IE 来测试它,但如果问题只是第一次显示,请尝试使用 setTimeout 并以非常小的超时(10 毫秒左右)来第二次调用您的代码。

      另外,找出代码的哪一部分不起作用可能是值得的 - 我想$oldColour = $(this).css('color');,但添加一些 console.log 并找出,它可能会有所帮助,你甚至可能会发现其他东西是发生在你现在看不到的事情。

      编辑:像这样的东西:

      $oldColour = $(this).css('color');
      var rgb;
      if($oldColour.substring(0, 3) == 'rgb') {
         rgb = getRGB($oldColour);
      } else { // it's a hex
         rgb = getFromHex($oldColour);
      }
      

      其中 getFromHex 可以类似于来自 http://www.richieyan.com/blog/article.php?artID=32 的那个,修改后可以按您的预期工作:

      function hex2rgb(hexStr){
          // note: hexStr should be #rrggbb
          var hex = parseInt(hexStr.substring(1), 16);
          var r = (hex & 0xff0000) >> 16;
          var g = (hex & 0x00ff00) >> 8;
          var b = hex & 0x0000ff;
          return [r, g, b];
      }
      

      【讨论】:

      • @icyrock - 感谢您的输入,我会尝试 setTimeout 的想法... 至于 console.log 的想法,console.logged 我自己死了:非 IE 浏览器将 $oldColour 视为 RGB值,而 IE 将其视为 HEX。第一次悬停后,IE 将其视为 RGB 值。叹息。
      • 好的,如果这是唯一的问题,你为什么不检查'rgb'字符串,如果它在开头,则解析rgb,否则解析十六进制(此处的示例函数:linuxtopia.org/online_books/javascript_guides/javascript_faq/… )?您可以尝试的另一个技巧是为此“保留”一个愚蠢的元素,加载$(...).css() 给你的任何东西,将该元素的color CSS 属性设置为该值并尝试从中读取 - 它可能会给你rgb值。
      • Eish,显然我是一个 jQuery 菜鸟......理论上,我明白你的意思,但我对如何做到这一点有点无能为力:)
      • 顺便说一句 - 非常感谢您花在这方面的时间,我真的很感激...
      • 完美!与 Stack Overflow 的忍者一样 - 我欠你的债。谢谢你 icyrock!
      猜你喜欢
      • 2016-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      相关资源
      最近更新 更多