如何同时淡入和动画?

发布于 2024-08-09 01:24:26 字数 347 浏览 3 评论 0原文

我使用 jQuery 创建一个基本的“工具提示”动画,以便工具提示将出现在一个小动画中,在该动画中它会淡入视图并垂直移动。

到目前为止,我有这样的:

$('.tooltip').fadeIn('slow');
$('.tooltip').animate({ top: "-10px" }, 'slow');

这样做或这样:

$('.tooltip').fadeIn('slow').animate({ top: "-10px" }, 'slow');

动画将一次运行一个,首先淡入,然后是垂直动画。有没有办法让它同时做这两件事?

Using jQuery I'm creating a basic 'tooltip' animation so that the tooltip will appear in a little animation in which it fades into view as well as move vertically.

So far I have this:

$('.tooltip').fadeIn('slow');
$('.tooltip').animate({ top: "-10px" }, 'slow');

Doing it that way or this way:

$('.tooltip').fadeIn('slow').animate({ top: "-10px" }, 'slow');

The animations will run one at a time, the fade in first and then the vertical animation. Is there a way to have it do both at the same time?

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

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

发布评论

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

评论(3

似最初 2024-08-16 01:24:26

对于几年后仍在寻找的人来说,情况发生了一些变化。您现在也可以将 queue 用于 .fadeIn(),这样它的工作方式如下:

$('.tooltip').fadeIn({queue: false, duration: 'slow'});
$('.tooltip').animate({ top: "-10px" }, 'slow');

这具有在 display: none 元素,这样您就不需要额外的两行代码。

For people still looking a couple of years later, things have changed a bit. You can now use the queue for .fadeIn() as well so that it will work like this:

$('.tooltip').fadeIn({queue: false, duration: 'slow'});
$('.tooltip').animate({ top: "-10px" }, 'slow');

This has the benefit of working on display: none elements so you don't need the extra two lines of code.

蓝色星空 2024-08-16 01:24:26

如果您想单独调用它们(例如,从不同的代码),则执行同步动画的另一种方法是使用队列。再次,正如 Tinister 的答案一样,您必须为此使用 animate 而不是 fadeIn:

$('.tooltip').css('opacity', 0);
$('.tooltip').show();
...

$('.tooltip').animate({opacity: 1}, {queue: false, duration: 'slow'});
$('.tooltip').animate({ top: "-10px" }, 'slow');

Another way to do simultaneous animations if you want to call them separately (eg. from different code) is to use queue. Again, as with Tinister's answer you would have to use animate for this and not fadeIn:

$('.tooltip').css('opacity', 0);
$('.tooltip').show();
...

$('.tooltip').animate({opacity: 1}, {queue: false, duration: 'slow'});
$('.tooltip').animate({ top: "-10px" }, 'slow');
痴者 2024-08-16 01:24:26
$('.tooltip').animate({ opacity: 1, top: "-10px" }, 'slow');

但是,这似乎不适用于 display: none 元素(如 fadeIn 那样)。所以,你可能需要提前把这个:

$('.tooltip').css('display', 'block');
$('.tooltip').animate({ opacity: 0 }, 0);
$('.tooltip').animate({ opacity: 1, top: "-10px" }, 'slow');

However, this doesn't appear to work on display: none elements (as fadeIn does). So, you might need to put this beforehand:

$('.tooltip').css('display', 'block');
$('.tooltip').animate({ opacity: 0 }, 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文