网站菜单深层次,桌面和移动设备上有不同的堆叠

时间:2016-07-27 17:27:33

标签: html css twitter-bootstrap twitter-bootstrap-3

我正在为网站创建菜单。它有2个级别。您可以单击该部分的标题,然后显示此部分的链接,而其他部分隐藏。很简单。

View on desktop

现在,问题是移动视图完全不同。它的工作原理相同,但这些部分首先显示为列表,下面显示部分链接(以当前显示的为准)。像这样:

View on mobile

我设法使用Bootstrap(条纹化不必要的代码)这样​​做:

<div class="row">

    <div class="col-xs-6 col-lg-12">
        Section 1 title
    </div>

    <div class="col-lg-12 hidden-xs">
        <ul>
            <li><a href="">Link in section 1</a></li>
            ...
        </ul>
    </div>

    <div class="col-xs-6 col-lg-12">
        Section 2 title
    </div>

    <div class="col-lg-12 hidden-xs">
        <ul>
            <li><a href="">Link in section 2</a></li>
            ...
        </ul>
    </div>

</div>

<div class="row visible-xs">

    <div class="col-xs-12">
        <ul>
            <li><a href="">Link in section 1</a></li>
            ...
        </ul>
    </div>

    <div class="col-xs-12">
        <ul>
            <li><a href="">Link in section 2</a></li>
            ...
        </ul>
    </div>

</div>

有效。但我想避免两次创建该子部分。部分标题也不会显示为列表,而是他们被欺骗的&#34;作为网格中的列。

2 个答案:

答案 0 :(得分:0)

* {
  padding: 0;
  margin: 0;
  list-style: none;
  text-indent: 0;
}
ul {
  padding: 1em;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  border: 1px solid;
}
li {
  display: inline-block;
  width: 50%;
  background-color: #ddd;
  border: 1px solid #fff;
  box-sizing: border-box;
  text-align: center;
}
<ul>
  <li>link in section 1</li>
  <li>link in section 1</li>
  <li>link in section 1</li>
  <li>link in section 1</li>
  <li>link in section 1</li>
</ul>

答案 1 :(得分:0)

在狭窄的屏幕上,您只能使用两列并使<ul>比其列宽。

layout

我使用的是标准collapse plugincol-xs-6 col-sm-12类,而不是col-xs-6 col-lg-12

我在狭窄的屏幕上绘制了背景,以便进行布局可视化。

https://jsfiddle.net/glebkema/9oo7bvrw/

var listFirst  = $('#list-1');
var listSecond = $('#list-2');

listFirst.on('show.bs.collapse', function () {
  listSecond.collapse('hide');
})
listSecond.on('show.bs.collapse', function () {
  listFirst.collapse('hide');
})
@import url('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css');

/* The heart of the matter */
@media (max-width: 767px) {
  #list-1, 
  #list-2 {
    width: 100vw !important;
  }
  #list-1 {
    margin-right: -50vw;
  }
  #list-2 {
    margin-left: -50vw;
  }
  .col-xs-6 {
    padding: 0;
  }
}

/* Visualization */
@media (max-width: 767px) {
  .col-xs-6:first-child {
    background: #69c;
  }
  .col-xs-6:last-child {
    background: #c69;
  }
  #list-1, 
  #list-2 {
    background: #9c6;
  }
  a, 
  a:hover, 
  a:focus {
    color: white;
  }
}

/* Decorations */
.container {
  margin-top: 12px;
}
a {
  display: block;
  font-size: 14px;
  font-weight: normal;
  line-height: 1;
  padding: 4px 8px;
}
a[aria-expanded="true"] {
  font-size: 16px;
  font-weight: bold;
  padding: 2px 8px;
}
ul {
  list-style: none;
}
@media (max-width: 767px) {
  a {
    text-align: center;
  }
  #list-1, 
  #list-2 {
    margin-top: 12px;
    padding: 0;
    text-align: center;
  }
  #list-1 li, 
  #list-2 li {
    display: inline-block;
  }
}
<div class="container">
  <div class="row">
    <div class="col-xs-6 col-sm-12">
      <a role="button" data-toggle="collapse" href="#list-1" aria-expanded="false" aria-controls="list-1">Section 1 title</a>
      <ul class="collapse" id="list-1">
        <li><a href="#">Link in section 1</a></li>
        <li><a href="#">Link in section 1</a></li>
        <li><a href="#">Link in section 1</a></li>
        <li><a href="#">Link in section 1</a></li>
      </ul>
    </div>

    <div class="col-xs-6 col-sm-12">
      <a role="button" data-toggle="collapse" href="#list-2" aria-expanded="false" aria-controls="list-2">Section 2 title</a>
      <ul class="collapse" id="list-2">
        <li><a href="#">Link in section 2</a></li>
        <li><a href="#">Link in section 2</a></li>
        <li><a href="#">Link in section 2</a></li>
        <li><a href="#">Link in section 2</a></li>
        <li><a href="#">Link in section 2</a></li>
      </ul>
    </div>
  </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>