为什么当鼠标离开时 jQuery 会在元素上触发鼠标悬停?
示例页面: http://workshop.grdev.co.nz/mousetime
有五个 div,其中应该响应鼠标悬停。
当展开动画开始时,开关 (hoverDemo.doAnim) 设置为 false。这是为了防止在放置鼠标时重复触发动画,以便动画在展开两个相邻 div 之间交替。有趣,但不是我们想要的。
hoverDemo = {
doAnim: true,
animDelay: 250,
marginBottom: 20,
marginTop: 50, // provides space for h2
heightContracted: 100,
init: function() {
$('.outer .inner').each( hoverDemo.contractDiv );
$('.outer .inner').mouseover( hoverDemo.expandDiv );
},
expandDiv: function() {
$('#debug-last').html(this.id);
if ( hoverDemo.doAnim ) {
hoverDemo.doAnim = false ;
$('#debug-ignore').html('IGNORING MOUSE');
$('.outer .inner').removeClass('expanded');
$(this).addClass('expanded');
var those = $('.outer .inner').not('.expanded');
$(this).animate( { height: $(this).find('p').height()+hoverDemo.marginBottom+hoverDemo.marginTop }, hoverDemo.animDelay, function() {
hoverDemo.doAnim = true ;
$('#debug-ignore').html('');
those.each( hoverDemo.contractDiv );
} );
}
},
contractDiv: function() {
$(this).animate( { height: hoverDemo.heightContracted }, hoverDemo.animDelay );
}
} ;
$(document).ready(hoverDemo.init);
当鼠标从 Lorem 移动到 Praesent 时,当鼠标离开 Lorem div.inner 时,JS 函数hoverDemo.expandDiv 被触发......我不确定为什么会发生这种情况。
您可以通过将鼠标移动到 Lorem 中,等待 IGNORING MOUSE 消息消失,然后慢慢将其移出展开的 Lorem div 来观察这一情况。
如果将鼠标移动到两侧,它不会触发相同的功能 - 仅当向下移动时。
如果将鼠标从一个 div 向下移动到另一个 div,通常第二个 div 不会展开。根据鼠标速度和动画时间,您可能会看到所有其他 div 都被展开,等等。
问题:
当鼠标刚刚离开展开的 div 时,这段代码会导致 mouseover 事件触发,为什么它只在鼠标通过底部边缘离开时才会发生?
使当前悬停的块展开并同时防止反弹效果的最佳方法是什么?
演示使用 jQuery 1.2.6,因为这是该网站当前使用的 jQuery 版本。
在 OSX Chrome、Firefox、Safari 中观察到的行为。
Example page: http://workshop.grdev.co.nz/mousetime
There are five divs which should respond to mouseover.
A switch (hoverDemo.doAnim) is set to false when the expand animation starts. This is intended to prevent the animation firing repeatedly when the mouse is placed so that the animation alternates between expanding two adjacent divs. Entertaining, but not what we want.
hoverDemo = {
doAnim: true,
animDelay: 250,
marginBottom: 20,
marginTop: 50, // provides space for h2
heightContracted: 100,
init: function() {
$('.outer .inner').each( hoverDemo.contractDiv );
$('.outer .inner').mouseover( hoverDemo.expandDiv );
},
expandDiv: function() {
$('#debug-last').html(this.id);
if ( hoverDemo.doAnim ) {
hoverDemo.doAnim = false ;
$('#debug-ignore').html('IGNORING MOUSE');
$('.outer .inner').removeClass('expanded');
$(this).addClass('expanded');
var those = $('.outer .inner').not('.expanded');
$(this).animate( { height: $(this).find('p').height()+hoverDemo.marginBottom+hoverDemo.marginTop }, hoverDemo.animDelay, function() {
hoverDemo.doAnim = true ;
$('#debug-ignore').html('');
those.each( hoverDemo.contractDiv );
} );
}
},
contractDiv: function() {
$(this).animate( { height: hoverDemo.heightContracted }, hoverDemo.animDelay );
}
} ;
$(document).ready(hoverDemo.init);
When the mouse moves from Lorem to Praesent, the JS function hoverDemo.expandDiv is fired as the mouse leaves the Lorem div.inner ... and I'm unsure why this is happening.
You can observe this by moving the mouse into Lorem, waiting until the IGNORING MOUSE message goes away, then slowly moving it down out of the expanded Lorem div.
If you move the mouse to the sides, it doesn't fire the same function - only when moving down.
If you move your mouse down from one div to another, frequently the second div won't be expanded. Dependent on mouse speed and animation timing, you might see every other div get expanded, and so forth.
Questions:
What about this code causes the mouseover event to fire when the mouse is just leaving the expanded div, and why it only happens as the mouse leaves via the bottom edge?
What's the best way to make the currently hovered block expand, while preventing the bounce effects?
Demo uses jQuery 1.2.6, as this is the version of jQuery which is currently in use on the site.
Behaviour observed in OSX Chrome, Firefox, Safari.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来解决方案(或者,一个好的解决方案)是使用hoverIntent插件。
添加这个几乎使它的行为如我所希望的那样。它没有回答我的问题,但它确实解决了问题:)
插件: http ://cherne.net/brian/resources/jquery.hoverIntent.html
更新代码:http: //workshop.grdev.co.nz/mouseintent
Looks like the solution (or, a good solution) is to use the hoverIntent plugin.
Adding this pretty much made it behave as I hoped it would. It doesn't answer my question about why, but it does fix the problem :)
Plugin: http://cherne.net/brian/resources/jquery.hoverIntent.html
Updated code: http://workshop.grdev.co.nz/mouseintent