【问题标题】:Responsive Navbar is pushing down content响应式导航栏正在下推内容
【发布时间】:2018-04-16 16:53:09
【问题描述】:

当导航栏在 Chrome 上处于正常无响应模式(我不确定这是否是正确的术语)并且您向下滚动时,当您向下滚动时,徽标图像仍会保留在菜单后面。但是,当屏幕宽度缩小并且菜单折叠并单击图标以展开菜单时,它会将徽标图像向下推。在移动设备上查看时,我希望该徽标图像保留在菜单后面。

我想我以后会有一些我确实想被推倒的内容,但是这个标志我不想被推倒。我可以让它成为我正在使用的背景的一部分,但我认为如果我可以将它保留为单独的图像来显示它会更好。

https://jsfiddle.net/martonian/Ljd5v3ty/

CSS

#logo {

    width: 50%;
    margin: 80px auto;

}

.topnav {

  background-image: url(http://www.buffettworld.com/images/navmenubg.png);
  background-size: 100%;
  font-family: 'Pathway Gothic One', sans-serif;
  margin-top: 10px;
  position: fixed;
  top: 0;
  width: 100%;
}


.topnav a {
  float: left;  
  display: block;
  color: white;
  text-align: center;
  padding: 14px 25px;
  text-decoration: none;
  font-size: 21px;

}

.topnav .icon {
  display: none;
}

.dropdown {
    float: left;
    overflow: hidden;

}

.dropdown .dropbtn {
    font-size: 21px;    
    border: none;
    outline: none;
    color: white;
    padding: 14px 25px;
    background-color: inherit;
    font-family: inherit;
    margin: 0;

}

.dropdown-content {
    display: none;
    position: absolute;
    background-color: #f9f9f9;
    min-width: 160px;
    box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
    z-index: 1;

}

.dropdown-content a {
    float: none;
    color: black;
    padding: 12px 16px;
    text-decoration: none;
    display: block;
    text-align: left;
}

.topnav a:hover, .dropdown:hover .dropbtn {
  background-color: #b1d235;
  color: white;
}

.dropdown-content a:hover {
    background-color: #ddd;
    color: black;
}

.dropdown:hover .dropdown-content {
    display: block;
}

@media screen and (max-width: 600px) {
  .topnav a:not(:first-child), .dropdown .dropbtn {
    display: none;
  }
  .topnav a.icon {
    float: right;
    display: block;
  }
}

@media screen and (max-width: 600px) {
  .topnav.responsive {position: relative;}
  .topnav.responsive .icon {
    position: absolute;
    right: 0;
    top: 0;
  }
  .topnav.responsive a {
    float: none;
    display: block;
    text-align: left;
  }
  .topnav.responsive .dropdown {float: none;}
  .topnav.responsive .dropdown-content {position: relative;}
  .topnav.responsive .dropdown .dropbtn {
    display: block;
    width: 100%;
    text-align: left;
  }
}

.sticky {
  position: fixed;
  top: 10px;
  width: 100%
}

HTML

<div class="topnav" id="myTopnav">
  <a href="#home">HOME</a>
  <a href="#news">NEWS</a>
  <a href="#news">TOUR</a>
    <div class="dropdown">
    <button class="dropbtn">JIMMY 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
        <a href="#">BIOGRAPHY</a>
        <a href="#">AVIATION</a>
        <a href="#">INCIDENTS</a>
        <a href="#">BUSINESS EMPIRE</a>
        <a href="#">CONTACT JIMMY</a>
    </div>
        </div>
          <div class="dropdown">
    <button class="dropbtn">MUSIC 
      <i class="fa fa-caret-down"></i>
    </button>
    <div class="dropdown-content">
        <a href="#">ALBUMS</a>
        <a href="#">SONGS</a>
        <a href="#">CHART HISTORY</a>
        <a href="#">TRIBUTE BANDS</a>
  </div>
        </div>
        <a href="#about">SET LISTS</a>
        <a href="#about">ABOUT</a>
        <a href="#about">ADVERTISE</a>
        <a href="#about">CONTACT</a>
  <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
</div>


<script>
function myFunction() {
    var x = document.getElementById("myTopnav");
    if (x.className === "topnav") {
        x.className += " responsive";
    } else {
        x.className = "topnav";
    }
}
</script>



    <div id="logo">

        <img src="http://www.buffettworld.com/images/logo.png">
    </div>

【问题讨论】:

    标签: css navbar


    【解决方案1】:

    使您的 tonav 位置绝对。这在您的max-width: 600px媒体查询中

    .topnav.responsive {position: absolute;}
    

    希望这会有所帮助:)

    #logo {
    	
    	width: 50%;
    	margin: 80px auto;
    	
    }
    
    .topnav {
    
      background-image: url(http://www.buffettworld.com/images/navmenubg.png);
      background-size: 100%;
      font-family: 'Pathway Gothic One', sans-serif;
      margin-top: 10px;
      position: fixed;
      top: 0;
      width: 100%;
    }
    
    
    .topnav a {
      float: left;  
      display: block;
      color: white;
      text-align: center;
      padding: 14px 25px;
      text-decoration: none;
      font-size: 21px;
    
    }
    
    .topnav .icon {
      display: none;
    }
    
    .dropdown {
        float: left;
        overflow: hidden;
    
    }
    
    .dropdown .dropbtn {
        font-size: 21px;    
        border: none;
        outline: none;
        color: white;
        padding: 14px 25px;
        background-color: inherit;
        font-family: inherit;
        margin: 0;
    
    }
    
    .dropdown-content {
        display: none;
        position: absolute;
        background-color: #f9f9f9;
        min-width: 160px;
        box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
        z-index: 1;
    
    }
    
    .dropdown-content a {
        float: none;
        color: black;
        padding: 12px 16px;
        text-decoration: none;
        display: block;
        text-align: left;
    }
    
    .topnav a:hover, .dropdown:hover .dropbtn {
      background-color: #b1d235;
      color: white;
    }
    
    .dropdown-content a:hover {
        background-color: #ddd;
        color: black;
    }
    
    .dropdown:hover .dropdown-content {
        display: block;
    }
    
    @media screen and (max-width: 600px) {
      .topnav a:not(:first-child), .dropdown .dropbtn {
        display: none;
      }
      .topnav a.icon {
        float: right;
        display: block;
      }
    }
    
    @media screen and (max-width: 600px) {
      .topnav.responsive {position: absolute;}
      .topnav.responsive .icon {
        position: absolute;
        right: 0;
        top: 0;
      }
      .topnav.responsive a {
        float: none;
        display: block;
        text-align: left;
      }
      .topnav.responsive .dropdown {float: none;}
      .topnav.responsive .dropdown-content {position: relative;}
      .topnav.responsive .dropdown .dropbtn {
        display: block;
        width: 100%;
        text-align: left;
      }
    }
    
    .sticky {
      position: fixed;
      top: 10px;
      width: 100%
    }
    <div class="topnav" id="myTopnav">
      <a href="#home">HOME</a>
      <a href="#news">NEWS</a>
      <a href="#news">TOUR</a>
        <div class="dropdown">
        <button class="dropbtn">JIMMY 
          <i class="fa fa-caret-down"></i>
        </button>
        <div class="dropdown-content">
    		<a href="#">BIOGRAPHY</a>
    		<a href="#">AVIATION</a>
    		<a href="#">INCIDENTS</a>
    		<a href="#">BUSINESS EMPIRE</a>
    		<a href="#">CONTACT JIMMY</a>
        </div>
    		</div>
    		  <div class="dropdown">
        <button class="dropbtn">MUSIC 
          <i class="fa fa-caret-down"></i>
        </button>
        <div class="dropdown-content">
    		<a href="#">ALBUMS</a>
    		<a href="#">SONGS</a>
    		<a href="#">CHART HISTORY</a>
    		<a href="#">TRIBUTE BANDS</a>
      </div>
    		</div>
    		<a href="#about">SET LISTS</a>
      		<a href="#about">ABOUT</a>
    		<a href="#about">ADVERTISE</a>
    		<a href="#about">CONTACT</a>
      <a href="javascript:void(0);" style="font-size:15px;" class="icon" onclick="myFunction()">&#9776;</a>
    </div>
    
    
    <script>
    function myFunction() {
        var x = document.getElementById("myTopnav");
        if (x.className === "topnav") {
            x.className += " responsive";
        } else {
            x.className = "topnav";
        }
    }
    </script>
    	
    
    	
    	<div id="logo">
    		
    		<img src="http://www.buffettworld.com/images/logo.png">
    	</div>

    【讨论】:

    • 谢谢杰拉尔多。这解决了它!
    • 欢迎您,当您获得更多代表时请点赞。谢谢
    猜你喜欢
    • 2014-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-17
    • 1970-01-01
    • 2021-03-28
    相关资源
    最近更新 更多