2

I have a white menu button that has a fixed position. I have a section on my website that has a white background color. My goals is that when the menu button scrolls over the white section it turns black instead of white.

I just have no idea how I can detect the background color of my fixed element? Is this even possible?

here's what my jQuery looks like:

$color = how to detect bg color?;

if ($color == "#fff"){
    $("nav-icon2").css("color", "#000");
}else{
    $("nav-icon2").css("color", "#fff");
}
Frank Lucas
  • 582
  • 2
  • 12
  • 28
  • Possible duplicate of [How to set background color in jquery](http://stackoverflow.com/questions/4780670/how-to-set-background-color-in-jquery) – geeksal Mar 30 '16 at 15:07

3 Answers3

2

Here's a working fiddle based on this solution : How to get the background color code of an element?

See this fiddle

var color = '';
var x = $(".nav-icon2").css('backgroundColor');
hexc(x);
alert(color);

function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    color = '#' + parts.join('');
}

EDIT :

If I understand correctly your issue, you wants this :

See this updated fiddle

$(window).scroll(function(){
    if($(window).scrollTop() > 350){
    $("h2").css('color','#f00');
  }else{
        $("h2").css('color','#fff');
  }
});
Community
  • 1
  • 1
Vincent G
  • 8,547
  • 1
  • 18
  • 36
0

Simple!

var $color = $('nav-icon2').css('background-color');
jonny
  • 3,022
  • 1
  • 17
  • 30
0

You can do this with:

$color = $( "your-element" ).css( "backgroundColor" );
Michael
  • 161
  • 1
  • 1
  • 7