为什么我的动画在恢复后变得非常慢?

发布于 2024-07-30 05:02:20 字数 749 浏览 3 评论 0原文

我有一个 jQuery 动画,它可以对容器上的“scrollLeft”进行动画处理,以产生一种“选取框”效果。

我已将其设置为当鼠标悬停在容器上时它会停止动画,而当鼠标离开时它会恢复。

$(banksContainer).mouseover(function() {
    $(banksContainer).stop(false);
});

$(banksContainer).mouseleave(function() {
    startAnimation();
});

每当我将鼠标移到动画上然后移开动画时,它都会以极慢的速度恢复,但在一分钟左右后逐渐加快。

为什么会出现这种情况以及可以解决吗?

附言。 这是所要求的 startAnimation 函数:

// recursive function - represents one cycle of the marquee
function startAnimation() {
    $(banksContainer).animate(
        { scrollLeft: scrollLeftEnd },
        35000,
        "linear",
        function() {
            $(this)[0].scrollLeft = scrollLeftHome;
            startAnimation();
        }
    );
}

I have a jQuery animation that animates the 'scrollLeft' on a container to produce a kind of 'marquee' effect.

I have it set up so on mouse-over of the container it stops the animation, and on mouse-leave, it resumes.

$(banksContainer).mouseover(function() {
    $(banksContainer).stop(false);
});

$(banksContainer).mouseleave(function() {
    startAnimation();
});

Whenever I move the mouse over the animation then off the animation, it resumes at an extremely slow speed, but gradually picks up after a minute or so.

Why is this happening and can it be resolved?

PS. Here's the startAnimation function as requested:

// recursive function - represents one cycle of the marquee
function startAnimation() {
    $(banksContainer).animate(
        { scrollLeft: scrollLeftEnd },
        35000,
        "linear",
        function() {
            $(this)[0].scrollLeft = scrollLeftHome;
            startAnimation();
        }
    );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

哆兒滾 2024-08-06 05:02:20

这可能是因为当您恢复动画时,移动距离已缩短,但时间仍为 35 秒。 因为速度=距离/时间,所以速度会很慢。

我认为你应该根据剩余距离设置时间。 即 35000 * 剩余距离 / 总距离。

像这样的东西吗?

function startAnimation() {
    $(banksContainer).animate(
        { scrollLeft: scrollLeftEnd },
        35000 * this.scrollLeft() / scrollLeftEnd, //or scrollLeftHome whichever is non-zero
        "linear",
        function() {
            $(this)[0].scrollLeft = scrollLeftHome;
            startAnimation();
        }
    );
}

That's probably because when you resume the animation, the distance to cover has reduced but the time remains at 35 seconds. since speed = distance / time, it would be slow.

I think you should set the time proportionate to the distance remaining. That would be 35000 * distance remaining / total distance.

Something like this?

function startAnimation() {
    $(banksContainer).animate(
        { scrollLeft: scrollLeftEnd },
        35000 * this.scrollLeft() / scrollLeftEnd, //or scrollLeftHome whichever is non-zero
        "linear",
        function() {
            $(this)[0].scrollLeft = scrollLeftHome;
            startAnimation();
        }
    );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文