代码之家  ›  专栏  ›  技术社区  ›  Tiny Giant Studios

jQuery:HEX到RGB的计算在浏览器之间是不同的?

  •  2
  • Tiny Giant Studios  · 技术社区  · 15 年前

    下面这段代码在所有浏览器中都能很好地工作,就像往常一样。 这就是需要发生的事情:

    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);
    });
    

    我期待着你大师的消息。

    2 回复  |  直到 8 年前
        1
  •  7
  •   icyrock.com    15 年前

    不用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];
    }
    
        2
  •  0
  •   Tiny Giant Studios    15 年前

    在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);
    });