滑入式菜单:witdth和position:fixed

时间:2016-02-10 08:39:02

标签: html css mobile responsive-design navigation

我现在已经尝试了几个小时来创建一个滑入式菜单,但仍然有问题。

我的菜单应该满足以下要点:

  • 只是html& css(no js)
  • 点击
  • 更改图标
  • 如果宽度> gt,则可以看到完整菜单x(比没有汉堡包图标)
  • 粘性标题
  • 响应(移动友好)
  • 应该适用于现代浏览器;)

这就是我迄今所做的:



/* Micro reset */
*,*:before,*:after{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;margin:0;padding:0;}
html, body { height: 100%; width: 100%; font-family: Helvetica, Arial, sans-serif; }

body {
  /* Without this, the body has excess horizontal scroll when the menu is open */
  overflow-x: hidden;
}

h1, h2, p {
  margin-bottom: 3em;
  line-height: 2em;
}

h1 {
  padding-top: 5em;
}
/* hb icon */

.menu-icon {
  padding: 28px 20px; /* better clickable > please consider in header */
  cursor: pointer;
  display: inline-block;
  float: right;
  position: relative;
  user-select: none;
  -moz-user-select: none;
  -webkit-user-select: none;
}

.menu-icon .navicon {
  background: #333;
  display: block;
  height: 2px;
  position: relative;
  transition: background .2s ease-out;
  width: 18px;
}

.menu-icon .navicon:before,
.menu-icon .navicon:after {
  background: #333;
  content: '';
  display: block;
  height: 100%;
  position: absolute;
  transition: all .2s ease-out;
  width: 100%;
}

.menu-icon .navicon:before {
  top: 5px;
}

.menu-icon .navicon:after {
  top: -5px;
}

/* Nav Trigger */

.nav-trigger {
  /* critical styles - hide the checkbox input */
  position: absolute;
  clip: rect(0, 0, 0, 0);
}

label[for="nav-trigger"] {
  /* critical positioning styles */
  position: fixed;
  right: 15px; top: 15px;
  z-index: 3;

}

.nav-trigger:checked ~ .menu-icon .navicon {
  background: transparent;
}

.nav-trigger:checked ~ .menu-icon .navicon:before {
  transform: rotate(-45deg);
}

.nav-trigger:checked ~ .menu-icon .navicon:after {
  transform: rotate(45deg);
}

.nav-trigger:checked ~ .menu-icon:not(.steps) .navicon:before,
.nav-trigger:checked ~ .menu-icon:not(.steps) .navicon:after {
  top: 0;
}

/* Make the Magic Happen */
.nav-trigger + label, .site-wrap {
  transition: left 0.5s;
}

.nav-trigger + label {
  right: 15px;
  transition: right 0.5s;
}

.nav-trigger:checked + label {
  right: 215px;
}

.nav-trigger:checked ~ .navigation {
  border: 4px solid red;
}

.nav-trigger:checked ~ .site-wrap {
  left: -200px;
  box-shadow: 0 0 5px 5px rgba(0,0,0,0.5);
}


/* Slider Menu - Background */
.navigation {
  /* critical sizing and position styles */
  width: 200px;
  height: 100%;
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  z-index:-1;
  /* non-critical appearance styles */
  list-style: none;
  background: #F2F2F2;
}

ul.navigation {
  margin: 0;
  padding: 0 0 0 20px;
  list-style: none;
  overflow: hidden;
}

ul.navigation li a {
  display: block;
  padding: 12px 0px;
  border-right: 1px solid #f4f4f4;
  text-decoration: none;
}

ul.navigation li a:hover {
  background-color: #f4f4f4;
}


@media (min-width: 51em) {
  ul.navigation li {
    float: left;
  }
  ul.navigation li a {
    padding: 20px 30px;
  }
  .navigation {
    clear: none;
    float: right;
    max-height: none;
  }
   .menu-icon, .navigation {
    display: none;
  }
}


/* Site Wrapper - Everything that isn't navigation */
.site-wrap {
  /* Critical position and size styles */
  min-height: 100%;
  min-width: 100%;
  position: relative;
  top: 0;
  bottom: 100%;
  left: 0;
  z-index: 1;
  background-color: #999; /* Needs a background or else the nav will show through */
  /* non-critical apperance styles */
  background-size: 200%;
}

#hdr {
  margin: 0;
  background-color: #CCC;
  min-width: 100%;
  z-index: 2;
}

.rofl {
  width:100%;
  overflow-x: hidden;
  position: fixed;
  padding: 28px 20px; /* follow from .menu-icon */
  background-color: #F00;
  display: block;
  box-shadow: 1px 1px 4px 0 rgba(0,0,0,.1);
}

.menu {
  float:right;
  width:400px;
  background-color: #333;
}

.menu li {
  float:left;
}

.menu li a {
  padding: 5px 10px;
  color: #FFF;
  text-decoration:none;
}

@media (max-width: 50.99em) {
   .menu {
    display: none;
  }
}

.wrp {
  max-width: 1600px;
  margin: 0px auto;
  height:100%
}

<link href="http://yui.yahooapis.com/3.18.1/build/cssreset/cssreset-min.css" rel="stylesheet"/>
<div class="wrp">
  <ul class="navigation">
    <li><a href="">Menu 1</a></li>
    <li><a href="">Menu 2</a></li>
    <li><a href="">Menu 3</a></li>
  </ul>
  <input type="checkbox" class="nav-trigger" id="nav-trigger" />
  <label for="nav-trigger" class="menu-icon"><span class="navicon"></span></label>
  <div class="site-wrap">
    <div id="hdr">
      <div class="rofl">Header...
        <ul class="menu">
          <li><a href="">Menu 1</a></li>
          <li><a href="">Menu 2</a></li>
          <li><a href="">Menu 3</a></li>
        </ul>
      </div>
    </div>
    <h1>Headline</h1>
    <h2>Subline</h2>
    <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
    <p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
    <p>Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</p>
    <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>
    <p>Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat. Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi.</p>
  </div>
</div>
&#13;
&#13;
&#13;

在codepen上观看:cordova-plugin-locationservices

现在的问题是如果宽度是> 1600标题(红色)不会在右侧停止。 (也许还有其他问题......)

是否有任何技巧可以获得标题的最大宽度?如果我删除&#34; position:fixed;&#34;在课堂rofl它的工作原理。但是滚动时标题应该是可见的。

感谢您阅读我的问题! 八字

1 个答案:

答案 0 :(得分:0)

我做了以下事情:

.rofl {
  max-width: 1600px;
}

为css添加最大宽度将使其超出所选宽度。