使用 jQuery 连接效果

发布于 2024-12-18 21:43:13 字数 930 浏览 6 评论 0原文

我有 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 技术交流群。

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

发布评论

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

评论(2

锦爱 2024-12-25 21:43:13

这是我为回答一个非常类似的问题而制作的jsfiddle: http://jsfiddle.net/jJ8vJ/ (问题可以在这里找到:如何按顺序设置动画位置?)。

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

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]);

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

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]);
空城缀染半城烟沙 2024-12-25 21:43:13

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

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 ;)

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