jQuery 沿着贝塞尔曲线制作动画,每个周期出现不规则的 y 轴偏移

发布于 2024-12-10 11:09:43 字数 1374 浏览 0 评论 0原文

我基本上是在正弦波上为对象设置动画。然而,它只遍历一个段一次,而我需要它不断地遍历。

我递归地重复动画以使其遵循路径,而不是通常从头开始重新启动的方式。

然而,循环的每次重复都会在下一个循环开始时将元素稍微降低。

我怎样才能让它无缝地遵循曲线?

查看演示:http://jsfiddle.net/nfeZF/121/


function float(dir){
    var x_current = $("div").offset().left;
    var y_current = $("div").offset().top;
    var SineWave = function() {
        // this is called every step in the animation over 6000 miliseconds, updating the x/y coordinates
        // with sin rules applied to follow the curve
        // repeating this cycle (each timeout callback) drops the 
        // object about 50-100 pixels down from where it left off
        this.css = function(p) {
            var s = Math.sin(p*10);
            if (!dir){
                // swap the direction to continue on the path
                s = -s;   
            }
            var x =  300-p * 300;
            var y = s * 100 + 100;
            var o = ((s+2)/4+0.1);
            // add the current x-position to continue the path, rather than restarting
            return {top: y + "px", left: x_current + x + "px"};
        } 
    };

    $("div").animate({
        path: new SineWave
    }, 6000, 'linear', function(){
        // avoid exceeding stack
        setTimeout(float(!dir), 0);
    });

}

I'm basically animating an object over a sin wave. However, it only traverses a segment once, whereas I need it to continually traverse.

I am repeating the animation recursively for it to follow the path, rather than how it would normally restart back at the beginning.

However, each repeat of the cycle drops the element slightly lower at the beginning of the next cycle.

How can I make this follow the curve seamlessly?

View Demonstration:http://jsfiddle.net/nfeZF/121/


function float(dir){
    var x_current = $("div").offset().left;
    var y_current = $("div").offset().top;
    var SineWave = function() {
        // this is called every step in the animation over 6000 miliseconds, updating the x/y coordinates
        // with sin rules applied to follow the curve
        // repeating this cycle (each timeout callback) drops the 
        // object about 50-100 pixels down from where it left off
        this.css = function(p) {
            var s = Math.sin(p*10);
            if (!dir){
                // swap the direction to continue on the path
                s = -s;   
            }
            var x =  300-p * 300;
            var y = s * 100 + 100;
            var o = ((s+2)/4+0.1);
            // add the current x-position to continue the path, rather than restarting
            return {top: y + "px", left: x_current + x + "px"};
        } 
    };

    $("div").animate({
        path: new SineWave
    }, 6000, 'linear', function(){
        // avoid exceeding stack
        setTimeout(float(!dir), 0);
    });

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文