css - 背景图片不会立即加载

标签 css

我正在尝试制作一个汉堡包菜单,其形状为……一个真正的汉堡包。 我唯一的小问题与扩展菜单上的“芝麻”背景图像有关。我设法找到了一个使用径向渐变图像的片段当您单击菜单图标并且菜单下拉时,白色圆点背景图像直到过渡完成后才会显示。

我特别关注的代码是 #menuList 选择器上的背景图像行。

body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
}

a {
  text-decoration: none; /* no underlines */
}

#menuToggle {
  display: block;
  position: absolute;
  height: 36px;
  width: 36px;
  top: 20px;
  right: 20px;
  z-index: 1;
  color: #e5cb7a;
  
  -webkit-user-select: none;
  user-select: none; /* don't let 'em select text */
}

/* Style the "checkbox" used to activate the hamburger menu */
#menuToggle input {
  display: block;
  position: absolute;
  height: 32px;
  width: 32px;
  top: -7px;
  right: -7px;
  cursor: pointer; /* make the burger pointy */
  opacity: 0; /* hide the checkbox */
  z-index: 2;
}

/* Make the menu look like a triangle shape by adding a thick border next to it. */
#menuToggle::before {
  content: '';
  position: absolute;
  top: -10px;
  right: -10px;
  border-color: #e5cb7a; /* Bun-color */
  border-style: solid;
  border-width: 22px;
  border-radius: 8px;
}

/* Style the three spans that make up the hamburger */
#menuToggle span {
  display: block;
  position: relative;
  height: 4px;
  width: 30px;
  margin: 0px 0px 6px 9px;
  border-radius: 3px;
  
  z-index: 1;
  
  transition: transform 0.5s,
              opacity 0.55s ease;
}

/* Color the top span a lettuce color */
#topBun {
  background: #32c704;
}

/* Color the middle span a burger color */
#meat {
  background: #450101;
}

/* Color the bottom span a ketchup color */
#bottomBun {
  background: #ec3521;
}

/* Move the top bun down 10px (its height), then turn it */
#menuToggle input:checked ~ #topBun {
  background: #450101;
  transform: translate(0px, 10px) rotate(45deg);
}

#menuToggle input:checked ~ #meat {
  opacity: 0;
  transform: rotate(0deg);
}

/* Move the bottom bun up 10px (its height), then turn it */
#menuToggle input:checked ~ #bottomBun {
  background: #450101;
  transform: translate(0px,-10px) rotate(-45deg);
}

#menuList {
  display: inline-block;
  position: absolute;
  top: 0px;
  right: 0px;
  height: 100px;
  width: 100px;
  margin: -20px -20px 0 0;
  padding: 50px 90px 0 0;
  background: transparent;
  list-style-type: none; /* no bullets, weapon-free zone */
  /*-webkit-font-smoothing: antialiased;*/
  
  /* Sesame seeds for the top bun of the expanded menu */
  background-image: radial-gradient(white 20%, transparent 0);
  background-size: 15px 12px;
  background-position: 0 12px, 10px 10px;
  
  /* Shrink the actual menu list until the hamburger is opened */
  transform: translate(0px,-65px) scale(1,0.00001);
  transition: transform 1.5s, background 1.5s;
}

/* Make a larger burger bun shape around the expanded hamburger menu */
#menuList::before {
  content: '';
  position: absolute;
  top: 10px;
  right: 10px;
  border-style: solid;
  border-width: 90px;
  border-radius: 10px;
  z-index: -1;
}

#menuList li {
  padding: 3px 5px;
  font-size: 22px;
  text-align: center;
  width: 170px;
  color: #ffffff;
}

#menuList li:hover {
  font-weight: bold;
  letter-spacing: 2px;
}

/* Color the first menu option a lettuce color */
#about {
  background: #32c704;
}

/* Color the second menu option a burger color */
#info {
  background: #450101;
}

/* Color the third menu option a ketchup color */
#contact {
  background: #ec3521;
}

/* Transform the list back to its normal "visible" size when the input is checked */
#menuToggle input:checked ~ ul {
  transform: none;
}
<nav>
  <div id="menuToggle">
    <input type="checkbox" />
    <span id="topBun"></span>
    <span id="meat"></span>
    <span id="bottomBun"></span>
    
    <ul id="menuList">
      <a href="#"><li id="about">ABOUT</li></a>
      <a href="#"><li id="info">INFO</li></a>
      <a href="#"><li id="contact">CONTACT</li></a>
    </ul>
  </div>
</nav>

为了更好地说明问题,我放慢了这支笔的过渡速度。

如何让背景始终显示,而不是仅在菜单完全展开时显示?

CodePen

最佳答案

展开后的菜单“包子”是边框,而不是背景,而背景是折叠的汉堡包。

body {
  margin: 0;
  padding: 0;
  font-family: 'Open Sans', sans-serif;
}

a {
  text-decoration: none;
  /* no underlines */
}

#menuToggle {
  display: block;
  position: absolute;
  height: 36px;
  width: 36px;
  top: 20px;
  right: 20px;
  z-index: 1;
  color: #e5cb7a;
  -webkit-user-select: none;
  user-select: none;
  /* don't let 'em select text */
}


/* Style the "checkbox" used to activate the hamburger menu */

#menuToggle input {
  display: block;
  position: absolute;
  height: 32px;
  width: 32px;
  top: -7px;
  right: -7px;
  cursor: pointer;
  /* make the burger pointy */
  opacity: 0;
  /* hide the checkbox */
  z-index: 2;
}


/* Make the menu look like a triangle shape by adding a thick border next to it. */

#menuToggle::before {
  content: '';
  position: absolute;
  top: -10px;
  right: -10px;
  border-color: #e5cb7a;
  /* Bun-color */
  border-style: solid;
  border-width: 22px;
  border-radius: 8px;
}


/* Style the three spans that make up the hamburger */

#menuToggle span {
  display: block;
  position: relative;
  height: 4px;
  width: 30px;
  margin: 0px 0px 6px 9px;
  border-radius: 3px;
  z-index: 1;
  transition: transform 0.5s, opacity 0.55s ease;
}


/* Color the top span a lettuce color */

#topBun {
  background: #32c704;
}


/* Color the middle span a burger color */

#meat {
  background: #450101;
}


/* Color the bottom span a ketchup color */

#bottomBun {
  background: #ec3521;
}


/* Move the top bun down 10px (its height), then turn it */

#menuToggle input:checked~#topBun {
  background: #450101;
  transform: translate(0px, 10px) rotate(45deg);
}

#menuToggle input:checked~#meat {
  opacity: 0;
  transform: rotate(0deg);
}


/* Move the bottom bun up 10px (its height), then turn it */

#menuToggle input:checked~#bottomBun {
  background: #450101;
  transform: translate(0px, -10px) rotate(-45deg);
}

#menuList {
  display: inline-block;
  position: absolute;
  top: 0px;
  right: 0px;
  height: 100px;
  width: 100px;
  margin: -20px -20px 0 0;
  padding: 50px 90px 0 0;
  background: transparent;
  list-style-type: none;
  /* no bullets, weapon-free zone */
  /*-webkit-font-smoothing: antialiased;*/
  
  /* Shrink the actual menu list until the hamburger is opened */
  transform: translate(0px, -65px) scale(1, 0.00001);
  transition: transform 1.5s;
}


/* Make a larger burger bun shape around the expanded hamburger menu */

#menuList::before {
  content: '';
  position: absolute;
  top: 10px;
  right: 10px;
  bottom: -30px;
  left: 0;
  border-radius: 10px;
  z-index: -1;
  /* Sesame seeds for the top bun of the expanded menu */
  background-color: #e5cb7a;
  background-image: radial-gradient(white 20%, transparent 0);
  background-size: 15px 12px;
  background-position: 0 12px, 10px 10px;
}

#menuList li {
  padding: 3px 5px;
  font-size: 22px;
  text-align: center;
  width: 170px;
  color: #ffffff;
}

#menuList li:hover {
  font-weight: bold;
  letter-spacing: 2px;
}


/* Color the first menu option a lettuce color */

#about {
  background: #32c704;
}


/* Color the second menu option a burger color */

#info {
  background: #450101;
}


/* Color the third menu option a ketchup color */

#contact {
  background: #ec3521;
}


/* Transform the list back to its normal "visible" size when the input is checked */

#menuToggle input:checked~ul {
  transform: translate3d(0,0,0);
}
<nav>
  <div id="menuToggle">
    <input type="checkbox" />
    <span id="topBun"></span>
    <span id="meat"></span>
    <span id="bottomBun"></span>

    <ul id="menuList">
      <a href="#">
        <li id="about">ABOUT</li>
      </a>
      <a href="#">
        <li id="info">INFO</li>
      </a>
      <a href="#">
        <li id="contact">CONTACT</li>
      </a>
    </ul>
  </div>
</nav>

关于css - 背景图片不会立即加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58958892/

相关文章:

javascript - `select` 标签的占位符?

html - 如何修复 html 中的渲染阻塞 CSS

Javascript 使用表格宽度 td 过滤 HTML 内容

css - box-shadow 仍然切断,overflow-y 设置为可见

html - div 只显示在背景图片上

html - Firefox CSSCoverage 工具 - 保存使用的 CSS

ruby-on-rails - Rails 生产服务器 : stylesheets not displaying!

jquery - 单击按钮时隐藏和显示 div

html - 将 col-md-6 div 并排放置

html - 无法获得 CSS Transition 的预期效果