如何仅在悬停菜单项或 div 时显示 div?
我的项目包含在无序列表中。我在此列表之外隐藏了 div(使用 display:none;)。当您将鼠标悬停在列表项上时,我希望显示 div。如果您将鼠标从列表项移动到其下方的 div,它也必须保持可见。
我愿意通过 CSS、JavaScript/jQuery 来实现这一目标。另外,我的文档设置为 HTML5。
这就是我目前正在做的工作。
<nav id="main-menu">
<ul>
<li><a class="first-link parent" href="">First Link</a></li>
<li><a class="second-link parent" href="">Second Link</a></li>
<li><a class="third-link parent" href="">Third Link</a></li>
<li><a class="fourth-link parent" href="">Fourth Link</a></li>
</ul>
</nav><!-- #main-menu !-->
在导航下方,我有将鼠标悬停在相应项目上时想要显示的内容。
<div id="first-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .first-widget !-->
<div id="second-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .second-widget !-->
<div id="third-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .third-widget !-->
<div id="fourth-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .fourth-widget !-->
</aside><!-- .hidden !-->
我觉得应该有一个简单的解决方案,但到目前为止我的尝试还不够。最大的问题是不仅在悬停菜单项时显示 div,而且在从菜单移动到可见 div 时也显示 div。
感谢您的任何建议,感谢您花时间阅读本文。
I have items contained in an unordered list. I have hidden divs (using display:none;) outside of this list. When you hover over a list item, I would like the div to appear. It must also remain visible if you move your mouse from the list item to the div below it.
I am open to accomplish this through CSS, JavaScript/jQuery. Also, my document is set to HTML5.
This is what I am currently working with.
<nav id="main-menu">
<ul>
<li><a class="first-link parent" href="">First Link</a></li>
<li><a class="second-link parent" href="">Second Link</a></li>
<li><a class="third-link parent" href="">Third Link</a></li>
<li><a class="fourth-link parent" href="">Fourth Link</a></li>
</ul>
</nav><!-- #main-menu !-->
And beneath the nav, I have the content that I would like to display when hovering over the corresponding item.
<div id="first-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .first-widget !-->
<div id="second-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .second-widget !-->
<div id="third-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .third-widget !-->
<div id="fourth-widget" class="widget-container">
<h2>Sub Title</h2>
<p>Lorem Ipsum
<a href="#">Read More</a></p>
</div><!-- .fourth-widget !-->
</aside><!-- .hidden !-->
I feel like there should be an easy solution to this, but my attempts so far have fallen short. The biggest problem is having the div show not only when hovering the menu item, but also when you move from the menu to the visible div.
Any advice is appreciate, thank you for taking the time to read this far.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将小部件包装在
div
中,并向其附加mouseleave
事件。此解决方案还使用 rel 属性来存储小部件 id :DEMOHTML
JS
You can wrap the widgets in a
div
and attach amouseleave
event to it. This solution also uses therel attibute
to store the widgetid
: DEMOHTML
JS
像这样的东西应该有效。
使用
.hover
绑定事件,.index
和.eq
查找哪个一种用于显示/隐藏,以及用于超时的本机 JS 方法,以便您将鼠标悬停在 div 上。Something like this should work.
Using
.hover
to bind the event,.index
and.eq
to find which one to show/hide and the native JS methods for timeouts to let you hover the divs.