我创建了一个切换导航栏,每当单击按钮时都会在其中显示菜单和动画。但是,我在几个部分上遇到了麻烦。


我的内容不在页面的右侧,主要是按钮本身。
我正在努力获取菜单的背景(薰衣草)以在页面出现时填满页面的整个宽度和高度。


我尝试了几种方法,包括'float:right','width:100vw','height:100vh'和更多,但是没有运气。

以下是我的代码,以及从本质上来说,我想要达到的外观的链接。任何帮助将不胜感激。谢谢!

示例:http://madetogether.com.au/case-study/sprout/

<!-- HTML -->
<!-- Navigation Bar -->
 <nav role="navigation">
  <div id="menuToggle">
    <input type="checkbox" />
      <span></span>
      <span></span>
      <span></span>
<ul id="menu">
  <li>
    <a href="#page1" class="nav-link">
      Services
    </a>
  </li>
  <li class="nav-item">
    <a href="#page3" class="nav-link">
      About
    </a>
  </li>
  <li class="nav-item">
    <a href="#page4" class="nav-link">
      Contact
    </a>
  </li>
</ul>





/* CSS */
/* Navigation Bar */
#menuToggle {
  display: block;
  position: relative;
  top: 1em;
  left: 1em;
  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;
  z-index: 2;
    -webkit-touch-callout: none;
}

a {
  text-decoration: none;
  color: black;
    transition: color 0.3s ease;
}

a:hover {
  color: red;
}

#menuToggle {
  display: block;
  position: relative;
  top: 1em;
  left: 1em;
  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;
  z-index: 2;
    -webkit-touch-callout: none;
}

#menuToggle span {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  background: black;
  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%;
}

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

#menuToggle input:checked ~ span:nth-last-child(3) {
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}

#menuToggle input:checked ~ span:nth-last-child(2) {
  transform: rotate(-45deg) translate(0, -1px);
}

#menu {
  position: absolute;
  width: 100%;
  margin: -100px 0 0 -50px;
  padding: 50px;
  padding-top: 125px;
  background: lavender;
  opacity: .85;
    list-style-type: none;
    -webkit-font-smoothing: antialiased;
    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: .5em 0;
  font-size: 1.5em;
  text-align: right;
}

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

最佳答案

用外部div包裹菜单图标范围,并向右添加浮点数,并为复选框使用权0

以下是更新的代码

更新

我尽我最大的努力使用CSS来获取100%的高度背景,但是失败了。所以我用JavaScript欺骗了它。



  function toggleHeight(){
      var navElement = document.getElementsByTagName("nav")[0];
      navElement.classList.toggle("fullHeight");

      var menuElement = document.getElementById("menu");
      menuElement.classList.toggle("fullHeight");

      var menuToggleElement = document.getElementById("menuToggle");
      menuToggleElement.classList.toggle("fullHeight");
  }

#menuToggle {
  display: block;
  position: relative;
  top: 1em;
  left: 1em;
  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;
  z-index: 2;
    -webkit-touch-callout: none;
}

a {
  text-decoration: none;
  color: black;
    transition: color 0.3s ease;
}

a:hover {
  color: red;
}

#menuToggle {
  display: block;
  position: relative;
  top: 1em;
  left: 1em;
  z-index: 1;
    -webkit-user-select: none;
    user-select: none;
}

#menuToggle input {
  display: block;
  width: 40px;
  height: 32px;
  position: absolute;
  top: -7px;
  right: -5px;
  cursor: pointer;
  opacity: 0;
  z-index: 2;
    -webkit-touch-callout: none;
}

#menuToggle span {
  display: block;
  width: 33px;
  height: 4px;
  margin-bottom: 5px;
  position: relative;
  background: black;
  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%;
}

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

#menuToggle input:checked ~ span:nth-last-child(3) {
  opacity: 0;
  transform: rotate(0deg) scale(0.2, 0.2);
}

#menuToggle input:checked ~ span:nth-last-child(2) {
  transform: rotate(-45deg) translate(0, -1px);
}

#menu {
  position: absolute;
  width: 100%;
  margin: -100px 0 0 -50px;
  padding: 50px;
  padding-top: 125px;
  background: lavender;
  opacity: .85;
    list-style-type: none;
    -webkit-font-smoothing: antialiased;
    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: .5em 0;
  font-size: 1.5em;
  text-align: right;
}

#menuToggle input:checked ~ ul {
  transform: none;
}
.menuIcon {
    float: right;
}

html, body {
    height: 100%;
    margin: 0px;
}

.fullHeight {
  height: 100% !important;
}

<nav role="navigation">
  <div id="menuToggle">
    <input type="checkbox" onclick="toggleHeight()"/>
    <div class="menuIcon">
        <span></span>
        <span></span>
        <span></span>
    </div>
<ul id="menu">
  <li>
    <a href="#page1" class="nav-link">
      Services
    </a>
  </li>
  <li class="nav-item">
    <a href="#page3" class="nav-link">
      About
    </a>
  </li>
  <li class="nav-item">
    <a href="#page4" class="nav-link">
      Contact
    </a>
  </li>
</ul>

关于html - 对齐和增加切换导航栏的宽度时遇到问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54741211/

10-12 05:28