简体   繁体   English

.动画曲线

[英].animate with a curve

First take a look:先来看看:

在此处输入图片说明

The cat needs to move to the x in a curve .猫需要移动到曲线中的 x 处。 (see the arrow) (见箭头)

When the cat hits the x, it should stay 10 seconds, and after that the cat should go back to o, again in a curve, and repeat.当猫撞到 x 时,它应该停留 10 秒,然后猫应该回到 o,再次弯曲,并重复。

I tried it with this code:我用这个代码试了一下:

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

curve();

But the cat is moving like this:但是猫是这样移动的:

在此处输入图片说明

Is there a way to get the cat to move in this kind of curve?有没有办法让猫在这种曲线上移动?

http://tympanus.net/codrops/2010/05/07/stunning-circular-motion-effect/ http://tympanus.net/codrops/2010/05/07/stunning-circular-motion-effect/

Found this by googling "jquery radial motion"通过谷歌搜索“jquery径向运动”找到了这个

You can use easing to achieve that, by doing a compound movement :您可以使用缓动来实现这一目标,方法是进行复合运动:

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

It works since jQuery 1.4, according to jQuery docs and the easings mentionned require jQuery UI (but only the Effect Core module).根据jQuery 文档,它从 jQuery 1.4 开始工作,并且提到的缓动需要 jQuery UI(但只有Effect Core模块)。 Each .animate() call accounts for a quarter of a full circle path, and the reverse easeInQuad vs. easeOutQuad makes the path looks like a circular path instead of straight to the new position.每个.animate()调用占完整圆形路径的四分之一,而相反的easeInQuadeaseOutQuad使路径看起来像一个圆形路径,而不是直接到达新位置。

A modern answer for 2021. You don't have to use jQuery for this. 2021 年的现代答案。您不必为此使用 jQuery。 But I'm assuming you want to.但我假设你想要。 You can use a trendy npm library like popmotion (600k downloads per week) in conjunction with jQuery like so:您可以将流行的 npm 库(如 popmotion(每周 60 万次下载))与 jQuery 结合使用,如下所示:

// poo.el.animate({top: footerTop - horsePooHeight})

// your imports may vary - I'm using Angular, popmotion focuses more on Vue and React
import { cubicBezier } from 'popmotion'
declare const popmotion: any
const {tween, easing} = popmotion

const fling = cubicBezier(0, .42, 0, 1)
tween({from: 100, to: 200, duration: 400, ease: fling}).start((v) => $(el).css('top', v))
tween({from: 100, to: 300, duration: 400, ease: easing.linear}).start((v) => $(el).css('left', v))

Where el is a jQuery element.其中 el 是一个 jQuery 元素。

Good news.好消息。 Its got a whole world more power in terms of easing, allowing curves.它在缓和方面获得了全世界更多的力量,允许曲线。 Bad news is its a little more complicated but if you can understand the above code you will be fine.坏消息是它有点复杂,但如果你能理解上面的代码,你会没事的。

PS I'm not saying popmotion should be used with jQuery. PS 我不是说 popmotion 应该与 jQuery 一起使用。 I'm just saying it can be.我只是说可以。

PPS Never forget that JESUS loves you :D PPS 永远不要忘记耶稣爱你 :D

PPPS jQuery is fine for simpler animations, but the lack of interest in questions like this and the lack of updated on jQuery animation libs prove that jQuery on its own is dead for more complex animations. PPPS jQuery 适用于更简单的动画,但对此类问题缺乏兴趣以及缺乏 jQuery 动画库的更新证明 jQuery 本身对于更复杂的动画已经死了。

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

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