首页 文章

如何将导航栏固定到移动视图的屏幕底部>

提问于
浏览
0

我在桌面上查看时,目前有一个侧边栏作为导航栏 . 在桌面上,视图,图标和文本与左侧的图标和右侧的文本并排 .

我试图得到它,以便当它折叠到移动视图时,侧边栏会折叠到固定在屏幕底部的导航栏 . 图标位于顶部,位于文本下方 .

这是我目前所拥有的:http://codepen.io/anon/pen/bpRgEJ

这是我试图找到底部位置的代码:

@media (max-width:35em) {

    .main-nav {
        background: #ECC264;
        border-right: 1px solid #e5e5e5;
        position: fixed;
        margin: 0;
        left: 0;
        bottom: 0;
        height: 5em;
        width: 100%;
    }
}

它目前折叠成一个顶部导航栏,图标和文字是并排的,而不是顶部和底部 .

任何帮助将不胜感激,谢谢!

3 回答

  • 0

    首先,我建议您按照以下方式组织CSS:

    1.-具有影响一切的规则的一般选择者和分支

    2.-仅影响特定宽度行为的媒体查询 .

    因此,如果您需要一个始终固定的导航栏,但桌面位于屏幕左侧,而移动设备位于页面底部,您应该编写如下内容:

    .sidebar{ /* Every rule that will be always visible, something like colors, fonts, etc. */
      background-color: yellow; 
      position: fixed;
    }
    
    @media(min-width:35.1em){ /* Larger than mobile devices */
      .sidebar{
        left: 0;
        top: 0;
        bottom: 0;
        width: 40px;
      }
    }
    
    @media(max-width:35em){ /* Breakpoint only for mobile */
      .sidebar{
        bottom: 0;
        width: 100%;
      }
    }
    

    这样,您将避免处理不匹配的规则 .

    您的完整CSS应如下所示:

    .fa-2x {
        font-size: 2rem;
    }
    .fa {
        position: relative;
        text-align: center;
        vertical-align: middle;
        font-size: 2rem;
    }
    
    .main-nav {
        background: #ECC264;
        border-right: 1px solid #e5e5e5;
        position: fixed;
        z-index: 1000;
    }
    .main-nav>ul {
        margin: 0rem 0rem;
    }
    
    nav ul,
    nav li {
        outline: 0;
        margin: 0;
        padding: 0;
    }
    .main-nav li:hover>a,
    nav.main-nav li.active>a{
        color: #fff;
        background-color: #919191;
    }
    
    .main-nav .nav-text, .main-nav li>a{
      font-family: 'Roboto', sans-serif;
    }
    
    .main-nav li>a {
          border-collapse: collapse;
          border-spacing: 0;
          color: white;
          font-size: 1.03rem;
          text-decoration: none;
    }
    
    @media (min-width:35.1em) {
      .fa{
        display: table-cell;
        width: 5rem;
        height: 5rem;
      }
    
      .main-nav:hover,
    nav.main-nav.expanded {
        width: 15em;
        overflow: visible;
    }
    
      .main-nav{
        top: 0;
        bottom: 0;
        height: 100%;
        left: 0;
        width: 5rem;
        overflow: hidden;
        -webkit-transition: width .05s linear;
        transition: width .05s linear;
        -webkit-transform: translateZ(0) scale(1, 1);
      }
    
      .main-nav li {
        position: relative;
        display: block;
        width: 18rem;
      }
      .main-nav li>a {
          position: relative;
          display: table;
          -webkit-transform: translateZ(0) scale(1, 1);
          -webkit-transition: all .1s linear;
          transition: all .1s linear;
      }
    
      .main-nav .nav-text {
          position: relative;
          display: table-cell;
          vertical-align: middle;
          width: 10rem;
      }
    }
    
    
    @media (max-width:35em) {
    
        .main-nav {
            background: #ECC264;
            border-right: 1px solid #e5e5e5;
            position: fixed;
            margin: 0;
            left: 0;
            bottom: 0;
            /*height: 5em;*/
            width: 100%;
        }
    
      .main-nav ul{
        display: table;
        width: 100%; 
      }
    
        .main-nav li{
          float: left;
          width: 25%;
          list-style-type: none;
          text-align: center;
        }
    
      .main-nav a{
        display: block;
        padding: 5px;
        height: 74px;
      }
    
      .main-nav i, .main-nav span{
        display: block;
      }
    
      .main-nav span{
        font-size: 14px;
      }
    }
    

    您的示例在Codepen中分叉:http://codepen.io/xWaZzo/pen/BKZpQE

  • 0

    这应该可以解决问题 .

    #myNavbar {
            position: relative;
        }
    
        #myNavbar .nav {
            position: absolute; 
            bottom: 0; 
            right: 0;
            margin-bottom: -10px;
        }
    

    http://jsfiddle.net/DTcHh/1819/

  • 0

    你应该删除top:0

    .main-nav {
    background: #ECC264;
    border-right: 1px solid #e5e5e5;
    position: fixed;
    / *top: 0;*/
    bottom: 0;
    height: 100%;
    left: 0;
    width: 5rem;
    overflow: hidden;
    -webkit-transition: width .05s linear;
    transition: width .05s linear;
    -webkit-transform: translateZ(0) scale(1, 1);
    }
    

    还可以在媒体查询类中增加高度

    @media (max-width:35em) {
    
    .main-nav {
    background: #ECC264;
    border-right: 1px solid #e5e5e5;
    position: relative;
    margin: 0;
    left: 0;
    bottom: 0;
    height: 20em;
    width: 100%;
    }
    }
    

    希望这是你想要的?

相关问题