【问题标题】:Mobile menu z-index issue?移动菜单 z-index 问题?
【发布时间】:2017-06-23 16:25:06
【问题描述】:

我的移动菜单有问题,它看起来是一个 z-index 问题,因为当切换菜单时,它出现在下面的内容后面。但是,我一直在玩这个,我无法将切换菜单放在前面。

代码: HTML:

        <section class="navigation">
            <div class="nav-container">
                <div class="brand">
                    <a href="/" title="Storey &#38; Co. Solicitors" rel="home"><img class="header-image" src="assets/img/storey-and-co-logo.png" alt="Storey &#38; Co. Solicitors" title="Storey &#38; Co. Solicitors"></a>
                </div>
                <nav>
                    <div class="nav-mobile">
                        <a id="nav-toggle" href="#!"><span></span></a>
                    </div>
                    <ul class="nav-list">
                        <li>
                            <a href="about-us.html">About Us</a>
                        </li>
                        <li>
                            <a href="our-team.html">Our Team</a>
                        </li>
                        <li>
                            <a href="services.html">Services</a>
                            <ul class="nav-dropdown">
                                <li>
                                    <a href="divorce.html">Divorce</a>
                                </li>
                                <li>
                                    <a href="wills.html">Wills</a>
                                </li>
                                <li>
                                    <a href="residential.html">Residential</a>
                                </li>
                            </ul>
                        </li>
                        <li>
                            <a href="contact.html">Contact</a>
                        </li>
                        <li class="nav-highlight">
                            <a href="https://www.perfectportal.co.uk" target="_blank">Obtain a Quote</a>
                        </li>
                    </ul>
                </nav>
            </div>
        </section>

jQuery

        (function($) {
          $(function() {
            $('nav>ul>li').addEventListener('click',function() {
              $(this).children('.nav-dropdown').show();
            });
            $('nav>ul>li').mouseleave(function() {
              $('.nav-dropdown').hide();
            });
          });
            document.querySelector('#nav-toggle').addEventListener('click', function() {
            this.classList.toggle('active');
          });
          $('#nav-toggle').click(function() {
            $('nav ul').toggle();
          });
        })(jQuery);

萨斯

        header
            background: $brand-primary
            height: $nav-height
            clear: both

        section.navigation
            padding: 0px
            clear: both

        nav
          float: right
          ul
            list-style: none
            margin: 0
            padding: 0
            li
              float: left
              position: relative
              a
                display: block
                padding: 0 20px
                line-height: $nav-height
                background: $brand-primary
                color: #fff
                text-decoration: none
                &:hover
                  background: $brand-3-dark
                  color: #fff
                &:not(:only-child):after
                  padding-left: 4px
                  content: ' ▾'
              ul li
                min-width: 190px
                & a
                  padding: 15px
                  line-height: 20px
                  z-index: 1

        .nav-highlight a
            background: $brand-3-dark
            color: #fff

            &:hover
                background: $brand-3-primary

        .nav-dropdown
          position: absolute
          display: none
          z-index: 1
          box-shadow: 0 3px 12px rgba(0, 0, 0, 0.15)

        .nav-mobile
          display: none
          position: absolute
          top: 0
          right: 0
          background: $brand-primary
          height: $nav-height
          width: $nav-height

        #nav-toggle
          position: relative
          z-index: 9
          left: 18px
          top: 22px
          cursor: pointer
          padding: 10px 35px 16px 0px
          span,
          span:before,
          span:after
            cursor: pointer
            border-radius: 1px
            height: 5px
            width: 35px
            background: #fff
            position: absolute
            display: block
            content: ''
            transition: all 300ms ease-in-out
          span:before
            top: -10px
          span:after
            bottom: -10px
          &.active span
            background-color: transparent
            &:before,
            &:after
              top: 0
            &:before
              transform: rotate(45deg)
            &:after
              transform: rotate(-45deg)

        @media only screen and (max-width: $breakpoint)
          .nav-mobile
            display: block
          nav
            width: 100%
            padding: $nav-height 0 15px
            ul
              display: none
              li
                float: none
                a
                  padding: 15px
                  line-height: 20px
                ul li a
                  padding-left: 30px
          .nav-dropdown
            position: static

        @media screen and (min-width: $breakpoint)
          .nav-list
            display: block !important

        .navigation
          height: $nav-height
          background: $brand-primary

        .nav-container
          max-width: $content-width
          margin: 0 auto

        .brand
          position: absolute
          float: left
          padding-top: 10px
          vertical-align: middle
          text-transform: uppercase
          font-size: 1.4em
          box-sizing: border-box
          a,
          a:visited
            color: #fff
            text-decoration: none

        img.header-image
            max-width: 200px

        @media screen and (max-width: $breakpoint-small)
            img.header-image
                max-width: 175px
                padding-top: 10px

现场版在这里:http://staging-maris-storey.transitiongraphics.co.uk/

帮助赞赏

【问题讨论】:

  • 解释如何重现问题。真不知道怎么办才能看到问题
  • 我认为这是因为.navigationheader 中的静态高度。
  • 哦,对了,它在手机上,我在看桌面。对不起

标签: css mobile menu


【解决方案1】:

尝试为您的导航添加相对位置和更高的 z-index:

nav {
    position: relative;
    z-index: 9;
}

【讨论】:

    【解决方案2】:

    我不确定为什么 Maggie Serino 的答案对你不起作用,因为它在我测试时起作用。然而,另一种解决方案是将clear:both; 添加到第一部分。该问题是由您的固定高度header 引起的,这导致下面的部分不会被下拉菜单向下推。

    header + section {
      clear:both;
    }
    

    【讨论】:

    • 我同意这两种解决方案都应该有效,但显然有些地方还不太正确?
    • @GrantSmith 在做出建议的 CSS 更改后,您是否尝试过清除手机浏览器缓存?
    • 是的,也在浏览器上尝试开发模式,同样的问题?
    • @GrantSmith 这很奇怪。我不确定该建议什么,因为当我在浏览器中进入开发模式并将 CSS 更改为这些解决方案中的任何一个时,它可以解决您的问题。
    • 我认为我在 sass 中的间距有误,现在流行起来。我想我有 jquery 问题要排序。另外,我希望菜单覆盖内容,而不是内容向下移动。谢谢大家
    【解决方案3】:

    @Grant Smith 如果您希望菜单覆盖您需要给它一个绝对位置的内容(并且父级需要是相对的)。我在您的分期上尝试了一些,您应该添加:

    nav {
        position: relative;
        // your other attributes
    }
    
    .nav-list {
        position: absolute;
        width: 100%;
        // your other attributes
    }
    

    让我知道它是否有效。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-15
      相关资源
      最近更新 更多