如果在 jQuery 中动画停止,如何防止回调?
我正在开发一个包含很多可悬停 div 的项目。我写了一个动画来做到这一点:
鼠标悬停时,图像淡出,作为回调,包含信息的 div 淡入。然后鼠标移出时,信息 div 淡出,作为回调,图像淡入。我正在使用hoverIntent插件。
我的问题是,如果你在 div 上停留足够长的时间,一切都会正常。但是如果你在上面停留 300 毫秒,动画就会停止,但显示信息 div 的回调仍然会被调用,忽略鼠标移出事件。这会导致 info div 永久保留...
这是我的代码:
var infoPaneSettings = {
sensitivity: 3,
interval: 200,
timeout: 500,
over: showPane,
out: hidePane
}
function showPane() {
var entry = $(this);
$('.featured_img', entry).fadeTo(1000, 0,
function(){
$('.infoPane', entry).fadeTo(200, 1);
});
}
function hidePane() {
var entry = $(this);
$('.infoPane', entry).fadeTo(300, 0,
function(){
$('.featured_img', entry).fadeTo(200, 1);
});
}
$('.entry').each(function(){
$(this).hoverIntent(infoPaneSettings);
});
我希望你能帮助我!
I am working on a project containing a lot of hoverable divs. I wrote an animation that does this :
On mouseover, an image fades out, and as a callback, a div containing information fades in. then on mouse out, the info div fades out, and as a callback the image fades back in. I'm using the hoverIntent plugin.
My problem is that if you stay long enough over the div, everything works fine. But if you stay say 300ms on it , the animation stops, but the callback showing the info div is still called, ignoring the mouse out event. That results in having the info div staying permanently...
Here is my code :
var infoPaneSettings = {
sensitivity: 3,
interval: 200,
timeout: 500,
over: showPane,
out: hidePane
}
function showPane() {
var entry = $(this);
$('.featured_img', entry).fadeTo(1000, 0,
function(){
$('.infoPane', entry).fadeTo(200, 1);
});
}
function hidePane() {
var entry = $(this);
$('.infoPane', entry).fadeTo(300, 0,
function(){
$('.featured_img', entry).fadeTo(200, 1);
});
}
$('.entry').each(function(){
$(this).hoverIntent(infoPaneSettings);
});
I hope you can help me !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在开始新动画之前,在元素上使用 http://api.jquery.com/stop/ 。它将阻止运行回调。
Use http://api.jquery.com/stop/ on the element before starting the new animation. It will prevent running the callback.