10分鐘使用純css實現完整的響應式導航菜單欄的效果

在開發hexo主題pixel的時候沒有選擇bootstrap和jquery實現響應式菜單,而是
使用了純css實現響應式菜單,這個想法來自於You-Dont-Need-Javascript
這個項目分享了不少精彩的純css效果,值得學習。css

簡單閱讀這些css效果源碼以後發現大部分css實現動態效果都是使用[type="checkbox"]也就是一個單選按鈕來保存html組件的狀態,更復雜的效果可能
須要單選按鈕組合來實現。html

checkbox有一個checked狀態,能夠使用css僞類選擇器[type="checkbox"]:checked來實現css切換。jquery

下面是這個博客使用的導航菜單scss格式的源碼git

.nav-collapse {
  height: 60px;

  .nav-brand {
    float: left;
    padding: 10px;
    font-style: italic;
    font-family: Georgia;
    font-size: 28px;
    text-decoration: none;
    color: #000;
    margin-right: 50px;
  }

  [type="checkbox"].check,
  label.toggle {
    display: none;
  }

  .nav-nav {
    li {
      text-align: center;
      line-height: 40px;
      margin-left: 40px;
      float: left;
      list-style: none;
    }

    li:hover {
      cursor: pointer;
      position: relative;
    }

    li > a {
      display: block;
      width: 100%;
      padding: 5px 0;
      font-weight: bold;
      font-size: 14px;
      text-transform: uppercase;
      letter-spacing: .2px;
    }

    li:hover > a {
      color: #e1eb3e;
    }
  }
}

@media all and (max-width: 768px) {
  .nav-collapse {
    .nav-brand {
      padding: 20px;
    }

    .nav-nav {
      background: #fff;
      opacity: 0;
      display: block;
      z-index: 9999;
      position: fixed;
      right: 0;
      left: 0;
      padding: 0;
      top: 60px;

      li {
        display: block;
        width: 100%;
        text-align: left;
        text-indent: 20px;
        margin-left: 10px;
        border-bottom: 1px solid #efefef;
      }
    }

    label.toggle {
      display: block;
      background: #fff;
      width: 40px;
      height: 40px;
      cursor: pointer;
      position: absolute;
      right: 20px;
      top: 20px;
    }

    label.toggle:after {
      content: '';
      display: block;
      width: 30px;
      height: 5px;
      background: #777;
      margin: 7px 5px;
      box-shadow: 0px 10px 0px #777, 0px 20px 0px #777;
    }

    [type="checkbox"].check:checked + .nav-nav {
      opacity: 1;
      transition: all .5s linear;
    }
  }
}

上面這個的導航菜單是固定在頂部的,若是須要導航菜單使用流動佈局,下面這個是能夠放到頁面任何位置的導航菜單github

.nav-collapse {
  background-color: #000;

  .nav-brand {
    float: left;
    padding: 10px;
    font-style: italic;
    font-family: Georgia;
    font-size: 28px;
    text-decoration: none;
    color: #000;
    margin-right: 50px;
  }

  [type="checkbox"].check,
  label.toggle {
    display: none;
  }

  .nav-nav {
    li {
      text-align: center;
      float: left;
      list-style: none;
    }

    li:hover {
      cursor: pointer;
      position: relative;
    }

    li > a {
      display: block;
      width: 100%;
      color: #fff;
      padding: 10px 12px;
      font-weight: bold;
      font-size: 14px;
      text-transform: uppercase;
      letter-spacing: .2px;
      font-family: 'Consolas', 微軟雅黑;
    }

    li:hover > a {
      color: #e1eb3e;
    }
  }
}

@media all and (max-width: 768px) {
  .top {
    width: 100%;
  }
  .nav-collapse {
    .nav-brand {
      padding: 20px;
    }

    .nav-nav {
      background: #fff;
      display: none;
      z-index: 9999;

      li {
        display: block;
        width: 100%;
        text-align: left;
        text-indent: 20px;
        margin-left: 10px;
        border-bottom: 1px solid #efefef;

        a {
          color: #000;
        }
      }

      li:hover > a {
        color: #e1eb3e;
      }
    }

    label.toggle {
      display: block;
      width: 40px;
      height: 40px;
      cursor: pointer;
    }

    label.toggle:after {
      content: '';
      display: block;
      width: 30px;
      height: 5px;
      background: #fff;
      margin: 7px 5px;
      box-shadow: 0px 10px 0px #fff, 0px 20px 0px #fff;
    }

    [type="checkbox"].check:checked + .nav-nav {
      opacity: 1;
      display: block;
      transition: all .5s linear;
    }
  }
}

若是想要看實際效果能夠訪問這個網站美劇天堂,效果是同樣的。bootstrap

休閒時間看看美劇是很好的消遣。hexo

相關文章
相關標籤/搜索