使用 jQuery 连接效果
我有 4 张图像(logo-1、logo-2、logo-3 和 logo-4),我想在单击按钮时依次显示它们。
首先,我将图像的高度设置为 0,以使用 escapeOutBounce 效果显示它们。
我正在使用动画功能,但四个图像同时显示。
如何连接动画?
<script>
function mostrar(num_logo) {
if (num_logo <= 4) {
$("#logo-"+num_logo)
.stop().animate(
{height: '218px'},
{queue:false,
duration:1000,
easing: 'easeOutBounce',
complete: mostrar(num_logo+1)}
);
}
}
$(function() {
// run the currently selected effect
function runEffect() {
for (i=1;i<=4;i++) {
$("#logo-"+i).css('height', '0px');
}
mostrar(1);
};
$( "#button" ).click(function() {
runEffect();
return false;
});
});
</script>
I have 4 images (logo-1, logo-2, logo-3 and logo-4) and I want to show them one after the other when I click on a button.
First, I set the height of images to 0 to show them with easeOutBounce effect.
I'm using animate function, but the four images are shown at the same time.
How can I concatenate the animations?
<script>
function mostrar(num_logo) {
if (num_logo <= 4) {
$("#logo-"+num_logo)
.stop().animate(
{height: '218px'},
{queue:false,
duration:1000,
easing: 'easeOutBounce',
complete: mostrar(num_logo+1)}
);
}
}
$(function() {
// run the currently selected effect
function runEffect() {
for (i=1;i<=4;i++) {
$("#logo-"+i).css('height', '0px');
}
mostrar(1);
};
$( "#button" ).click(function() {
runEffect();
return false;
});
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是我为回答一个非常类似的问题而制作的jsfiddle: http://jsfiddle.net/jJ8vJ/ (问题可以在这里找到:如何按顺序设置动画位置?)。
基本思想是设置一个 jQuery 对象数组(要按顺序设置动画的元素),并使用 .animate() 函数的回调按顺序为元素设置动画
Here is a jsfiddle I made to answer a very similar question: http://jsfiddle.net/jJ8vJ/ (The question can be found here: how to animate position in Order?).
The basic idea is to set an array of jQuery objects (elements you want to animate in order) and use the callback of the
.animate()
function to animate the elements in order贾斯珀的回答很好。我给你一些你应该阅读的链接:它们解释了你在代码中犯的一些常见错误以及如何避免它们;)
The answer from Jasper is good. I give you some links you should read: they explain some common mistakes you've made in your code and how to avoid them ;)