首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jQuery连接特效

使用jQuery连接特效
EN

Stack Overflow用户
提问于 2011-11-30 04:20:16
回答 2查看 2.7K关注 0票数 2

我有4个图像(logo-1,logo-2,logo-3和logo-4),当我点击一个按钮时,我想一个接一个地显示它们。

首先,我将图像的高度设置为0,以使用easeOutBounce效果显示它们。

我使用的是动画功能,但同时显示了四个图像。

如何连接动画?

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-30 04:26:29

下面是我制作的一个jsfiddle,它回答了一个非常类似的问题:http://jsfiddle.net/jJ8vJ/ (这个问题可以在这里找到:how to animate position in Order?)。

基本思想是设置jQuery对象(要按顺序设置动画的元素)的数组,并使用.animate()函数的回调按顺序设置元素的动画

代码语言:javascript
复制
var $items     = [$('#one'), $('#two'), $('#three'), $('#four')],//stores the DOM elements to be animated in order
    item_index = 0,//stores the current index in the animation queue
    animations = {
        '0px0px'     : { left : '100px', top : '0px' },//0px0px means the element is at the top left
        '100px0px'   : { left : '100px', top : '100px' },//100px0px means the element is at the top right
        '0px100px'   : { left : '0px'  , top : '0px' },//0px100px means the element is at the bottom left
        '100px100px' : { left : '0px'  , top : '100px' }//100px100px means the element is at the bottom right
    };//this stores the conversion between where the element is and where it should animate to

//setup a function that can call itself inside the animation callback to iterate through the DOM elements to be animated
function run_animation($element) {

    //check to make sure there are more DOM elements to animate, if none are found then the process is done
    if (item_index in $items) {

        //get this element's top and left CSS properties and put them together so we can convert that to the next coordinates it needs
        var css = $items[item_index].css('left') + '' + $items[item_index].css('top');

        //now animate this element by converting the current top/left CSS properties to its next coordinates
        $items[item_index].animate({
            left : animations[css].left,
            top  : animations[css].top
        }, 1500, function () {

            //here we run this same function for the next index
            item_index++;
            run_animation($items[item_index]);
        });
    }
}

//run the animation function for the first time
run_animation($items[item_index]);
票数 2
EN

Stack Overflow用户

发布于 2011-11-30 04:48:47

Jasper的回答是好的。我给你提供了一些你应该阅读的链接:它们解释了你在代码中犯的一些常见错误以及如何避免它们;)

  • 7 Common Javascript Mistakes or Confusions (master article)
  • Premature Invocation (您的主error)
  • Delaying Repeatedly (这是Jasper所解释的,但有一点不同)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8317185

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档