具有移动效果的脚本回调不规则性
我想在鼠标悬停时将菜单项弹出 5px,并使用 Scriptaculous 将其返回到原始位置。我使用 afterFinish 回调来确保凹凸移动效果在凹凸移动效果运行之前完成。
问题是,如果您多次快速将鼠标悬停和鼠标移出,该项目不会返回到其原始位置。鼠标移出越多,它就越关闭。我认为这是 afterFinish 回调应该防止的。它似乎可以与 Morph 效果一起正常工作,并且应该与所有 Scriptaculous 效果一起工作,如下所述:
Javascript - Scriptaculous - 效果回调函数
我尝试过使用效果队列,得到相同的结果,并花了几个小时搜索各种论坛来帮助我理解这一点。
这是正常工作的变形示例:
<SCRIPT>
function morphMe(obj) {
new Effect.Morph($(obj), {
style: {
height: '200px',
width: '200px'},
afterFinish: function(){ new Effect.Morph($(obj), {
style: {
height: '20px',
width: '200px'}});
}
})
}
</SCRIPT>
<div id="bumptest" class="leftnav" style="position: absolute; left: 100px; width: 200px; border: 1px solid green" onmouseover="morphme(this);">Morph Me</div>
这是移动示例,在快速鼠标悬停和移出时无法按预期工作。也许我需要使用 setTimeout 但我不明白为什么这是必要的。
<SCRIPT>
function OnFinish(obj){
new Effect.Move(obj.element.id, {x: -5, y: 0, duration: .4});
}
function bumpOut(myObject){
new Effect.Move(myObject,
{ x: 5,
y: 0,
duration: 0.4,
afterFinish: OnFinish,
});
}
</SCRIPT>
<div id="bumptest" class="leftnav" style="position: absolute; left: 100px; width: 200px; border: 1px solid green" onmouseover="bumpOut(this);">Bump me right and then back</div>
任何帮助,甚至可能是使用 setTimeout 的 mod 或指向已经执行此操作的可靠脚本的指针,非常感谢。
谢谢,
摩托车流浪汉
I would like to bump a menu item out 5px on mouseover and have it return to its original position using Scriptaculous. I am using the afterFinish callback to ensure that the bump-out Move effect is completed before the bump-back-in Move effect runs.
The problem is that the item doesn't return to its original position if you quickly mouseover and mouseout more than once. The more you mouseover-and-out, the more it's off. I thought this is what the afterFinish callback was supposed to prevent. It seems to work correctly with the Morph effect, and it's supposed to work with all Scriptaculous effects, as stated below:
Javascript - Scriptaculous - Effect Callback function
I've tried using effects queues, same result, and spent several hours scouring various forums to help me understand this.
Here is the Morph example that works correctly:
<SCRIPT>
function morphMe(obj) {
new Effect.Morph($(obj), {
style: {
height: '200px',
width: '200px'},
afterFinish: function(){ new Effect.Morph($(obj), {
style: {
height: '20px',
width: '200px'}});
}
})
}
</SCRIPT>
<div id="bumptest" class="leftnav" style="position: absolute; left: 100px; width: 200px; border: 1px solid green" onmouseover="morphme(this);">Morph Me</div>
Here's the Move example that doesn't work as expected on quick mouseover and outs. Perhaps I need use setTimeout but I don't understand why this should be necessary.
<SCRIPT>
function OnFinish(obj){
new Effect.Move(obj.element.id, {x: -5, y: 0, duration: .4});
}
function bumpOut(myObject){
new Effect.Move(myObject,
{ x: 5,
y: 0,
duration: 0.4,
afterFinish: OnFinish,
});
}
</SCRIPT>
<div id="bumptest" class="leftnav" style="position: absolute; left: 100px; width: 200px; border: 1px solid green" onmouseover="bumpOut(this);">Bump me right and then back</div>
Any help, maybe even a mod using setTimeout or a pointer to a solid script that already does this, greatly appreciated.
Thanks,
motorhobo
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要并行思考:当多个效果同时运行时就会出现问题。实际上,这是因为 scriptacoulous 处理效果的方式:它在效果创建时计算目标位置。
以下场景不考虑 afterFinish:
(同样的推理也适用于 highlight():如果您多次突出显示背景颜色不会立即恢复为原始颜色也适用于:缩放、BlindDown/Up 以及其他一些。)
解决方案其方法是在每次调用时给出绝对值:
队列是解决方案的另一部分。当调用
bumpOut
时,请确保取消任何 OnFinish-Effect - 否则框的位置将会闪烁。可能需要进行更多有效性检查,以确保:第三个障碍:当
bumptest
的子元素(甚至是!)悬停时也会触发 mouseover,其频率比您通常预期的要高得多。如果您只想获取一次进入/离开
bumptest
的事件,请使用mouseenter / mouseleave。You'll need to think parallel: the problem occurs when several effects are running at the same time. Actually, it's because of the way that scriptacoulous handles effects: it calculates the target position at creation time of the effect.
The following scenario doesn't take afterFinish into account:
(The same reasoning applies to highlight(): If you highlight several times at once, the background color doesn't revert to the original one. Also applies to: Scale, BlindDown/Up, and some others.)
The solution to this is to give absolute values at every call:
Queues are another part of the solution. When
bumpOut
is called, make sure that any OnFinish-Effect is cancelled - otherwise the position of the box will flicker. More validity checks may be necessary to make sure that:A third roadblock: mouseover is triggered also when a child element of
bumptest
(even a<b>
!) is hovered, so much more often than you'd normally expect. Use mouseenter / mouseleave if you want to get the event of entering / leavingbumptest
only once.