Skip to content Skip to sidebar Skip to footer

Color Change Hamburger Menu Depending On Background Color

There is a white burger menu. The problem is that in my project in some places of the site it is not visible due to the opaque background. The task is to write a script that should

Solution 1:

Rather than approach this with JavaScript, you might find a CSS based solution using mix-blend-mode to be a more robust and flexible alternative.

The mix-blend-mode property allows you to control the blending behavior of an element against it's background. In your case, the contrasted appearance can be achieved via the "difference" blend mode to ensure the burger bars are visible in most cases.

The difference blend mode can be thought of as "color inversion" - when applied to your burger menu, this would ensure that the menu is visible under most backgrounds. The other nice thing about this approach is that it will work against most solid color, image or gradient backgrounds.

You can make use of blending for your menu by adding the following CSS:

#menuToggle {
    mix-blend-mode: difference; /* Invert color of menu from background */
}

#menuTogglespan {
    background:white; /* Ensure full blend contrast with background */
}

Here's a working snippet below as well:

@import url(https://fonts.googleapis.com/css?family=Quicksand);
html,
body {
  height: 100%;
  background-color: red;
  font-family: 'Quicksand', sans-serif;
  color: white;
}

#menuToggle {
  display: block;
  position: fixed;
  top: 50px;
  left: 50px;
  z-index: 1;
  -webkit-user-select: none;
  user-select: none;
  
    mix-blend-mode: difference;
}

#menuToggleinput {
  display: block;
  width: 40px;
  height: 32px;
  position: absolute;
  top: -7px;
  left: -5px;
  cursor: pointer;
  opacity: 0;
  /* hide this */z-index: 2;
  /* and place it over the hamburger */
  -webkit-touch-callout: none;
}


/*
 * Just a quick hamburger
 */#menuTogglespan {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative; 
  border-radius: 3px;
  z-index: 1;
  transform-origin: 4px0px;
  transition: transform 0.5scubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5scubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
  
  background:white;
}

#menuTogglespan:first-child {
  transform-origin: 0%0%;
}

#menuTogglespan:nth-last-child(2) {
  transform-origin: 0%100%;
}


/* 
 * Transform all the slices of hamburger
 * into a crossmark.
 */#menuToggleinput:checked~span {
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: #232323;
}


/*
 * But let's hide the middle one.
 */#menuToggleinput:checked~span:nth-last-child(3) {
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}


/*
 * Ohyeah and the last one should go the other direction
 */#menuToggleinput:checked~span:nth-last-child(2) {
  transform: rotate(-45deg) translate(0, -1px);
}


/*
 * Make this absolute positioned
 * at the top left of the screen
 */#menu {
  position: absolute;
  width: 300px;
  margin: -100px00 -50px;
  padding: 50px;
  padding-top: 125px;
  background: #ededed;
  list-style-type: none;
  -webkit-font-smoothing: antialiased;
  /* to stop flickering of text in safari */transform-origin: 0%0%;
  transform: translate(-100%, 0);
  transition: transform 0.5scubic-bezier(0.77, 0.2, 0.05, 1.0);
}

#menuli {
  padding: 10px0;
  font-size: 22px;
}


/*
 * And let's slide it in from the left
 */#menuToggleinput:checked~ul {
  transform: none;
}
<!--    Made by Erik Terwan    --><!--   24th of November 2015   --><!--        MIT License        --><navrole="navigation"><divid="menuToggle"><!--
    A fake / hidden checkbox is used as click reciever,
    so you can use the :checked selector on it.
    --><inputtype="checkbox" /><!--
    Some spans to act as a hamburger.
    
    They are acting like a real hamburger,
    not that McDonalds stuff.
    --><span></span><span></span><span></span><!--
    Too bad the menu has to be inside of the button
    but hey, it's pure CSS magic.
    --><ulid="menu"><ahref="#"><li>Home</li></a><ahref="#"><li>About</li></a><ahref="#"><li>Info</li></a><ahref="#"><li>Contact</li></a><ahref="https://erikterwan.com/"target="_blank"><li>Show me more</li></a></ul></div></nav><divclass="container" #slide-1><divclass="row"><divclass="col-sm-10 col-sm-push-1 col-md-8 col-md-push-2"><h1>Full Screen Burger Menu</h1><h3>Click the burger.</h3><p>Lucas ipsum dolor sit amet darth lando qui-gonn mara droid jade qui-gonn skywalker wedge lars. Ben antilles chewbacca palpatine mothma fisto binks mara. Wicket greedo c-3p0 han lars mara darth. Darth binks wookiee mara luuke amidala. Lando kessel
        jango wicket. Moff jango dantooine luuke skywalker obi-wan. Darth mandalore binks leia solo moff yoda. Darth c-3p0 maul ben jar lando. Jabba ahsoka grievous mandalorians kenobi wookiee chewbacca. Binks calrissian tatooine baba jango k-3po darth
        calamari yavin.</p><p>
        Wicket sidious organa skywalker mandalore mon qui-gonn fisto. Leia grievous biggs bespin calamari. Alderaan luuke darth hutt. Vader leia boba kashyyyk dagobah jade kashyyyk. Lars amidala gonk lobot organa. Twi'lek darth gamorrean obi-wan boba moff. Yavin
        c-3p0 darth darth luke chewbacca han. Skywalker dooku c-3po owen. Leia jade alderaan jar dooku utapau tusken raider dantooine jawa. Anakin leia kessel jango bespin lando organa tatooine tusken raider. Mara ackbar mara calamari hutt qui-gonn dantooine.
      </p><p>
        Yoda sidious lando padmé chewbacca fett. Maul calrissian boba lobot jade darth obi-wan. Wookiee darth ahsoka dagobah darth sidious naboo lars. Dooku skywalker skywalker ben luke windu. Wicket darth coruscant darth coruscant organa palpatine yoda padmé.
        Darth leia organa gonk lando mon mara hutt. Wookiee darth padmé windu hutt. Darth cade maul padmé grievous greedo moff dooku solo. Coruscant twi'lek dooku moff skywalker.
      </p></div></div></div><divclass="container"style="background-color:#000;width:100%;"><divclass="row"><divclass="col-sm-10 col-sm-push-1 col-md-8 col-md-push-2"><h1>Full Screen Burger Menu</h1><h3>Click the burger.</h3><p>Lucas ipsum dolor sit amet darth lando qui-gonn mara droid jade qui-gonn skywalker wedge lars. Ben antilles chewbacca palpatine mothma fisto binks mara. Wicket greedo c-3p0 han lars mara darth. Darth binks wookiee mara luuke amidala. Lando kessel
        jango wicket. Moff jango dantooine luuke skywalker obi-wan. Darth mandalore binks leia solo moff yoda. Darth c-3p0 maul ben jar lando. Jabba ahsoka grievous mandalorians kenobi wookiee chewbacca. Binks calrissian tatooine baba jango k-3po darth
        calamari yavin.</p><p>
        Wicket sidious organa skywalker mandalore mon qui-gonn fisto. Leia grievous biggs bespin calamari. Alderaan luuke darth hutt. Vader leia boba kashyyyk dagobah jade kashyyyk. Lars amidala gonk lobot organa. Twi'lek darth gamorrean obi-wan boba moff. Yavin
        c-3p0 darth darth luke chewbacca han. Skywalker dooku c-3po owen. Leia jade alderaan jar dooku utapau tusken raider dantooine jawa. Anakin leia kessel jango bespin lando organa tatooine tusken raider. Mara ackbar mara calamari hutt qui-gonn dantooine.
      </p><p>
        Yoda sidious lando padmé chewbacca fett. Maul calrissian boba lobot jade darth obi-wan. Wookiee darth ahsoka dagobah darth sidious naboo lars. Dooku skywalker skywalker ben luke windu. Wicket darth coruscant darth coruscant organa palpatine yoda padmé.
        Darth leia organa gonk lando mon mara hutt. Wookiee darth padmé windu hutt. Darth cade maul padmé grievous greedo moff dooku solo. Coruscant twi'lek dooku moff skywalker.
      </p></div></div></div>

Solution 2:

You can try following as well, while scrolling you add class then control color through CSS.

$(window).scroll(function() {
  var height = $('#scroll-to').offset().top - 70;
  var sticky = $('#scroll-to'),
    scroll = $(window).scrollTop();

  if (scroll >= height) sticky.parent('body').addClass('change');
  else sticky.parent('body').removeClass('change');
});
@import url(https://fonts.googleapis.com/css?family=Quicksand);
html,
body {
  height: 100%;
  background-color: red;
  font-family: 'Quicksand', sans-serif;
  color: white;
}

#menuToggle {
  display: block;
  position: fixed;
  top: 50px;
  left: 50px;
  z-index: 1;
  -webkit-user-select: none;
  user-select: none;
}

#menuToggleinput {
  display: block;
  width: 40px;
  height: 32px;
  position: absolute;
  top: -7px;
  left: -5px;
  cursor: pointer;
  opacity: 0;
  /* hide this */z-index: 2;
  /* and place it over the hamburger */
  -webkit-touch-callout: none;
}


/*
 * Just a quick hamburger
 */#menuTogglespan {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  background: #000;
  border-radius: 3px;
  z-index: 1;
  transform-origin: 4px0px;
  transition: transform 0.5scubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5scubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
}

#menuTogglespan:first-child {
  transform-origin: 0%0%;
}

#menuTogglespan:nth-last-child(2) {
  transform-origin: 0%100%;
}


/* 
 * Transform all the slices of hamburger
 * into a crossmark.
 */#menuToggleinput:checked~span {
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: #232323;
}


/*
 * But let's hide the middle one.
 */#menuToggleinput:checked~span:nth-last-child(3) {
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}


/*
 * Ohyeah and the last one should go the other direction
 */#menuToggleinput:checked~span:nth-last-child(2) {
  transform: rotate(-45deg) translate(0, -1px);
}


/*
 * Make this absolute positioned
 * at the top left of the screen
 */#menu {
  position: absolute;
  width: 300px;
  margin: -100px00 -50px;
  padding: 50px;
  padding-top: 125px;
  background: #ededed;
  list-style-type: none;
  -webkit-font-smoothing: antialiased;
  /* to stop flickering of text in safari */transform-origin: 0%0%;
  transform: translate(-100%, 0);
  transition: transform 0.5scubic-bezier(0.77, 0.2, 0.05, 1.0);
}

#menuli {
  padding: 10px0;
  font-size: 22px;
}


/*
 * And let's slide it in from the left
 */#menuToggleinput:checked~ul {
  transform: none;
}

.change#menuTogglespan {
  background: #fff;
}
<scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><!--    Made by Erik Terwan    --><!--   24th of November 2015   --><!--        MIT License        --><navrole="navigation"><divid="menuToggle"><!--
    A fake / hidden checkbox is used as click reciever,
    so you can use the :checked selector on it.
    --><inputtype="checkbox" /><!--
    Some spans to act as a hamburger.
    
    They are acting like a real hamburger,
    not that McDonalds stuff.
    --><span></span><span></span><span></span><!--
    Too bad the menu has to be inside of the button
    but hey, it's pure CSS magic.
    --><ulid="menu"><ahref="#"><li>Home</li></a><ahref="#"><li>About</li></a><ahref="#"><li>Info</li></a><ahref="#"><li>Contact</li></a><ahref="https://erikterwan.com/"target="_blank"><li>Show me more</li></a></ul></div></nav><divclass="container" #slide-1><divclass="row"><divclass="col-sm-10 col-sm-push-1 col-md-8 col-md-push-2"><h1>Full Screen Burger Menu</h1><h3>Click the burger.</h3><p>Lucas ipsum dolor sit amet darth lando qui-gonn mara droid jade qui-gonn skywalker wedge lars. Ben antilles chewbacca palpatine mothma fisto binks mara. Wicket greedo c-3p0 han lars mara darth. Darth binks wookiee mara luuke amidala. Lando kessel
        jango wicket. Moff jango dantooine luuke skywalker obi-wan. Darth mandalore binks leia solo moff yoda. Darth c-3p0 maul ben jar lando. Jabba ahsoka grievous mandalorians kenobi wookiee chewbacca. Binks calrissian tatooine baba jango k-3po darth
        calamari yavin.</p><p>
        Wicket sidious organa skywalker mandalore mon qui-gonn fisto. Leia grievous biggs bespin calamari. Alderaan luuke darth hutt. Vader leia boba kashyyyk dagobah jade kashyyyk. Lars amidala gonk lobot organa. Twi'lek darth gamorrean obi-wan boba moff. Yavin
        c-3p0 darth darth luke chewbacca han. Skywalker dooku c-3po owen. Leia jade alderaan jar dooku utapau tusken raider dantooine jawa. Anakin leia kessel jango bespin lando organa tatooine tusken raider. Mara ackbar mara calamari hutt qui-gonn dantooine.
      </p><p>
        Yoda sidious lando padmé chewbacca fett. Maul calrissian boba lobot jade darth obi-wan. Wookiee darth ahsoka dagobah darth sidious naboo lars. Dooku skywalker skywalker ben luke windu. Wicket darth coruscant darth coruscant organa palpatine yoda padmé.
        Darth leia organa gonk lando mon mara hutt. Wookiee darth padmé windu hutt. Darth cade maul padmé grievous greedo moff dooku solo. Coruscant twi'lek dooku moff skywalker.
      </p></div></div></div><divclass="container"id="scroll-to"style="background-color:#000;width:100%;"><divclass="row"><divclass="col-sm-10 col-sm-push-1 col-md-8 col-md-push-2"><h1>Full Screen Burger Menu</h1><h3>Click the burger.</h3><p>Lucas ipsum dolor sit amet darth lando qui-gonn mara droid jade qui-gonn skywalker wedge lars. Ben antilles chewbacca palpatine mothma fisto binks mara. Wicket greedo c-3p0 han lars mara darth. Darth binks wookiee mara luuke amidala. Lando kessel
        jango wicket. Moff jango dantooine luuke skywalker obi-wan. Darth mandalore binks leia solo moff yoda. Darth c-3p0 maul ben jar lando. Jabba ahsoka grievous mandalorians kenobi wookiee chewbacca. Binks calrissian tatooine baba jango k-3po darth
        calamari yavin.</p><p>
        Wicket sidious organa skywalker mandalore mon qui-gonn fisto. Leia grievous biggs bespin calamari. Alderaan luuke darth hutt. Vader leia boba kashyyyk dagobah jade kashyyyk. Lars amidala gonk lobot organa. Twi'lek darth gamorrean obi-wan boba moff. Yavin
        c-3p0 darth darth luke chewbacca han. Skywalker dooku c-3po owen. Leia jade alderaan jar dooku utapau tusken raider dantooine jawa. Anakin leia kessel jango bespin lando organa tatooine tusken raider. Mara ackbar mara calamari hutt qui-gonn dantooine.
      </p><p>
        Yoda sidious lando padmé chewbacca fett. Maul calrissian boba lobot jade darth obi-wan. Wookiee darth ahsoka dagobah darth sidious naboo lars. Dooku skywalker skywalker ben luke windu. Wicket darth coruscant darth coruscant organa palpatine yoda padmé.
        Darth leia organa gonk lando mon mara hutt. Wookiee darth padmé windu hutt. Darth cade maul padmé grievous greedo moff dooku solo. Coruscant twi'lek dooku moff skywalker.
      </p></div></div></div>

Post a Comment for "Color Change Hamburger Menu Depending On Background Color"