Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get effective background color of transparent element with javascript/jQuery

Basically, I have an element on the page that may be inserted in multiple different locations. It has no background color set but depending on the background color of where it's inserted I may want to make a couple of changes to it.

My question is: how to get the effective background-color for an element even if that background-color may have been inherited from a parent (potentially all the way to body).

Here's a simplified example where I would like to find the background-color of .color-me

<div class="cont">
   <div class="color-me">what's my background-color?</div>
</div>

And a codepen: http://codepen.io/evanhobbs/pen/FbAnL/

like image 495
Evan Hobbs Avatar asked Apr 07 '14 16:04

Evan Hobbs


2 Answers

This is possible given some (pretty major) caveats. zzzzBov summed them up well in a comment on the question:

... the complexity comes in when you need to start taking into account background images, gradients, rgba values, and opacity. It's relatively simple to get the color of the nearest parent with a non-transparent background color, it just might not be the actual color behind the element.

With those caveats in place, basically you have to get the background color of the element and, if it's transparent, go to the parent element, and so on.

Different browsers have different views of "transparent", here's an example covering most of them: Updated Pen

$(function() {
  var bc;
  var $elm = $(".color-me");
  while (isTransparent(bc = $elm.css("background-color"))) {
    if ($elm.is("body")) {
      console.log("Gave up");
      return;
    }
    $elm = $elm.parent();
  }
  console.log("Effective color: " + bc);

  function isTransparent(color) {
    switch ((color || "").replace(/\s+/g, '').toLowerCase()) {
      case "transparent":
      case "":
      case "rgba(0,0,0,0)":
        return true;
      default:
        return false;
    }
  }
});

I'm not at all sure this is possible with all browsers, particularly not ones that give back an rgb (not rgba) value for the computed color.

like image 173
T.J. Crowder Avatar answered Sep 29 '22 22:09

T.J. Crowder


I much prefer jsfiddle but here is your updated codepen.. http://codepen.io/anon/pen/nhDGC Added anonymous function so to use it just call $('.yourselector').getBg();

jQuery.fn.getBg = function() {
return $(this).parents().filter(function() {

    var color = $(this).css('background-color');

    if(color != 'transparent' && color != 'rgba(0, 0, 0, 0)' && color != undefined) 
    { return color; 
    }
}).css('background-color');
};

$(function() {
  //console.log($('.cont').css('background-color'));
   console.log($('.color-me').getBg())
   $('.color-me').append($('.color-me').getBg());

   console.log($('.color-me2').getBg())
   $('.color-me2').append($('.color-me2').getBg());
 });

It filters parents + will return when it finds the background color existing.

like image 30
JF it Avatar answered Sep 29 '22 22:09

JF it