jQuery 动画故障:当鼠标快速移动时,函数会以错误的顺序触发
注意:我没有写有问题的脚本,是我的同事写的。只是自愿为我们提供一些意见。请原谅听起来像菜鸟的错误术语或描述;我不是脚本专家,我是 HTML/CSS 专家。
有问题的页面:
http://cure.org/curekids/kenya/2010/02/joseph_muchiri/
问题截图:
< img src="https://i.sstatic.net/ipsX5.jpg" alt="ck-dash-tab.jpg">
问题:
当您查看该页面时,您会在页面顶部看到一个类似破折号的工具栏,但就在网站标题下方(上面写着“CUREkids”)。
当您将鼠标悬停在该工具栏的任何区域上时,左侧会出现一个绿色的小选项卡,该选项卡从其后面以动画方式显示(其上带有问号的部分)。单击时,该选项卡会打开一个描述性的 Slidedeck 界面。到目前为止一切都很好。
问题是,如果您将鼠标移过工具栏太快,就会出现一个螺旋故障,导致 jQuery 规则以一种奇怪的方式触发,导致选项卡从后面出来,但又回到工具栏的上方。
其他详细信息:
脚本的工作方式是,默认情况下选项卡隐藏在工具栏后面,jQuery 首先将其向左动画以远离工具栏,然后更改 z-index 以将其带入工具栏。它实际上位于工具栏元素的顶部,以实现最大的可用性和选项卡上的单击区域。所有这一切都发生在 mouseOver 上。在 mouseOut 上,会发生相反的情况(z-index 更改为低于工具栏,然后以动画方式返回到它所在的位置)。
我的想法
我认为脚本的编写方式一定存在问题,可以进行调整,以便当 mouseOver 事件发生得如此快时,不会导致重叠错误。
感谢所有帮助。
NOTE: I didn't write the script in question, my collegue did. Just volunteering to get some input on this for us. Please pardon incorrect terms or descriptions that sound like a noob; I'm not a scripting guy, I'm an HTML/CSS guy.
The page in question:
http://cure.org/curekids/kenya/2010/02/joseph_muchiri/
A screenshot of the issue:
The issue:
When you look at the page, you'll see a toolbar sort of dash at the top of the page, but just below the site header (its the piece that says "CUREkids").
When you hover over any area of that toolbar there is a small green tab on the left that animates out from behind it (its the piece with the question mark on it). When clicked, the tab toggles open a descriptive Slidedeck interface. So far all is well.
The problem is that if you mouse across the toolbar too quickly, there is a screwy glitch that causes the jQuery rules to fire in a strange way that results in the tab coming out from behind, but going back in overtop of the toolbar.
Additional details:
The way the script works is that the tab is hidden behind the toolbar by default, and the jQuery first animates it left to come out away from the toolbar, then changes the z-index to bring it actually over top of the toolbar element for maximum usability and clicking area on the tab. All this happens on mouseOver. On mouseOut the reverse occurs (z-index changed to lower than the toolbar, and then animates right back to where it was located).
My thoughts
I think there must be an issue with how the script is written that possibly could be tweaked so that when the mouseOver event happens so quickly it doesn't result in the overlap bug.
All help is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在为 mouseleave 定义的函数中,添加
.stop(true, true)
,然后将 z-index 更改回 -1。$("#what-tag").stop(true, true).css("z-index", "-1");
查看 http://api.jquery.com/stop/
第一个
true
传递给.stop(true, true)
将删除所有排队的动画。第二个true
是这种情况下的关键 - 它基本上跳转到您为 mouseenter 定义的函数的末尾并立即触发其回调。在这种情况下,通过使用.stop(true, true)
,您可以确保 z-index 在设置为 -1 之前始终设置为 1。我认为当前发生的情况是在 mouseenter 回调函数触发之前将 z-index 设置为 -1。
编辑:
不相关 - 您还应该考虑将 jquery 选择器缓存到变量中。您在此悬停方法中调用
$("#what-tag")
六次。如果您在悬停方法上方定义一个变量,如下所示:var $whatTag = $("#what-tag")
并替换悬停方法内的引用,它将运行得更快。On the function you defined for mouseleave, add
.stop(true, true)
before you change the z-index back to -1.$("#what-tag").stop(true, true).css("z-index", "-1");
Check out http://api.jquery.com/stop/
The first
true
passed to.stop(true, true)
will remove all queued animations. The secondtrue
is the key in this situation - it basically jumps to the end of the function you defined for mouseenter and fires its callback immediately. In this case, by using.stop(true, true)
, you ensure that the z-index will always be set to 1 before setting it to -1.I think what is currently happening is that it's setting the z-index to -1 before the mouseenter callback function fires.
Edit:
Unrelated - you should also look into caching your jquery selectors into variables. You are calling
$("#what-tag")
six times in this hover method. If you define a variable above the hover method like this:var $whatTag = $("#what-tag")
and replace the references inside the hover method, it will work faster.据我所知,解决此问题并使用各种选项(例如以毫秒为单位的间隔、超时、灵敏度等)控制行为的最佳方法是 jQuery 插件, hoverIntent。
仅默认用法就可以修复鼠标触发/排队动画的快速移动。
如果您只想停止排队,请查看这些文章/帖子:
http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup
http://css-tricks.com/full-jquery-animations/
The best way I know of to fix this, and to control the behaviour with various options (like the interval in ms, timeout, sensitivity etc.) is the jQuery plugin, hoverIntent.
Just the default usage alone fixes the quick movement of the mouse triggering / queueing animations.
If you want only to stop the queueing, then have a look at these articles / posts:
http://www.learningjquery.com/2009/01/quick-tip-prevent-animation-queue-buildup
http://css-tricks.com/full-jquery-animations/
发生的情况是,当 mouseout 事件被触发时,在 mouseover 上启动的 jquery animate 函数尚未完成...因此 z-index 更改之前的 100 毫秒延迟应该发生在动画的“显示”部分结束在鼠标移开“隐藏”功能上更改 z-index 后发生。 (希望这是有道理的...)
所以尝试这个...更改:
为:
重要的一点是
$("#what-tag").clearQueue();
What's happening is that the jquery animate function that is started on mouseover isn't finished when the mouseout event is fired... thus the 100ms delay before the z-index change is suppose to happen on the "show" part of the animation ends up happening after the z-index is changed on the mouseout "hide" function. (Hope that makes sense...)
So try this... change:
to:
the important bit being the
$("#what-tag").clearQueue();
在标记中,将锚元素
#what-tag
移到 div 元素#curekids-dash
之外,例如,移到其前面: curekids.css,第 24 行,对于选择器
#curekids-dash
,添加属性z-index: 2;
,使整个规则如下: FF 3.6,如果它不能修复 IE、Webkit 和早期 FF 版本中的问题,我会感到惊讶。
In your mark-up, move the anchor-element
#what-tag
outside of your div-element#curekids-dash
, for example, right in front of it:In curekids.css, line 24, for the selector
#curekids-dash
, add the propertyz-index: 2;
, making the whole rule this:That fixed the issue in FF 3.6, and I'd be surprised if it doesn't fix it in IE, Webkit, and earlier FF-versions as well.