简体   繁体   English

当我在菜单文本上 hover 时,如何让 Sub Nav Dropdown 仅显示?

[英]How to get Sub Nav Dropdown to only display when I hover over the menu text?

Right now I have a sub nav container inside of my "services" menu item.现在我的“服务”菜单项中有一个子导航容器。 My issue is whenever I hover over the sub nav container, it will show up, but I only want it to display when I hover over the text "services".我的问题是,每当我在子导航容器上进行 hover 时,它就会显示出来,但我只希望它在我 hover 在文本“服务”上显示时显示。

I don't know how I am able to solve this because the entire sub nav is located inside of my services text li tags, so no matter where I hover, it always triggers it because everything is located inside of the li "services" tag我不知道如何解决这个问题,因为整个子导航都位于我的服务文本 li 标签内,所以无论我在哪里 hover,它总是会触发它,因为所有内容都位于 li“服务”标签内

Here is a codepen showing issue.这是一个显示问题的代码笔。 https://codepen.io/designextras/pen/RwrqzwN https://codepen.io/designextras/pen/RwrqzwN

I only want the sub nav to show when I hover over the text "services", You might have to refresh the screen multiple times to see the issue.我只想在 hover 在文本“服务”上显示子导航,您可能需要多次刷新屏幕才能看到问题。 If you just move the mouse across the blank white area, it will still trigger the sub nav because that div is being hovered over.如果您只是将鼠标移过空白区域,它仍然会触发子导航,因为该 div 被悬停在上面。

Here is the services li code with the sub nav code inside这是服务 li 代码,里面有 sub nav 代码

 <li id="services"><a href="/">Services</a>
                    <div class="sub-nav" id="sub-nav">
                        <div class="sub-nav-col left">
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                        </div>
                        <div class="sub-nav-col right">
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                            <a href="/" class="sub-nav-box">
                                <div class="icon-background">
                                    <i class="fas fa-user-lock icon"></i>
                                </div>
                                <h4>Sync and Organize</h4>
                                <p>Keep your notes handy</p>
                            </a>
                        </div>
                    </div>
                    </li>

I feel like the only way is to use Javascript and hard code the html through there, but that seems excessive and doesn't make sense for such a simple hover effect.我觉得唯一的方法是使用 Javascript 并通过那里硬编码 html ,但这似乎太过分了,对于如此简单的 hover 效果来说没有意义。

Let me know what you think让我知道你的想法

You can use simply display:none to hide your submenu and display:flex in active class to toggle the sub menu.您可以简单地使用 display:none 来隐藏您的子菜单和 display:flex 在活动的 class 中切换子菜单。 In javascript, you are just using mouseenter event, here you can play with mouseover and mouseout event and make active class toggle on hover.在 javascript 中,您只是使用 mouseenter 事件,在这里您可以使用 mouseover 和 mouseout 事件并在 hover 上激活 class 切换。 anchor text display value should be inline block for better cursor position.锚文本显示值应该是内联块以获得更好的 cursor position。

You can also make this hover functionality without javascript.您也可以在没有 javascript 的情况下使用此 hover 功能。

Just add this css in your css file.只需在 css 文件中添加此 css 即可。

#services:hover #sub-nav {
  display: flex;
}

and remove JS code.并删除 JS 代码。

 const services = document.querySelector('#services') const subNav = document.querySelector('#sub-nav') services.addEventListener('mouseenter', e => { subNav.classList.add('active') }); services.addEventListener('mouseout', e => { subNav.classList.remove('active') });
 * { box-sizing: border-box; margin: 0; padding: 0; font-family: 'Montserrat', sans-serif; -webkit-font-smoothing: antialiased; }.global-header { position: fixed; top: 0; width: 100%; background: red; z-index: 999; }.top { }.row { display: flex; align-items: center; height: 80px; padding: 0; max-width: 1000px; margin: 0 auto; }.global-logo { display: inline-block; }.nav-menu { display: inline-block; vertical-align: top; margin-top: 22px; margin-left: 50px; } ul { font-size: 13px; line-height: 21px; margin:0; padding:0; display: flex; align-items: center; } li:first-child { margin-left: 0; } li { display: inline-block; margin-left: 28px; position: relative; } li > a { font-size: 14px; text-transform: uppercase; letter-spacing: .5px; text-decoration: none; display: inline-block; padding:0 0 22px 0; }.sub-nav { background: blue; border-radius: 5px; box-shadow: 0 10px 8px 0 rgba(0,0,0,0.2); display: none; left: -120px; padding: 16px; position: absolute; top: 100%; transition: .1s all ease-in-out; width: 628px; }.active{ display: flex; }.sub-nav-col { display: flex; flex-direction: column; flex: 1; max-width: 50%; background: red; }.left { margin-right: 4px; }.right { margin-left: 4px; }.sub-nav-box { background: #FAFAFA; border-radius: 3px; display: flex; flex: 0 1 auto; flex-direction: column; font-size: 14px; justify-content: center; margin: 4px 0; min-height: 100px; padding: 24px 14px 24px 96px; position: relative; transition: .1s all ease-in-out; text-decoration: none; }.icon-background { height: 64px; width: 64px; background: #252222; border-radius: 50%; display: flex; justify-content: center; align-items: center; position: absolute; left: 14px; }.icon { color: #fff; font-size: 20px; }.sub-nav-box h4 { font-size: 12px; letter-spacing: 0.9px; line-height: 16px; margin: 0 0.5em; padding: 0; text-transform: uppercase; }.sub-nav-box p { color: red; line-height: 16px; width: 100%; }.sub-nav::after { bottom: 100%; left: 140px; content: ''; height: 0; width: 0; position: absolute; pointer-events: none; border: solid transparent; border-color: rgba(252,252,252,0); border-bottom-color: #FCFCFC; border-width: 20px; border-radius: 2px; margin-left: -20px; }
 <,DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width. initial-scale=1.0"> <title>Dropdown Nav Menu</title> <link rel="stylesheet" href="style:css"> <link href="https.//fonts.googleapis?com/css2:family=Montserrat&display=swap" rel="stylesheet"> <link rel="stylesheet" href="https.//use.fontawesome.com/releases/v5.14.0/css/all.css" integrity="sha384-HzLeBuhoNPvSl5KYnjx0BT+WB0QEEqLprO+NBkkk5gbc67FTaL7XIGa2w1L0Xbgc" crossorigin="anonymous"> </head> <body> <header class="global-header"> <div class="top"> <div class="row"> <div class="global-logo"> <a href="/">Logo</a> </div> <nav class="nav-menu"> <ul> <li><a href="/">Home</a></li> <li id="services"><a href="/">Services</a> <div class="sub-nav" id="sub-nav"> <div class="sub-nav-col left"> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> </div> <div class="sub-nav-col right"> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> <a href="/" class="sub-nav-box"> <div class="icon-background"> <i class="fas fa-user-lock icon"></i> </div> <h4>Sync and Organize</h4> <p>Keep your notes handy</p> </a> </div> </div> </li> </ul> </nav> </div> </div> </header> <script src="app.js"></script> </body> </html>

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

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