.曲线的动画

ber*_*nte 6 html javascript jquery curve jquery-animate

先来看看:

在此输入图像描述

猫需要在曲线中移动到x .(见箭头)

当猫击中x时,它应该保持10秒,之后猫应该回到o,再次在曲线中,然后重复.

我用这段代码试了一下:

function curve() {
    $('#cat').delay(10000).animate({top: '-=20',left: '-=20'}, 500, function() {
        $('#cat').delay(10000).animate({top: '+=20', left: '+=20'}, 500, function() {
            curve();
        });
    });
}

curve();
Run Code Online (Sandbox Code Playgroud)

但猫正在这样移动:

在此输入图像描述

有没有办法让猫在这种曲线中移动?

mat*_*hat 1

您可以通过执行复合运动来使用缓动来实现这一点:

function curve () {
    $('#cat').delay(10000).animate({top: "+=20px", left: "+=20px"}, {
      duration: 500, 
      specialEasing: {top: 'easeOutQuad', left: 'easeInQuad'}, 
      complete: function () { 
        $('#cat').animate({top: "-=20px", left: "+=20px"}, {
          duration: 500, 
          specialEasing: {top: 'easeInQuad', left: 'easeOutQuad'},
          complete: function() {
            // repeat the other way around.
          }});
      }
    });
}
Run Code Online (Sandbox Code Playgroud)

根据jQuery 文档,它从 jQuery 1.4 开始工作,并且提到的缓动需要 jQuery UI(但仅限于Effect Core模块)。每次.animate()调用都占整圆路径的四分之一,而反向easeInQuadvs 则easeOutQuad使路径看起来像圆形路径,而不是笔直到达新位置。