简体   繁体   English

隐藏带有按钮的响应式导航以打开

[英]Hiding a responsive navigation with button to open

I am trying to hide my menu when a page gets too small and has a button to open it if required.当页面变得太小并且有一个按钮可以在需要时打开它时,我试图隐藏我的菜单。 I am struggling to make it happen.我正在努力让它发生。 I can hide the navigation when the screen shrinks and display the div button to reopen it but cannot get the menu to reappear.我可以在屏幕缩小时隐藏导航并显示 div 按钮以重新打开它,但无法重新出现菜单。 I have tried to use focus to bring up the original side nav div with little success thinking that it is not overwriting the code to hide it on screen size.我试图使用焦点来调出原始的侧面导航 div,但认为它没有覆盖代码以将其隐藏在屏幕大小上,但收效甚微。 How do I get around this?我该如何解决这个问题?

Any guidance greatly appreciated.非常感谢任何指导。

 .sidenav { height: 100%; width: 220px; position: fixed; z-index: 1; top: 0; left: 0; background-color: #111; overflow-x: hidden; padding-top: 20px; -webkit-box-shadow: 2px 0px 5px 0px rgba(0,0,0,0.75); -moz-box-shadow: 2px 0px 5px 0px rgba(0,0,0,0.75); box-shadow: 2px 0px 5px 0px rgba(0,0,0,0.75); } .sidenav a { padding: 6px 8px 6px 16px; text-decoration: none; font-size: 25px; color: #00aad4; display: block; } .sidenav a:hover { color: #000; } /* Style page content */ .main { margin-left: 220px; /* Same as the width of the sidebar */ padding: 0px 10px; } @media screen and (max-height: 450px) { .sidenav {padding-top: 15px;} .sidenav a {font-size: 18px;} } .HideMenu { display:none; } .ShowMenu { display:block; } /* Desktop*/ @media screen and (min-width: 768px) { .HideMenu { display: block; } } /* Mobile*/ @media screen and (max-width: 768px) { .ShowMenu:focus ~ .sidenav { display: block; } }
 <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> <div class="HideMenu"> <div class="sidenav"> <img src="images/logo.png"> <a href="#">Textures</a> <a href="#">HDRI's</a> </div> </div> <!-- Content –––––––––––––––––––––––––––––––––––––––––––––––––– --> <div class="grid-container halves full" style="background-color: #000;"> <div>Centre whole</div> </div> <div class="ShowMenu" tabindex="0">Show Me</div> <div class="grid-container halves"> <div> 50 L </div> <div> 50 R </div> </div> <div class="grid-container thirds"> <div>Third L</div> <div>Thirs M</div> <div>Third R</div> </div>

Here's the HTML code这是 HTML 代码

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="HideMenu">
<div class="sidenav">
<img src="images/logo.png">
  <a href="#">Textures</a>
  <a href="#">HDRI's</a>
</div>  
</div>
<!------------------------------- Content ------------------------------->

 <div class="grid-container halves full"  style="background-color: #000;">
        <div>Centre whole</div> 
 </div>
 <div class="ShowMenu" tabindex="0">Show Me</div>
 <div class="grid-container halves">
      <div>
        50 L
      </div>
      <div>
          50 R
      </div>
    </div>

<div class="grid-container thirds">
    <div>Third L</div>
    <div>Thirs M</div>
    <div>Third R</div>
</div>

<!-- You'll need to import the javascript file with the <script> tag -->
<script type="text/javascript" src="/path/to/desktop-mobile-layout-handler.js"></script>

Here's the CSS code这是 CSS 代码

.sidenav {
  height: 100%; 
  width: 220px; 
  position: fixed; 
  z-index: 1; 
  top: 0; 
  left: 0;
  background-color: #111; 
  overflow-x: hidden; 
  padding-top: 20px;
  -webkit-box-shadow: 2px 0px 5px 0px rgba(0,0,0,0.75);
    -moz-box-shadow: 2px 0px 5px 0px rgba(0,0,0,0.75);
    box-shadow: 2px 0px 5px 0px rgba(0,0,0,0.75);
}


.sidenav a {
  padding: 6px 8px 6px 16px;
  text-decoration: none;
  font-size: 25px;
  color: #00aad4;
  display: block;
}


.sidenav a:hover {
  color: #000;
}

/* Style page content */
.main {
  margin-left: 220px; /* Same as the width of the sidebar */
  padding: 0px 10px;
}

/*
@media screen and (max-height: 450px) {
  .sidenav {padding-top: 15px;}
  .sidenav a {font-size: 18px;}
}
*/


.HideMenu { display:none; }
.ShowMenu { display:block; }

/* Desktop*/
/*
@media screen and (min-width: 768px) {
  .HideMenu  { display: block; }
}
*/

/* Mobile*/
/*
@media screen and (max-width: 768px) {
  .ShowMenu:focus ~ .sidenav { display: block; }
}
*/

And finally, the JavaScript code最后,JavaScript 代码

// desktop-mobile-layout-handler.js

var screenHeight = window.screen.availHeight;
var screenWidth = window.screen.availWidth;

var hideMenuElement = document.querySelector(".HideMenu");
var sideNavElement = document.querySelector(".sidenav");
var sideNavLinks = document.querySelectorAll(".sidenav a");

function desktopLayout () {
    hideMenuElement.style.display = "block"; // hide the .HideMenu element
}

function mobileLayout () {
    sideNavElement.onfocus = function () { // when the .sidenav element is being focused
        sideNavElement.style.display = "block"; // display the .sidenavelement
    }
    sideNavElement.onblur = function () { // when the .sidenav element is not being focus
        sideNavElement.style.display = "none"; // hide the .sidenav element
    }
}

if (screenHeight <= 450) {
    sideNavElement.style.paddingTop = "15px";
    sideNavLinks.forEach(function (elem) {
        elem.style.fontSize = "18px";
    });
}

if (screenWidth >= 768) { // use Desktop layout
    desktopLayout();
}
else { // use Mobile Layout
    mobileLayout();
}

The javascript code will help you fix the CSS error you had above. javascript 代码将帮助您修复上面的 CSS 错误。

I hope this helps我希望这有帮助

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM