简体   繁体   English

为jQuery中的多个元素平滑地链接动画

[英]Chaining animations smoothly for several elements in jQuery

I'm trying to recreate a map's zoom-in effect by animating sequentially several stacked images using jQuery, for cross-domain purposes. 我正在尝试通过使用jQuery为跨域目的依次为几个堆叠的图像设置动画来重新创建地图的放大效果。

I achieved something so far, by queueing the animations using delays and two single animations (A & B logs) for each image, in order to generate smooth transitions between images by zooming, and fading them over the next one. 到目前为止,我取得了一些成就,通过为每个图像使用延迟和两个单个动画(A和B日志)对动画进行排队,以便通过缩放在图像之间生成平滑过渡,并在下一个图像上使它们淡入淡出。

$('img:not(:last-child)') /* All images but farest zoom */
.reverse() /* From last to first */
.each(function (index) {
    $(this).css( /* From half size… */ {
        'width': 584,
        'height': 336,
        'margin-left': -292,
        'margin-top': -168
    });
    $(this).delay(index * 300).animate( /* …to actual size */ {
        'width': 1168,
        'height': 673,
        'margin-left': -584,
        'margin-top': -336
    }, {
        duration: 300,
        easing: 'linear',
        done: function () {
            console.log('A:', index, new Date().getTime() - timestamp);
        }
    });
});
$('img:not(:first-child)') /* All images but closest zoom */
.reverse() /* From last to first */
.each(function (index) {
    $(this).animate( /* Animate to double size */ {
        'width': 2336,
        'height': 1346,
        'margin-left': -1168,
        'margin-top': -673,
        'opacity': 0
    }, {
        duration: 300,
        easing: 'linear',
        done: function () {
            console.log('B:', index, new Date().getTime() - timestamp);
            $(this).remove(); /* Remove the elment once completed */
        }
    });
});

It's well known jQuery's lack of support to queue animations for different DOM elements in a single queue, wich leads to this complex solution. 众所周知,jQuery不支持在单个队列中将不同DOM元素的动画排入队列,这导致了这种复杂的解决方案。

Please check this Fiddle . 检查此小提琴

As you'll see, once the images are fully loaded and you click in the map, the animation queue starts. 如您所见,一旦图像完全加载并单击地图,动画队列就会开始。 But it's far from perfect. 但这远非完美。 The transitions aren't fluid at all , causing a little pause between animations, that ruins the result. 过渡一点都不流畅,在动画之间造成一点停顿,破坏了结果。 I've tried everything for hours, playing with the timeouts, rethinking the algorithm, forcing linear transitions, but with no result. 我已经尝试了好几个小时,尝试超时,重新思考算法,强制进行线性转换,但是都没有结果。

My main goal is to achieve a fluid animation , and then, recreate a global easing effect like 'swing' for the entire animation, speeding up progressively as middle images are animated. 我的主要目标是实现流畅的动画 ,然后为整个动画重新创建全局缓动效果,例如“摇摆”,并随着中间图像的动画化而逐渐加快速度。

So a spent my last few hour figuring out what's the hack here, and here is the code you should inject 因此,我花了最后几个小时来弄清楚这里的漏洞,这是您应该注入的代码

jQuery.easing = {
    zoom: function( p ) {
        return (3*p + Math.pow( p, 2 ))/4;
    }
};

After that you can use easing: 'zoom' in your code. 之后,您可以在代码中使用easing: 'zoom'

Quite ridiculous btw that in the jQuery UI there's 32 different easing but nothing to zoom!!! 荒谬的是,在jQuery UI中有32种不同的缓动,但是没有什么可缩放的!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM