简体   繁体   English

CSS,HTML:侧边栏,动画为1按钮,不是2

[英]CSS, HTML: Sidebar with animation as 1 button not as 2

I got side menu with one button "M" that opens the menu, but when the menu is opened instead of 10 buttons i get 20 like B-Button, B is image and Button is text. 我得到了带有一个用于打开菜单的“ M”按钮的侧面菜单,但是当打开菜单而不是10个按钮时,我得到20个像B-Button一样的东西,B是图像,Button是文本。 When the menu is opened they need to be the same button not 2 separate ones, I know they are coded to be 2 separate buttons but in what direction should i go to make them 1 button only when the menu is opened 当打开菜单时,它们需要是相同的按钮,而不是2个单独的按钮,我知道它们被编码为2个单独的按钮,但是只有在打开菜单时,我应该朝哪个方向将它们设为1个按钮

Code: 码:

  function MenuButton() { if (document.getElementById("mySidenav").style.width == "150px") { document.getElementById("mySidenav").style.width = "0px"; } else { document.getElementById("mySidenav").style.width = "150px"; } } 
 .sidenav { height: 100%; width: 0; position: fixed; top: 34px; left: 49px; z-index: 2; background-color: #111; overflow: hidden; transition: 0.5s; padding-top: 55px; } .sidenav a { font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: normal; font-weight: normal; font-stretch: normal; font-size: 25px; line-height: 1; padding: 8px 8px 8px 15px; text-decoration: none; display: block; color: white; transition: 0.3s; white-space: nowrap; } .sidenav a:hover { background-color: #3498db; } .buttons { height: 100%; width: 50px; position: fixed; margin: 0 auto; top: 34px; left: 0; z-index: 1; background-color: #111; overflow: hidden; transition: 0.5s; padding-top: 14px; } .buttons a{ font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-variant-numeric: normal; font-weight: normal; font-stretch: normal; font-size:25px; line-height: 1; padding: 8px 8px 8px 15px; text-decoration: none; color: white; display: block; transition: 0.3s; cursor: pointer; } .buttons a:hover{ background-color: #3498db; } 
 <div id="mySidenav" class="sidenav"> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> </div> <div id="buttonside" class="buttons"> <a><span onclick="MenuButton()">M</span></a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> <a>B</a> </div> 

JSFiddle Link JSFiddle链接

Try to wrap your icons or images into a span with some width like below 尝试将图标或图像包装成一定宽度的span ,如下所示

 function MenuButton() { if (document.getElementById("buttonside").style.width == "150px") { document.getElementById("buttonside").style.width = "50px"; } else { document.getElementById("buttonside").style.width = "150px"; } } 
 .sidenav { background-color: #111; overflow: hidden; transition: 0.5s; } .sidenav a:hover { background-color: #3498db; } .buttons { width: 50px; background-color: #111; overflow: hidden; transition: 0.5s; padding-top: 14px; } .buttons a { font-size: 25px; line-height: 1; padding: 8px 0px; text-decoration: none; color: white; display: block; transition: 0.3s; cursor: pointer; white-space: nowrap; } .buttons a span { width: 50px; display: inline-block; text-align: center; } .buttons a:hover { background-color: #3498db; } 
 <div id="buttonside" class="buttons"> <a onclick="MenuButton()"><span>M</span></a> <div id="mySidenav" class="sidenav"> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> <a><span>B</span>Link</a> </div> </div> 

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

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