admin管理员组文章数量:1392054
I currently have several elements in a row that have a mouseover event that fires some animation. My problem is that if someone mouses over several of the elements in quick succession the animation gets a little frantic.
I'm curious if there is a way to have a mouseover event that only fires if the mouse is over an element for a certain amount of time (say 250 milliseconds). Can this be done with jQuery?
I currently have several elements in a row that have a mouseover event that fires some animation. My problem is that if someone mouses over several of the elements in quick succession the animation gets a little frantic.
I'm curious if there is a way to have a mouseover event that only fires if the mouse is over an element for a certain amount of time (say 250 milliseconds). Can this be done with jQuery?
Share Improve this question asked Aug 17, 2011 at 21:49 Abe MiesslerAbe Miessler 85.2k104 gold badges321 silver badges495 bronze badges 3-
stop()
can be an alternative and you're not constrained to download any plug-ins, have a look at it (api.jquery./stop) – vdegenne Commented Aug 17, 2011 at 21:56 - How would I use stop to do this? – Abe Miessler Commented Aug 17, 2011 at 22:03
- edit your post and stamp the code that handle elements' event, I'll see if you can use this in your code and will post an example as an answer to your trouble. – vdegenne Commented Aug 17, 2011 at 22:09
4 Answers
Reset to default 5I would suggest you use setTimeout for this:
(function ($) {
var t;
$('ul li').hover(function() {
var that = this;
window.clearTimeout(t);
t = window.setTimeout(function () {
$(that).animate({opacity: .5}, 'slow').animate({opacity: 1});
}, 250);
});
}(jQuery));
If there are multiple items activated in rapid succession the timeout will override the timeout-id thus preventing the first item that should not start from animating.
It does not require any arcane plugin (although hoverIntent may provide some nice additional features you may want to use) and window.setTimeout is supported everywhere.
UPDATE
I updated the code sample to work.. was writing this from memory yesterday and didn't get the setTimeout call quite right.. Also see this jsFiddle for reference.
The issue I see with this is that it will execute the hover animation even if you leave the . So you could also add a $('ul').mouseleave(function() { window.clearTimeout(t) });
to prevent that.
greetings Daniel
I suggest that you check out the jQuery HoverIntent plugin ( 1.4k minified ). Here's the link: http://cherne/brian/resources/jquery.hoverIntent.html. It's a great plugin, I've used it many times!
Here's a small sampling of code:
var config = {
over: makeTall, // function = onMouseOver callback (REQUIRED)
timeout: 500, // number = milliseconds delay before onMouseOut
out: makeShort // function = onMouseOut callback (REQUIRED)
};
$("#demo3 li").hoverIntent( config )
yes:
to acplish this put a setTimeout
in your onMouseover function and a clearTimeout
on mouseout
You may need a little more logic, but that's the nuts and bolts of it
here's an example of stop()
in action, hope that will help:
without stop()
:
http://jsfiddle/5djzM/
with stop()
cleaning the queue of animations:
http://jsfiddle/KjybD/
本文标签: javascriptPossible to have quotmouseover for X secondsquot functionality using jQueryStack Overflow
版权声明:本文标题:javascript - Possible to have "mouseover for X seconds" functionality using jQuery? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744782508a2624797.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论