如何告诉 .hover() 等待?
我有一个下拉菜单。 现在,当它向下滑动到多个级别时,我希望它在消失之前添加大约 2 秒的等待时间,以便用户在中断 .hover()
时可以重新进入。因为失误。
是否可以?
我的幻灯片代码:
$('.icon').hover(function() {
$('li.icon > ul').slideDown('fast');
}, function() {
$('li.icon > ul').slideUp('fast');
});
I have a drop down menu. Now when it's slided down to multiple levels, I'd like it to add wait time for like 2 secs, before it disappears, so the user can get back in, when he breaks the .hover()
by mistake.
Is it possible?
my code for the slide:
$('.icon').hover(function() {
$('li.icon > ul').slideDown('fast');
}, function() {
$('li.icon > ul').slideUp('fast');
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这将使第二个函数在执行前等待 2 秒(2000 毫秒):
当用户将鼠标悬停回来时,它还会清除超时以避免疯狂的行为。
然而,这并不是一个非常优雅的方法。 您可能应该查看 hoverIntent 插件,该插件旨在解决这个特定问题。
This will make the second function wait 2 seconds (2000 milliseconds) before executing:
It also clears the timeout when the user hovers back in to avoid crazy behavior.
This is not a very elegant way of doing this, however. You should probably check out the hoverIntent plugin, which is designed to solve this particular problem.
我个人喜欢“hoverIntent”插件:
http://cherne.net/brian/resources/ jquery.hoverIntent.html
来自页面:hoverIntent 是一个尝试确定用户意图的插件...就像水晶球一样,只需移动鼠标即可! 它的工作原理类似于(并且源自)jQuery 的内置悬停。 但是,它不会立即调用 onMouseOver 函数,而是等到用户的鼠标速度足够慢后再进行调用。
为什么? 延迟或防止意外触发动画或 ajax 调用。 简单的超时适用于小区域,但如果您的目标区域很大,则无论意图如何,它都可能会执行。
配置选项
灵敏度:
如果鼠标在轮询间隔之间移动的像素数少于此数量,则将调用“over”函数。 当最小灵敏度阈值为 1 时,鼠标不得在轮询间隔之间移动。 灵敏度阈值越高,您就越有可能收到误报。 默认灵敏度:7
间隔:
hoverIntent 在读取/比较鼠标坐标之间等待的毫秒数。 当用户的鼠标第一次进入该元素时,其坐标被记录。 最快可以在单个轮询间隔之后调用“over”函数。 将轮询间隔设置得较高会增加第一次可能的“过度”调用之前的延迟,但也会增加到下一个比较点的时间。 默认间隔:100
超过:
必需的。 您想要调用 onMouseOver 的函数。 您的函数接收与 jQuery 的悬停方法相同的“this”和“event”对象。
超时:
在调用“out”函数之前的简单延迟(以毫秒为单位)。 如果用户在超时到期之前将鼠标移回到元素上,则不会调用“out”函数(也不会调用“over”函数)。 这主要是为了防止草率/人为的鼠标轨迹暂时(无意地)使用户离开目标元素......给他们时间返回。 默认超时:0
超时:
必需的。 您想要调用 onMouseOut 的函数。 您的函数接收与 jQuery 的悬停方法相同的“this”和“event”对象。 请注意,如果在同一运行中调用了“over”函数,hoverIntent 只会调用“out”函数。
personally I like the "hoverIntent" plugin:
http://cherne.net/brian/resources/jquery.hoverIntent.html
from the page: hoverIntent is a plug-in that attempts to determine the user's intent... like a crystal ball, only with mouse movement! It works like (and was derived from) jQuery's built-in hover. However, instead of immediately calling the onMouseOver function, it waits until the user's mouse slows down enough before making the call.
Why? To delay or prevent the accidental firing of animations or ajax calls. Simple timeouts work for small areas, but if your target area is large it may execute regardless of intent.
Configuration Options
sensitivity:
If the mouse travels fewer than this number of pixels between polling intervals, then the "over" function will be called. With the minimum sensitivity threshold of 1, the mouse must not move between polling intervals. With higher sensitivity thresholds you are more likely to receive a false positive. Default sensitivity: 7
interval:
The number of milliseconds hoverIntent waits between reading/comparing mouse coordinates. When the user's mouse first enters the element its coordinates are recorded. The soonest the "over" function can be called is after a single polling interval. Setting the polling interval higher will increase the delay before the first possible "over" call, but also increases the time to the next point of comparison. Default interval: 100
over:
Required. The function you'd like to call onMouseOver. Your function receives the same "this" and "event" objects as it would from jQuery's hover method.
timeout:
A simple delay, in milliseconds, before the "out" function is called. If the user mouses back over the element before the timeout has expired the "out" function will not be called (nor will the "over" function be called). This is primarily to protect against sloppy/human mousing trajectories that temporarily (and unintentionally) take the user off of the target element... giving them time to return. Default timeout: 0
out:
Required. The function you'd like to call onMouseOut. Your function receives the same "this" and "event" objects as it would from jQuery's hover method. Note, hoverIntent will only call the "out" function if the "over" function has been called on that same run.
一般的想法是使用
setTimeout
,如下所示:但是,如果用户将鼠标移出然后快速再次移入,这可能会造成违反直觉的事情 - 这并不能说明当用户将鼠标悬停在上方时清除超时再来一次。 这将需要额外的状态。
The general idea is to use
setTimeout
, like so:But this may do counterintuitive things if the user mouses out and then mouses in again quickly—this doesn't account for clearing the timeout when the user hovers over it again. That would require additional state.
以下命令将停止滑动 2 秒:
The following will stop the sliding from triggering by 2 seconds:
或者你可以简单地使用
过渡:所有 2 秒缓入缓出。
确保为不同的浏览器添加 -webkit、-moz 和 -o。
or you could simply use
transition:all 2s ease-in-out.
make sure that you add -webkit, -moz and -o for different browsers.
我认为这是您需要的代码:
I think this is code your need:
//编辑:插入代码
//edit: instert code
我想向 Paolo Bergantino 补充一点,您可以在没有数据属性的情况下执行此操作:
I would like to add to Paolo Bergantino that you can do this without the data attribut: