根据背景颜色更改汉堡菜单的颜色 [英] Color change hamburger menu depending on background color

查看:66
本文介绍了根据背景颜色更改汉堡菜单的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个白色汉堡菜单.问题在于,由于背景不透明,在我的项目中网站的某些位置看不到它.任务是编写一个脚本,该脚本应根据背景颜色将颜色更改为对比色.在下面的代码中,例如,在黑色背景上,菜单应变为白色.最初的计划是为每个块分配一个不同的ID,并且当菜单图标打到新部分时,它会更改颜色.但是由于知识不足,js无法正常工作.还要链接到codepen:

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 change color depending on the background color to a contrasting color. In the code below, for example, on a black background, the menu should turn white. The original plan was to assign a different id to each block, and when the menu icon hits a new section, it changes color. But due to poor knowledge, js did not work. Also link to codepen:

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

#menuToggle input {
  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
 */
#menuToggle span {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  
  background: #000;
  border-radius: 3px;
  
  z-index: 1;
  
  transform-origin: 4px 0px;
  
  transition: transform 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
              background 0.5s cubic-bezier(0.77,0.2,0.05,1.0),
              opacity 0.55s ease;
}

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

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

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

/*
 * But let's hide the middle one.
 */
#menuToggle input: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
 */
#menuToggle input: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: -100px 0 0 -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.5s cubic-bezier(0.77,0.2,0.05,1.0);
}

#menu li {
  padding: 10px 0;
  font-size: 22px;
}

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

<!--    Made by Erik Terwan    -->
<!--   24th of November 2015   -->
<!--        MIT License        -->
<nav role="navigation">
  <div id="menuToggle">
    <!--
    A fake / hidden checkbox is used as click reciever,
    so you can use the :checked selector on it.
    -->
    <input type="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.
    -->
    <ul id="menu">
      <a href="#"><li>Home</li></a>
      <a href="#"><li>About</li></a>
      <a href="#"><li>Info</li></a>
      <a href="#"><li>Contact</li></a>
      <a href="https://erikterwan.com/" target="_blank"><li>Show me more</li></a>
    </ul>
  </div>
</nav>


<div class="container" #slide-1>
   <div class="row">
      <div class="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>

<div class="container" style="background-color:#000;width:100%;">
   <div class="row">
      <div class="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>

推荐答案

您可能会使用

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.

mix-blend-mode 属性允许您控制元素在其背景下的混合行为.在您的情况下,可以通过,以确保在大多数情况下可见汉堡包.

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.

您可以通过添加以下CSS来为菜单使用混合功能:

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 */
}

#menuToggle span {
    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;
}

#menuToggle input {
  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
 */

#menuToggle span {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative; 
  border-radius: 3px;
  z-index: 1;
  transform-origin: 4px 0px;
  transition: transform 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), background 0.5s cubic-bezier(0.77, 0.2, 0.05, 1.0), opacity 0.55s ease;
  
  background:white;
}

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

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


/* 
 * Transform all the slices of hamburger
 * into a crossmark.
 */

#menuToggle input:checked~span {
  opacity: 1;
  transform: rotate(45deg) translate(-2px, -1px);
  background: #232323;
}


/*
 * But let's hide the middle one.
 */

#menuToggle input: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
 */

#menuToggle input: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: -100px 0 0 -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.5s cubic-bezier(0.77, 0.2, 0.05, 1.0);
}

#menu li {
  padding: 10px 0;
  font-size: 22px;
}


/*
 * And let's slide it in from the left
 */

#menuToggle input:checked~ul {
  transform: none;
}

<!--    Made by Erik Terwan    -->
<!--   24th of November 2015   -->
<!--        MIT License        -->
<nav role="navigation">
  <div id="menuToggle">
    <!--
    A fake / hidden checkbox is used as click reciever,
    so you can use the :checked selector on it.
    -->
    <input type="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.
    -->
    <ul id="menu">
      <a href="#">
        <li>Home</li>
      </a>
      <a href="#">
        <li>About</li>
      </a>
      <a href="#">
        <li>Info</li>
      </a>
      <a href="#">
        <li>Contact</li>
      </a>
      <a href="https://erikterwan.com/" target="_blank">
        <li>Show me more</li>
      </a>
    </ul>
  </div>
</nav>


<div class="container" #slide-1>
  <div class="row">
    <div class="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>

<div class="container" style="background-color:#000;width:100%;">
  <div class="row">
    <div class="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>

这篇关于根据背景颜色更改汉堡菜单的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆