3

我需要为图像设置动画,但我想使用 CSS 变换。我发现了一个以前的问题,解决方案是这样的:

jsFiddle

我这样修改它:

jsFiddle

我不知道如何传递 x 和 y 值,因为我需要 translate(x,y) 两者。

这种方式行不通:

$('#box').animate({  fake: [100,50] }, {
    step: function(now,fx) {
       $(this).css('-webkit-transform','translate(' + now[0] + 'px,' + now[1] + 'px)'); 
    },
    duration:'slow'
},'linear');
4

3 回答 3

2
;(function($) {
            var delay = 0;
            $.fn.translate3d = function(translations, speed, easing, complete) {
                var opt = $.speed(speed, easing, complete);
                opt.easing = opt.easing || 'ease';
                translations = $.extend({x: 0, y: 0, z: 0}, translations);

                return this.each(function() {
                    var $this = $(this);

                    $this.css({ 
                        transitionDuration: opt.duration + 'ms',
                        transitionTimingFunction: opt.easing,
                        transform: 'translate3d(' + translations.x + 'px, ' + translations.y + 'px, ' + translations.z + 'px)'
                    });

                    setTimeout(function() { 
                        $this.css({ 
                            transitionDuration: '0s', 
                            transitionTimingFunction: 'ease'
                        });

                        opt.complete();
                    }, opt.duration + (delay || 0));
                });
            };
        })(jQuery);

这样你也可以使用 translateZ;

来源http://cameronspear.com/blog/animating-translate3d-with-jquery/

于 2013-10-14T11:16:55.203 回答
1
$('#box').animate({  fake: 200, fake2: 10 }, {
    step: function(now,fx) {
      $(this).css('-webkit-transform','translate('+now+'px,'+now+'px )'); 
    },
    duration:'slow'
},'linear');

jsFiddle

于 2013-04-09T23:31:42.427 回答
1

这种方式似乎可行,但它可能存在更好的解决方案

jsfiddle 示例

var offX, offY;

$('#box').animate({  fake1: 150, fake2: 100 }, {
  step: function(now,fx) {

    if (fx.prop === "fake1") {
      offX = now;
    } else if (fx.prop === "fake2") {
      offY = now;
      $(this).css('-webkit-transform','translate('+ offX +'px,'+ offY +'px)');
    }
  },
  duration:300
},'linear');
于 2013-04-10T08:00:31.013 回答