4

我将一个元素分成多个块(由许多行和列定义),然后淡化这些块以创建动画效果。动画的类型由delay()值决定:

$('.block').each(function (i) {
  $(this).stop().delay(30 * i).animate({
    'opacity': 1
  }, {
    duration: 420
  });
});

在这种情况下,每个块的淡入淡出效果都会延迟(30 * 当前块索引)。第一个块得到 0 延迟,第二个块 30 延迟,......最后一个块 30 *(块数)延迟。所以这将水平淡化所有块。

我已经发布了到目前为止我在这里提出的效果列表:http: //jsfiddle.net/MRPDw/

我需要帮助的是找到螺旋型效果的延迟表达式,也许还有你认为可能的其他表达式:D

4

2 回答 2

4

以下是螺旋图案的代码示例:

  case 'spiral':
    $('.block', grid).css({
        'opacity': 0
    });
    var order = new Array();
    var rows2 = rows/2, x, y, z, n=0;
        for (z = 0; z < rows2; z++){
            y = z;
            for (x = z; x < cols - z - 1; x++) {
                order[n++] = y * cols + x;
            }
            x = cols - z - 1;
            for (y = z; y < rows - z - 1; y++) {
                order[n++] = y * cols + x;
            }
            y = rows - z - 1;
            for (x = cols - z - 1; x > z; x--) {
                order[n++] = y * cols + x;
            }
            x = z;
            for (y = rows - z - 1; y > z; y--) {
                order[n++] = y * cols + x;
            }
        }

    for (var m = 0; m < n; m++) {
        $('.block-' + order[m], grid).stop().delay(100*m).animate({
            opacity: 1
        }, {
            duration: 420,
            complete: (m != n - 1) ||
                function () {
                    alert('done');
                }
        });
    }
    break;

看到它在这个小提琴中工作。

我还改进了您的“随机”动画,以显示所有方块,而不仅仅是一个子集。代码是:

  case 'random':

    var order   = new Array();
    var numbers = new Array();

    var x, y, n=0, m=0, ncells = rows*cols;
    for (y = 0; y < rows; y++){
        for (x = 0; x < cols; x++){
            numbers[n] = n++;
        }
    }
    while(m < ncells){
        n = Math.floor(Math.random()*ncells);
        if (numbers[n] != -1){
            order[m++] = n;
            numbers[n] = -1;
        }
    }   

    $('.block', grid).css({
      'opacity': 0
    });

    for (var m = 0; m < ncells; m++) {
        $('.block-' + order[m], grid).stop().delay(100*m).animate({
            opacity: 1
        }, {
            duration: 420,
            complete: (m != ncells - 1) ||
            function () {
                alert('done');
            }
        });
    }

    break;

看到它在这个小提琴中工作。

于 2011-05-01T20:42:17.353 回答
1

也许考虑制作螺旋动画的最简单方法是将矩阵视为一张纸。

如果你在 x 和 y 中心轴上折叠 2 倍该纸,你最终会得到一个更小的正方形(或矩形)象限。

现在,如果你只从右下角到左上角为这个象限设置动画(就像你对“对角反转”所做的那样),你可以将此运动传播到其他 3 个象限以获得最终效果从矩阵中心到四个角的动画。

case 'spiral':
    $('.block', grid).css({
        'opacity': 0
    });
    n = 0;
    var center = {
        x: cols / 2,
        y: rows / 2
    };
    // iterate on the second quadrant only
    for (var y = 0;  y < center.y; y++)
        for (var x = 0; x < center.x; x++) {
            // and apply the animation to all quadrants, by using the multiple jQuery selector
            $('.block-' + (y * rows              + x) +            ', ' +    // 2nd quadrant
              '.block-' + (y * rows              + cols - x - 1) + ', ' +    // 1st quadrant
              '.block-' + ((rows - y - 1) * rows + x) +            ', ' +    // 3rd quadrant
              '.block-' + ((rows - y - 1) * rows + cols - x - 1)             // 4th quadrant
             , grid).stop().delay(100 * (center.y - y + center.x - x)).animate({
                    opacity: 1
                }, {
                    duration: 420,
                    complete: function () {
                        if (++n == rows * cols) {
                            alert('done'); // fire next animation... 
                        }
                    }
                });
        }

这是演示(单击螺旋链接)

于 2011-05-01T19:02:49.527 回答