2015-02-07 19 views
2

我不能为了我的生活找出为什么我运行的这个函数没有隐藏元素。当将鼠标悬停在列表项上时,我想让它内部的两个div动画为49%高度,当鼠标离开此列表项时,它们将返回0并再次获得display: none;。但是,即使animate的回调函数中的语句执行,它们仍然保持在display: block;动画完成后未隐藏的元素

这里是什么样子时,他们的动画到49%,如:

animation complete image

而这里的时候,他们回去0

image of the error that occurrs

包含两个div元素出于某种原因,回调中的图像不会隐藏回调函数.hide()

这是行不通的功能:

$('#site-wrapper').on('mouseenter', '#images-list li', function() { 

    $(this).children('div').show().stop().animate({height: '49%'}, 'fast'); 
}).on('mouseleave', '#images-list li', function() { 

    $(this).children('div').stop().animate({height: 0}, 'fast', function() { 
     $(this).children('div').hide(); 
    }); 
}); 

此解决方案的作品,但它隐藏它马上在用户不能够看到动画,这是我不想:

$('#site-wrapper').on('mouseenter', '#images-list li', function() { 

    $(this).children('div').show().stop().animate({height: '49%'}, 'fast'); 
}).on('mouseleave', '#images-list li', function() { 

    $(this).children('div').stop().animate({height: 0}, 'fast').hide(); 
}); 

我应该怎么做才能解决这个相当愚蠢的错误?

+0

任何地方,我们可以看到在行动呢? – darshanags 2015-02-07 17:09:21

+0

@darshanags不幸的是,似乎无法让小提琴开始工作。 – Chrillewoodz 2015-02-07 17:16:52

回答

2

从文档上.animate()(重点煤矿)

功能齐全

如果提供完整的回调函数一旦 动画完成,就会启动。这对于按顺序串联不同的 动画非常有用。该回调没有发送任何 自变量,但this被设置为正在动画的DOM元素

所以不是

... 
.on('mouseleave', '#images-list li', function() { 
    $(this).children('div').stop().animate({height: 0}, 'fast', function() { 
     $(this).children('div').hide(); // Wrong line 
    }); 
}); 

应该

... 
.on('mouseleave', '#images-list li', function() { 
    $(this).children('div').stop().animate({height: 0}, 'fast', function() { 
     $(this).hide(); // Hide the same div that was animated 
    }); 
}); 
+0

有道理,现在就像魅力一样。非常感谢 :) – Chrillewoodz 2015-02-07 17:19:17

0

你能试试吗?这将是更好地使用小提琴证明这一点:

$('#site-wrapper').on('mouseenter', '#images-list li', function() { 
    $this = $(this); 
    $this.children('div').show().stop().animate({height: '49%'}, 'fast'); 
}).on('mouseleave', '#images-list li', function() { 
    $this.children('div').stop().animate({height: 0}, 'fast', function() { 
     $this.children('div').hide(); 
    }); 
}); 
+1

这是一个“isch”解决方案,如果我在动画完成之前离开列表项目并且不进入另一个列表项目,它将起作用。但是如果我快速输入另一个列表项目,它不会在新列表项目中生成动画,并且之前列表项目的div仍然可见。 – Chrillewoodz 2015-02-07 17:11:16