简体   繁体   English

当我切换菜单以关闭窗口并调整窗口大小时,如何防止徽标消失?

[英]How do I prevent my logos from disappearing when I toggle my menu to close and resize my window?

I have a menu of logos that will be clickable.我有一个可点击的徽标菜单。 They always display except on smaller screens, where they need to be toggled to show using a hamburger menu.它们总是显示在较小的屏幕上,在较小的屏幕上需要切换以使用汉堡菜单显示。 The menu toggles fine when it is on a smaller screen, but when you resize the window (if the menu is closed), all of the logos display as "none" and won't toggle back.当菜单在较小的屏幕上时切换正常,但是当您调整窗口大小时(如果菜单关闭),所有徽标都显示为“无”并且不会切换回来。 I'm not sure if I need to adjust my css or if my JS is off.我不确定是否需要调整我的 css 或者我的 JS 是否关闭。

 document.getElementById('menu').addEventListener('click', myFunction); function myFunction() { let logo = document.getElementsByClassName("team"); for (i = 0; i < logo.length; i++) { if (logo[i].style.display === 'none') { logo[i].style.display = 'inline'; } else { logo[i].style.display = 'none'; } } }
 .team { width: 55px; display: flex; } .menu-icon { display: none; } @media screen and (max-width: 600px) { .mobile-container { margin: auto; height: fit-content; } .menu-icon { display: inline; width: 100%; background-color: red; } .team { display: none; } }
 <div class="wrapper"> <div class="container mobile-container"> <a href="#" class="menu-icon" id="menu"> <img src="https://via.placeholder.com/55"> </a> <div class="sidebar"> <div class="column logos"> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> </div> </div> </div> </div>

The problem is that when you click on the menu button, you're adding inline styles for all the menu items, so when you hide them all of them have display: none;问题在于,当您单击菜单按钮时,您正在为所有菜单项添加内联样式,因此当您隐藏它们时,所有这些样式都会display: none; and they stay like that no matter the screen size.无论屏幕大小如何,它们都会保持这种状态。

What you need to do is to alter the display property of the menu items only on mobile.您需要做的是仅在移动设备上更改菜单项的显示属性。 You can achieve this by using a class that affects the styles of the items on mobile only.您可以通过使用仅影响移动设备上项目样式的类来实现此目的。 See below the .hide-mobile class, and the JS code that just toggles that class on and off.请参阅下面的.hide-mobile类,以及用于打开和关闭该类的 JS 代码。

 document.getElementById('menu').addEventListener('click', myFunction); function myFunction() { let logo = document.getElementsByClassName("team"); for (i = 0; i < logo.length; i++) { logo[i].classList.toggle('hide-mobile'); } }
 .team { width: 55px; display: flex; } .menu-icon { display: none; } @media screen and (max-width: 600px) { .mobile-container { margin: auto; height: fit-content; } .menu-icon { display: inline; width: 100%; background-color: red; } .team { display: inline; } .team.hide-mobile { display: none; } }
 <div class="wrapper"> <div class="container mobile-container"> <a href="#" class="menu-icon" id="menu"> <img src="https://via.placeholder.com/55"> </a> <div class="sidebar"> <div class="column logos"> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> <a href=""><img src="https://via.placeholder.com/55" alt="" class="team"></a> </div> </div> </div> </div>

Hope that helps.希望有帮助。

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

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