【问题标题】:CSS display a hidden drop-up menu on mobileCSS 在移动设备上显示隐藏的下拉菜单
【发布时间】:2017-03-06 06:12:08
【问题描述】:

我有一个很好的下拉页脚:

$('#drop-up-open').click(function() {
  $('#drop-up #drop-upDashboard').slideToggle({
    direction: "up"
  }, 300);
  $(this).toggleClass('drop-upClose');
}); // end click
#drop-up {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding-bottom: 4%;
  z-index: 100;
  margin-left: auto;
  margin-right: auto;
  left: 0;
  right: 0;
}

#drop-up-open {
  cursor: pointer;
  text-decoration: none;
}

#drop-up .drop-upClose {}

#drop-up #drop-upDashboard {
  display: none;
}

@media screen and (max-width: 1000px) {
#drop-up #drop-upDashboard{ 
display:block; }
}
<div class="container">

    <!-- Jumbotron Header -->
    <header class="jumbotron panel-heading">

    <p class="text-center">
    
                Text getting covered by drop-up menu on mobile

    </p>

    <section>
                <a href="#"><button> Main button </button></a>
    </section>
    
    </header>

<div id="drop-up">
  <a href="#" id="drop-up-open">Open</a>

  <div id="drop-upDashboard">
    <p>It's now open!</p>

    <div class="row">

      <div class="col-md-3 col-xs-12">
        <section> <a href="#"><button>
                            
                            <p>Button 1</p>

                        </button></a> </section>
      </div>

      <div class="col-md-3 col-xs-12">
        <section> <a href="#"><button>
                            
                            <p>Button 2</p>

                        </button></a> </section>
      </div>

      <div class="col-md-3 col-xs-12">
        <section> <a href="#"><button>
                            
                            <p>Button 3</p>

                        </button></a> </section>
      </div>

      <div class="col-md-3 col-xs-12">
        <section> <a href="#"><button>
                            
                            <p>Button 4</p>

                        </button></a> </section>
      </div>

    </div>
    <!-- row -->
  </div>
  <!-- dashboard -->
</div>
<!-- drop-up -->
</div>
<!-- container -->

当我单击 id="drop-up" div 时,菜单从下到上打开,并在桌面视图中占据窗口底部的一小部分。

现在,我想要的是这个最初隐藏在桌面视图中的菜单,在较小的设备(移动设备+平板电脑)上显示,而无需单击 id="drop-up" div。它将占据移动设备+平板电脑页面底部的相同部分(然后用户需要向下滚动才能在较小的设备上查看下拉菜单,而在桌面视图中则不然)。

关于实现这一目标的最佳方法的任何建议/提示?

【问题讨论】:

  • 这很容易通过@media 查询来实现。网上应该有很多例子
  • 谢谢。我刚刚编辑了 CSS 部分。但是,我有一个尺寸问题,因为按钮在移动设备上相互堆叠,并且它们不会像应有的那样在页面底部受到限制,而是超出了它们的预期限制并显示在上面的内容之上。这有什么线索吗?
  • 不确定您的意思,它们不应该堆叠吗?对我来说看起来不错,也许可以多编辑一下你的 sn-p 以显示它上面的“内容”或 w/e
  • 我在上面有一个简单的标题作为编辑。事实上,这些按钮应该在移动设备上堆叠,但它们不应该显示在标题顶部。我一定是搞错了。
  • 是的,这是因为position absolute 定位绝对函数类似于fixed,但允许相对于display relative 元素而不是窗口定位

标签: javascript jquery html css


【解决方案1】:

您可以将整个东西包裹在一个 flex box 中,然后让 body flex 填充其余的空间,从而将您的页脚放在页脚级别。当您做出响应时,也“似乎”工作。

然而,这在此刻感觉很 hacky,就好像你在 flex box 容器周围画了一个边框,它只能适应它原来的 100% 水平。

$('#drop-up-open').click(function() {
  $('#drop-up #drop-upDashboard').slideToggle({
    direction: "up"
  }, 300);
  $(this).toggleClass('drop-upClose');
}); // end click
html, body {
  height: 100%;
}
#drop-up-open {
  cursor: pointer;
  text-decoration: none;
}

#drop-up .drop-upClose {}

#drop-up #drop-upDashboard {
  display: none;
}

@media screen and (max-width: 200px) {
  #drop-up #drop-upDashboard{ 
  display:block; }

  html, body {
    height: auto !important;
  }

}


.container {
  display: flex;
  flex-direction: column;
  border: 1px solid black;
  min-height: 100%;
}

.body {
  flex: 1;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">

    <!-- Jumbotron Header -->
    <header class="jumbotron panel-heading" style="background-color: yellow; opacity: 0.7">

      <p class="text-center">

                  Text getting covered by drop-up menu on mobile

      </p>

      <section>
                  <a href="#"><button> Main button </button></a>
      </section>

    </header>
    
    <div class="body" style="background-color: orange; opacity: 0.3">
      a body
    </div>

  <div id="drop-up" style="background-color: lightblue; opacity: 0.7">
    <a href="#" id="drop-up-open">Open</a>

    <div id="drop-upDashboard">
      <p>It's now open!</p>

      <div class="row">

        <div class="col-md-3 col-xs-12">
          <section> <a href="#"><button>

                              <p>Button 1</p>

                          </button></a> </section>
        </div>

        <div class="col-md-3 col-xs-12">
          <section> <a href="#"><button>

                              <p>Button 2</p>

                          </button></a> </section>
        </div>

        <div class="col-md-3 col-xs-12">
          <section> <a href="#"><button>

                              <p>Button 3</p>

                          </button></a> </section>
        </div>

        <div class="col-md-3 col-xs-12">
          <section> <a href="#"><button>

                              <p>Button 4</p>

                          </button></a> </section>
        </div>

      </div>
      <!-- row -->
    </div>
    <!-- dashboard -->
  </div>
  <!-- drop-up -->
</div>
<!-- container -->

【讨论】:

  • 我同意这很 hacky。也许最好的方法是说如果响应,然后显示“下拉”位置:相对而不是绝对。有媒体查询吗?
  • 您可以添加一个mobile 类标签,然后尝试display: relative !important 看看它是否有效
  • 我也做了一些修改,所以我觉得现在应该会更好,看看
  • @media screen and (max-width: 1000px) { #drop-up #drop-upDashboard{ display:block; } #drop-up { 位置:相对; } } 一切都很好,谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-12-03
  • 1970-01-01
  • 1970-01-01
  • 2013-10-24
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多