无法对齐和增加切换导航栏的宽度

问题描述 投票:0回答:1

我创建了一个切换导航栏,只要单击该按钮,就会出现菜单和动画。但是,我遇到了一些问题。

  1. 我的内容没有在页面的右侧对齐,主要是按钮本身。
  2. 我正在努力获得我的菜单(薰衣草)的背景,以填充页面出现时的整个宽度和高度。

我尝试了几种方法,包括'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;
}
html css html5 css3
1个回答
0
投票

用外部div包装菜单图标span并向右添加float,对于复选框使用权限0

以下是更新的代码

UPDATE

我尝试使用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>
© www.soinside.com 2019 - 2024. All rights reserved.