具有移动效果的脚本回调不规则性

发布于 2024-10-01 18:05:24 字数 1538 浏览 9 评论 0原文

我想在鼠标悬停时将菜单项弹出 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

就此别过 2024-10-08 18:05:24

您需要并行思考:当多个效果同时运行时就会出现问题。实际上,这是因为 scriptacoulous 处理效果的方式:它在效果创建时计算目标位置。

以下场景不考虑 afterFinish:

  • 0.0s: 第一个新效果:“我当前的位置是 (100|10),我需要向右移动 6px,所以我的目标位置是 (106 |10) 在 0.4 秒内。”
  • 0.2s:第二个新效果:“我当前的位置是(103|10),我需要向右移动6px,所以我的目标位置是(109|10)。” <- 哎呀,偏移!
  • 0.2 - 0.4s:两种效果都会改变左侧值(可能会导致一些闪烁)
  • 0.4 - 0.6s: > 只剩下第二个效果,从 (106|10) 移动到 (109|10)。
  • 0.6s: Div 位于 (109|10)。

(同样的推理也适用于 highlight():如果您多次突出显示背景颜色不会立即恢复为原始颜色也适用于:缩放、BlindDown/Up 以及其他一些。)

解决方案其方法是在每次调用时给出绝对值

$(this).morph({left:106px}); // etc.

队列是解决方案的另一部分。当调用 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:

  • 0.0s: 1st new Effect: "My current position is (100|10), I need to move 6px right, so my target position is (106|10) in 0.4 seconds."
  • 0.2s: 2nd new Effect: "My current position is (103|10), I need to move 6px right, so my target position is (109|10)." <- Oops, offset!
  • 0.2 - 0.4s: both effects will change the left value (may cause some flickering)
  • 0.4 - 0.6s: Only the second effect is left, moves from (106|10) to (109|10).
  • 0.6s: Div is at (109|10).

(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:

$(this).morph({left:106px}); // etc.

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:

  • Only one bump-effect is executed on this box at a time
  • This is the right one ("recent activity cancels out more older ones" or "an animation that has started has to finish first.").

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 / leaving bumptest only once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文