如何为此菜单设置鼠标悬停延迟?
我有下拉菜单,我想在鼠标移开时添加延迟。因此,当您将鼠标悬停在菜单上时,它将显示下拉列表,但是当您从菜单中移动鼠标时,我希望有一些延迟。
我已经搜索了一整天,但没有找到任何东西,期待我不知道使用的hoverIntent插件。
这是我的代码:
HTML:
<ul id='nav'>
<li><a href='#'>Top level 1</a></li>
<li><a href='#'>Top level 2</a>
<ul>
<li><a href='#'>Sub 2 - 1</a></li>
<li>
<a href='#'>Sub 2 - 2</a>
<ul>
<li>
<a href='#'>Sub 2 - 2 - 1</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 1 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 4</a></li>
</ul>
</li>
<li><a href='#'>Sub 2 - 2 - 2</a></li>
<li>
<a href='#'>Sub 2 - 2 - 3</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 3 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 4</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>Sub 2 - 3</a></li>
</ul>
</li>
</ul>
Jquery:
$(document).ready(function(){
$("#nav ul ").css({display: "none"});
$("#nav li").hover(
function()
{
$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideToggle();
},
function(){
$(this).find('ul:first').css({visibility: "hidden"});
}
);
请假设我是 javascript 的大菜鸟:)
谢谢!
编辑:
我已经尝试了所有的解决方案,但除了帕特里夏的解决方案之外,没有一个有效。现在我得到了奇怪的行为,正如您在 http://pastehtml.com/view/aykmhy9ae.html
I have drop down menu, and I want to add delay on mouseout. So when you hover over the menu, it ll display drop down, but when you move mouse from the menu, I want to there be some delay.
I have searched all day for this, and didnt found anything, expect hoverIntent plugin which i dont know to use.
Here is my code:
HTML:
<ul id='nav'>
<li><a href='#'>Top level 1</a></li>
<li><a href='#'>Top level 2</a>
<ul>
<li><a href='#'>Sub 2 - 1</a></li>
<li>
<a href='#'>Sub 2 - 2</a>
<ul>
<li>
<a href='#'>Sub 2 - 2 - 1</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 1 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 1 - 4</a></li>
</ul>
</li>
<li><a href='#'>Sub 2 - 2 - 2</a></li>
<li>
<a href='#'>Sub 2 - 2 - 3</a>
<ul>
<li><a href='#'>Sub 2 - 2 - 3 - 1</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 2</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 3</a></li>
<li><a href='#'>Sub 2 - 2 - 3 - 4</a></li>
</ul>
</li>
</ul>
</li>
<li><a href='#'>Sub 2 - 3</a></li>
</ul>
</li>
</ul>
Jquery:
$(document).ready(function(){
$("#nav ul ").css({display: "none"});
$("#nav li").hover(
function()
{
$(this).find('ul:first').css({visibility: "visible",display: "none"}).slideToggle();
},
function(){
$(this).find('ul:first').css({visibility: "hidden"});
}
);
Please assume, that I'm big noob for javascript :)
Thanks!
EDIT:
I have tried all the solutions, and none worked expect the solution from Patricia. Now i get strange behavior as you can see at http://pastehtml.com/view/aykmhy9ae.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这会增加 5 秒(5000 毫秒)的延迟...
编辑:
在此处了解更多信息。
This adds a 5 second (5000 ms) delay...
EDIT:
Read more here.
我可以建议逐渐淡出比延迟更好吗?在这种情况下,请尝试使用以下代码代替
visibility: "hidden"
代码小提示:当您使用 jQuery 显示/隐藏元素时,您只需使用以下内容:
设置
display
和visibility
CSS 属性是徒劳的。Can I suggest that a gradual fade out is better than a delay. In which case, try the following instead of your
visibility: "hidden"
codeA small tip: When you're displaying/hiding elements using jQuery, you only need use the following:
Setting
display
andvisibility
CSS properties is futile.你想要这样的东西:
其中 1000 是你想要鼠标移开操作延迟的毫秒数
you'd want something like this:
where 1000 is the number of milliseconds you want to have the delay of the mouseout action
您只需要使用 settimeout 来延迟函数的触发,并确保如果它们悬停则超时被清除。这将产生 1000 毫秒的超时。
You just need to use settimeout to delay the function from firing, and also make sure that if they hover away the timeout is cleared. This will do a 1000ms timeout.