【问题标题】:Swiper - slideable menu full widthSwiper - 可滑动菜单全宽
【发布时间】:2017-08-25 08:24:27
【问题描述】:

我想在使用 bootstrap 4 构建的项目中使用 swiper slide menu。 我有一个看起来像这样的导航,菜单按钮位于小屏幕的右侧:

<div class="container">
  <nav class="navbar navbar-toggleable-sm navbar-light bg-faded">

      <div class="menu-button navbar-toggler navbar-toggler-right">
         <div class="bar"></div>
         <div class="bar"></div>
         <div class="bar"></div>
      </div>
      <a href="{{ url('/') }}" class="logo-link">
        <img src="{{ asset('/img/logo-small-new.png') }}" alt="logo">
      </a>
      <div class="navbar-collapse collapse justify-content-between" id="navbar5">
          <form action="/search" method="get" class="my-auto d-inline mx-auto col-md-8" role="search">
              <div class="input-group">
                <span class="input-group-addon" id="basic-addon1">
                  <button type="submit" class="search-button">
                    <i class="ion-ios-search-strong"></i>
                  </button>
                </span>
                <input type="text" id="search-input" name="q" class="form-control aa-input-search" placeholder="Search for players and videos ..."  value="{{ Request::get('q') }}" aria-describedby="basic-addon1">
              </div>
          </form>
          <ul class="navbar-nav my-auto">
              @if (Auth::check())
              <li class="nav-item dropdown">
                  <a class="nav-link" href="http://example.com" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                      <i class="ion-ios-gear-outline"></i>
                  </a>
                  <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdownMenuLink">
                      <a class="dropdown-item" href="{{ url('/home') }}">Admin dashboard</a>
                      <a class="dropdown-item" href="{{ url('/logout') }}"
                          onclick="event.preventDefault();
                                   document.getElementById('logout-form').submit();">
                          Log out
                      </a>
                      <form id="logout-form" action="{{ url('/logout') }}" method="POST" style="display: none;">
                          {{ csrf_field() }}
                      </form>
                  </div>
              </li>
              @else
              <li>
                <a href="{{ url('/login') }}" class="btn btn-outline-info">Sign in</a>
              </li>
              @endif
          </ul>
      </div>
  </nav>
</div>

现在它的工作方式与here 中的演示一样,但我希望菜单占据整个宽度,并且在菜单打开时菜单按钮在右上角可见。如果我注释掉菜单的一些 css 属性:

.menu {
    min-width: 100px;
    /* width: 70%; */
    /* max-width: 320px; */
    background-color: #2C8DFB;
    color: #fff;
}

然后菜单是全宽的,但菜单按钮不可见,我尝试给它大 z-index,但这没有帮助。我需要做什么才能让它可见?

【问题讨论】:

    标签: html css swiper


    【解决方案1】:

    警告:我对您链接的演示中的代码进行了这些更改。我没有玩过你在这里粘贴的 html。

    你在正确的轨道上。删除 widthmax-width 就像你已经做的那样。

    .menu {
        min-width: 100px;   
        background-color: #2C8DFB;
        color: #fff;
    }
    

    然后添加以下css,以便在展开菜单时将按钮移动到右上角:

    .menu-button.cross{
        left: -80px;
    }
    

    如果您想要全角菜单并且右上角的按钮适用于小屏幕,则需要使用媒体查询。根据需要修改以下代码。

    /*smaller screen css*/
    .menu {
        min-width: 100px;   
        background-color: #2C8DFB;
        color: #fff;
    }
    .menu-button.cross{
        left: -80px;
    }
    media (min-width: 768px) {
        /*larger screen css*/
        .menu {
            width: 70%;
            max-width: 320px;
        }
        .menu-button.cross{
            left: 0;
        }
    }
    

    【讨论】:

    • 感谢您的帮助,有没有办法将菜单高度设置为视口的 100%,因为现在它是内容的高度。我尝试将菜单高度设置为 100vh,并且溢出-y:隐藏,但这不起作用。
    • @Leff 试试这个 css 的菜单元素:position: absolute; top: 0; bottom: 0; 然后position: relative 用于包含菜单的 div。摆脱手动设置菜单高度。
    • 如果我这样做,那么菜单会覆盖内容。
    • @Leff 您应该只将该 CSS 应用于活动菜单。当您单击菜单按钮时,html 元素的类名称会发生​​变化。例如在演示中它更改为swiper-slide-active。仅将 css 应用于此。您还需要为菜单提供自己的类,以将其与内容幻灯片区分开来。所以像.swiper-slide-active.my-menu{ /*css here*/} 这样的选择器。如果我误解了,我们将需要您的实际用例的演示。很难理解你的意思。这也应该是一个单独的 SO 问题。
    • 你是对的,我会尝试你的建议,并给你这个问题的赏金,非常感谢你的帮助!
    猜你喜欢
    • 2014-09-02
    • 1970-01-01
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2023-01-24
    • 2021-12-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多