gpt4 book ai didi

Why on smaller screens, does my nested-menu not work?(为什么在小屏幕上,我的嵌套菜单不起作用?)

转载 作者:bug小助手 更新时间:2023-10-28 11:30:11 25 4
gpt4 key购买 nike



On smaller screens, I want the dropdown menu and nested menu to only appear when they're clicked. I.e you click apple, and red apple and green apple appear, if you click red apple, tasty appears. Etc. However, I can't seem to get the click to work after the initial clicking of apple shows red apple and green apple. I can never get tasty to appear on smaller screens.

在较小的屏幕上,我希望下拉菜单和嵌套菜单只在单击时出现。即点击苹果,会出现红苹果和绿苹果,点击红苹果,就会出现美味。等。然而,在苹果的初始点击显示红色苹果和绿色苹果之后,我似乎无法让点击工作。出现在较小的屏幕上,我永远不会有味道。


https://codepen.io/Dezalops/pen/yLGMbqQ
Here's the codepen

Https://codepen.io/Dezalops/pen/yLGMbqQ,这就是编剧


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<style>
/* Basic Styles */
.navbar {
background-color: #333;
}

.main-menu {
list-style-type: none;
padding: 0;
display: flex;
justify-content: space-between;
}

.menu-item {
position: relative;
margin-right: 20px;
}

a {
text-decoration: none;
color: white;
padding: 10px;
display: block;
}

/* Dropdown Styles */
.submenu {
display: none;
position: absolute;
background-color: #444;
top: 100%;
left: 0;
padding: 0;
z-index: 1;
}

/* Media Query for Larger Screens (Hover) */
@media screen and (min-width: 769px) {
.menu-item:hover .submenu {
display: block;
}

.submenu li:hover .nested-menu {
display: block;
}
}

/* Nested Menu Styles */
.submenu li {
padding: 10px;
}

.nested-menu {
display: none;
position: absolute;
top: 0;
left: 100%;
background-color: #555;
padding: 0;
z-index: 2;
}
</style>
<title>Navbar</title>
</head>
<body>
<nav class="navbar">
<ul class="main-menu">
<li class="menu-item">
<a href="#">Apple</a>
<ul class="submenu">
<li class="menu-item">
<a href="#">Red Apple</a>
<ul class="nested-menu">
<li><a href="#">Tasty</a></li>
</ul>
</li>
<li class="menu-item">
<a href="#">Green Apple</a>
<ul class="nested-menu">
<li><a href="#">Sweet</a></li>
</ul>
</li>
</ul>
</li>
<li class="menu-item">
<a href="#">Cake</a>
<ul class="submenu">
<li class="menu-item">
<a href="#">Chocolate Cake</a>
<ul class="nested-menu">
<li><a href="#">Rich</a></li>
</ul>
</li>
<li class="menu-item">
<a href="#">Vanilla Cake</a>
<ul class="nested-menu">
<li><a href="#">Light</a></li>
</ul>
</li>
</ul>
</li>
<li class="menu-item">
<a href="#">Tea</a>
<ul class="submenu">
<li class="menu-item">
<a href="#">Black Tea</a>
<ul class="nested-menu">
<li><a href="#">Strong</a></li>
</ul>
</li>
<li class="menu-item">
<a href="#">Green Tea</a>
<ul class="nested-menu">
<li><a href="#">Healthy</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</nav>
<script>
// JavaScript for toggling the dropdowns on click for smaller screens
const menuItems = document.querySelectorAll(".menu-item");

menuItems.forEach((menuItem) => {
menuItem.addEventListener("click", (event) => {
if (window.innerWidth <= 768) {
// Prevent event bubbling
event.stopPropagation();

// Toggle the active class to show/hide the submenu
menuItem.classList.toggle("active");

// Toggle the submenu visibility
const submenu = menuItem.querySelector(".submenu");
if (submenu) {
submenu.style.display =
submenu.style.display === "block" ? "none" : "block";
}
}
});
});

// Close submenus when clicking anywhere outside for smaller screens
document.addEventListener("click", () => {
if (window.innerWidth <= 768) {
menuItems.forEach((menuItem) => {
menuItem.classList.remove("active");
const submenu = menuItem.querySelector(".submenu");
if (submenu) {
submenu.style.display = "none";
}
});
}
});
</script>
</body>
</html>

I have tried numerous javascript and css styles and nothing works, ever.

我尝试了很多的javascrip和css样式,但都不起作用。


更多回答

Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.

请澄清您的具体问题或提供更多详细信息,以突出您的确切需求。按照目前的写法,很难准确地说出你在问什么。

优秀答案推荐

Your event click function will only target the "submenu" class to change the display and your nested menus only have the class "nested-menu" so when you click on "chocolate cake" the submenu item returned is null. If you add the submenu class to each nested menu element it will "work" but will still have some issues. (it will break your css hover effect, and nested menus will stay open)

您的事件单击函数将只针对“SubMenu”类来更改显示,而您的嵌套菜单只有“Nest-Menu”类,因此当您单击“巧克力蛋糕”时,返回的子菜单项为空。如果您将SubMenu类添加到每个嵌套的Menu元素,它将“工作”,但仍会有一些问题。(它将打破您的css悬停效果,嵌套菜单将保持打开状态)


I think your best solution involves setting up a different click event on the submenus that is intended to target your nested menus.

我认为您最好的解决方案是在子菜单上设置一个不同的Click事件,该事件旨在针对您的嵌套菜单。


更多回答

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com