简体   繁体   English

单击汉堡包图标时如何下推菜单内容?

[英]How to push menu content down when hamburger icon is clicked?

I want my hamburger icon to push down the content, meaning the background and writing when the hamburger icon is clicked on. 我希望我的汉堡图标下推内容,即单击汉堡图标时的背景和文字。 I tried to add padding to the body and set the width to the hamburger but does not seem to be working. 我试图在身体上添加填充物并将汉堡包的宽度设置为正确,但似乎不起作用。 I have been trying for the last couple hours. 我已经尝试了最近几个小时。 I only want to use CSS if possible. 如果可能,我只想使用CSS。

 .mobile-nav { position: fixed; top: 0; right: 0; text-align: right; padding: 10px; background: var(--bg-color); width: 100vw; z-index: 2; } .mobile-nav .menu { display: flex; text-align: center; justify-content: center; display: none; } .mobile-nav .menu ul li { list-style: none; padding: 0.1rem; font-size: 1.2rem; } .mobile-nav #toggle { display: none; cursor: pointer; } .mobile-nav #toggle:checked + .menu { display: flex; } 
 <div class="mobile-nav"> <label for="toggle"><i class="fas fa-bars fa-2x"></i></label> <input type="checkbox" id="toggle"> <div class="menu"> <ul> <li><a href="#home">HOME</a></li> <li><a href="#promise">OUR PROMISE</a></li> <li><a href="#team">TEAM</a></li> <li><a href="#sponsors">SPONSORSHIPS</a></li> <li><a href="#about">ABOUT</a></li> <li><a href="#contact">CONTACT</a></li> </ul> </div> </div> 

You can put your main content in a div and adjust the top margin when the menu is expanded. 您可以将主要内容放入div中,并在展开菜单时调整上边距。

Live Demo 现场演示

HTML: HTML:

<body id="body">
    <div class="mobile-nav">
        <label for="toggle">button</label>
    <input type="checkbox" id="toggle" onclick='onClickHandler(this)'>
    <div class="menu">
      <ul>
        <li><a href="#home">HOME</a></li>
        <li><a href="#promise">OUR PROMISE</a></li>
        <li><a href="#team">TEAM</a></li>
        <li><a href="#sponsors">SPONSORSHIPS</a></li>
        <li><a href="#about">ABOUT</a></li>
        <li><a href="#contact">CONTACT</a></li>
      </ul>
    </div>
  </div>

  <div id="content">
    Content
  </div>
</body>

<script>
function onClickHandler(e){
  var content = document.getElementById("body");

  if(e.checked){
     content.setAttribute( 'class', 'push-down' );

  }else{
     content.setAttribute( 'class', '' );
  }
}
</script>

CSS: CSS:

//.. your styles 

#content{
  height:300px;
  background-color:yellow;
  margin-top:50px;
}

#body.push-down{
  margin-top:250px;
}

You may use max-height: 0px; 您可以使用max-height:0px; and overflow: hidden; 溢出:隐藏; instead of display: none; 而不是显示:无; for div.menu . 对于div.menu。 Animate max-height: on toggle. 动画最大高度:切换时。

js for toggle class 切换类的js

$('#toggle').on('click', function(){
    $('.mobile-nav .menu').toggleClass('active'); //Toggle Class on Click
});

CSS CSS

.menu {
    height: auto;
    max-height: 0px;
    overflow: hidden;
    transition: all 0.5s ease-in;
}
.menu.active {
    max-height: 500px; //Set any value higher than menu
}

See a working demo here : https://jsfiddle.net/rbzfk0ca/36/ 在此处查看有效的演示: https : //jsfiddle.net/rbzfk0ca/36/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 单击时如何更改汉堡菜单图标? - How to change hamburger menu icon when clicked? 如何获得响应式汉堡包菜单不要压低内容 - how to get responsive hamburger menu to not push down content 单击时如何阻止我的引导程序汉堡包图标和可折叠菜单跳跃? - How can I stop my bootstrap hamburger icon and collapsible menu from jumping when clicked? 单击锚点后如何关闭汉堡菜单 - How to close down an hamburger menu after an anchor is clicked 使用 Javascript 在菜单外单击时如何关闭汉堡菜单 - How to close Hamburger menu when clicked outside the menu using Javascript 汉堡菜单:单击时汉堡图标和 X 图标不一致切换 - HAMBURGER MENU: Burger-icon and X-icon inconsistent toggling when clicked 单击汉堡菜单时如何更改徽标src? - How to change logo src, when clicked on hamburger menu? 单击菜单内的链接后,如何将汉堡包菜单图标重置回未打开状态? - How to reset hamburger menu icon back to unopened after link inside of menu is clicked? 在反应中单击汉堡包图标时如何显示菜单列表 - How to display the menu list when clicking on the hamburger icon in react 如何在展开时使我的汉堡菜单完全可见,而不是将轮播图像滑块向下推? - How can I make my hamburger menu fully visible when expanded, and not push my carousel image slider down?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM