当鼠标退出 div 区域时自动折叠下拉导航
我制作了一个简单的下拉导航,当您将鼠标悬停在链接上时,该导航会展开。
当鼠标悬停在链接上时,绝对定位的 div 将展开,显示子导航。我的问题是,当鼠标离开导航区域时,这个 div 不会自动折叠,而这正是我需要发生的。
我已将 onMouseover 命令添加到导航中的其他链接,该命令将在激活时折叠 div,但我真正需要的功能是当鼠标离开 div 区域时下拉 div 折叠。
我非常感谢对此的任何帮助。
------编辑-----
感谢您的帮助@Tuanderful。我以一种不优雅(但有效)的方式解决了这个问题。
我只是将带有关闭触发器的 onmouseover 事件添加到导航下方和上方的 div 中。这样,当用户的光标离开下拉导航时,触发器会触发并且下拉菜单会自动关闭。
我同意纯 CSS 方法是理想的,但由于此导航的一些其他复杂性,我不得不使用 Javascript 解决方案。
I put together a simple drop-down navigation that expands when you hover over a link.
When hovering over a link an absolute positioned div will expand, revealing the sub-navigation. My problem is that this div doesn't collapse automatically when the mouse leaves the navigation area, and that's what I need to happen.
I've added onMouseover commands to the other links in the navigation that will collapse the div when activated, but the functionality that I really need is for the drop-down div to collapse when the mouse leaves the div area.
I greatly appreciate any help with this.
------EDIT-------
Thanks for the help @Tuanderful. I solved this problem in an inelegant (yet effective) fashion.
I simply added the onmouseover event with a close trigger to the divs immediately below and above the navigation. This way when a user's cursor leaves the drop-down navigation, the trigger fires and the drop-down closes automatically.
I agree that a pure CSS approach would have been ideal, but due to some other complications with this navigation I had to go with the Javascript solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试使 div#subnav1 成为嵌套列表,而不是使 #subnav1 成为 #nav 的同级;您的导航结构将如下所示:
然后您可以使用 CSS(而不是 javascript)来控制子导航的隐藏和显示。这可以通过 :hover 伪选择器来完成。
该方法是最初隐藏子菜单
当用户鼠标悬停在主菜单项上时,使用 :hover 选择器显示子菜单
如果您决定更改 HTML 结构,您可能需要继续适当地完善 CSS/图像,以使所有内容都到位。
可以在 A List Apart: Horizontal DropDowns。可以在 创建纯 CSS 下拉菜单
Instead of making #subnav1 a sibling of #nav, you can try make div#subnav1 a nested list; your nav structure would look like:
Then you can use CSS (instead of javascript) to control the hiding and showing of the sub-nav. This can be done with the :hover pseudo-selector.
The approach is to initially hide the submenu with
When the user mouse's over the main menu's items, use the :hover selector to display the sub-menu
If you decide to change the HTML structure you may need to continue to refine your CSS/images appropriately for everything to fall into place.
A good resource that goes into this approach in further detail can be found at A List Apart: Horizontal DropDowns. An even more robust example (and slightly different approach) can be found at this CSS Wizardry article on Creating a Pure CSS Dropdown Menu