简体   繁体   English

关闭画布菜单html css

[英]off canvas menu html css

I want to make a off canvas menu like this but I shall push the text out of the screen and not crop. 我想像这样创建一个脱画布菜单,但是我将文本推出屏幕而不是裁剪。 And I want to add a function that close the menu after I clicked out of it. 我想添加一个在单击菜单后关闭菜单的功能。

I got the code from w3school off canvas menu. 我从画布菜单上的w3school获得了代码。

 function openNav() { document.getElementById("mySidenav").style.width = "250px"; document.getElementById("main").style.marginLeft = "250px"; } function closeNav() { document.getElementById("mySidenav").style.width = "0"; document.getElementById("main").style.marginLeft= "0"; } 
 body { font-family: "Lato", sans-serif; } .sidenav { height: 100%; width: 0; position: fixed; z-index: 1; top: 0; left: 0; background-color: #111; overflow-x: hidden; transition: 0.5s; padding-top: 60px; } .sidenav a { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #818181; display: block; transition: 0.3s; } .sidenav a:hover, .offcanvas a:focus{ color: #f1f1f1; } .sidenav .closebtn { position: absolute; top: 0; right: 25px; font-size: 36px; margin-left: 50px; } #main { transition: margin-left .5s; padding: 16px; } @media screen and (max-height: 450px) { .sidenav {padding-top: 15px;} .sidenav a {font-size: 18px;} } 
 <div id="mySidenav" class="sidenav"> <a href="javascript:void(0)" class="closebtn" onclick="closeNav()">&times;</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Clients</a> <a href="#">Contact</a> </div> <div id="main"> <h2>Sidenav Push Example</h2> <p>Click on the element below to open the side navigation menu, and push this content to the right.</p> <span style="font-size:30px;cursor:pointer" onclick="openNav()">&#9776; open</span> </div> 

Rather than trying to add / remove CSS rules on the click events - try using a single function that adds / removes a class to achieve the opening / closing. 与其尝试在click事件上添加/删除CSS规则,不如尝试使用添加/删除一个类的单个函数来实现打开/关闭。

I create an open class that had the styling that you had in the js - then its just a case of adding / removing the class to allow the sidenav to open / close. 我创建了一个开放类,它具有您在js中的样式-然后只是添加/删除该类以允许sidenav打开/关闭的一种情况。

This is not how I would suggest to have it - but keeping it in touch with your original code as much as possible - I wrapped the entire thing in a div with an id of wrapper - and then said that on the click of that div (which contains the side nav and the main panel) that if the wrapper has the open class - then remove it (closing the sidenav) and if not - open it. 这不是我建议的方式-但要使其与您的原始代码尽可能保持联系-我将整个内容包装在具有包装ID的div中,然后在单击该div时说(包含侧面导航栏和主面板),如果包装器具有开放类,则将其移除(关闭sidenav),否则,将其打开。

You will need to look at what you want to trigger the opening - I assume you don't want any click on the entire pain panel opening the side nave - but this should give you an idea on how to get a click outside of the sidenav to close it. 您将需要查看要触发打开的内容-我假设您不希望在整个疼痛面板上单击以打开侧面中殿-但这应为您提供一个如何在侧面导航之外进行点击的想法关闭它。

 function toggleNav() { var wrapper = document.getElementById("wrapper"); if(wrapper.classList.contains("open")){ wrapper.classList.remove("open") } else { wrapper.classList.add("open"); }; } 
 body { font-family: "Lato", sans-serif; } .sidenav { height: 100%; width: 0; position: fixed; z-index: 1; top: 0; left: 0; background-color: #111; overflow-x: hidden; transition: 0.5s; padding-top: 60px; } .open #mySidenav { width:250px; } .open #main { margin-left:250px; } .sidenav a { padding: 8px 8px 8px 32px; text-decoration: none; font-size: 25px; color: #818181; display: block; transition: 0.3s; } .sidenav a:hover, .offcanvas a:focus{ color: #f1f1f1; } .sidenav .closebtn { position: absolute; top: 0; right: 25px; font-size: 36px; margin-left: 50px; } #main { transition: margin-left .5s; padding: 16px; } @media screen and (max-height: 450px) { .sidenav {padding-top: 15px;} .sidenav a {font-size: 18px;} } 
 <div id="wrapper" class="" onclick="toggleNav()"> <div id="mySidenav" class="sidenav"> <a href="javascript:void(0)" class="closebtn">&times;</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Clients</a> <a href="#">Contact</a> </div> <div id="main"> <h2>Sidenav Push Example</h2> <p>Click on the element below to open the side navigation menu, and push this content to the right.</p> <span style="font-size:30px;cursor:pointer">&#9776; open</span> </div> <div> 

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

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