自定义缓和方程

时间:2013-10-11 21:21:47

标签: jquery math jquery-easing

我有这段代码:

$({ speed: 0 }).animate({ speed: 500 }, {
    duration: duration,
    easing: 'easeInSine', // can be anything
    step: function (now, fx) { // called on every step
        var $current = $(self.settings.itemClass).first();
        var left = parseInt($current.css("left"));

        //console.log(now);

        if (left <= distance) {
            $current.hide(now, "linear", function () {
                $(this).appendTo(self.settings.itemContainerClass).show(now, "linear");
            });
        }
    },
    complete: function () {
        var $container = $(self.settings.itemContainerClass);
        var $current = $(self.settings.itemClass).first();
        var id = parseInt($current.attr("id"));

        $.post("/Draw/SelectWinner/" + id, function (data) {
            $container.delay(1000).hide("fast", function () {
                var $name = $current.find("h3");
                var $img = $current.find(".carousel-photo").find("img");
                var $profileImage = ($img.attr("src") == "/img/transparent.png") ? "" : $img;
                var twitter = ($current.find(".twitter").length > 0) ? $current.find(".twitter").text() : "";

                var $winner = $('<div class="winner" />')
                    .css({
                        width: "100%",
                        height: "100%",
                        display: "none"
                    })
                    .addClass("red")
                    .appendTo(self.element)
                    .append("<div><h2>We have a winner!</h2></div>")
                    .append($name)
                    .append("<h4>" + twitter + "</h4>")
                    .append($profileImage)
                    .show("slow");
            });
        });
    }
});

如您所见,我使用 easeInSine 缓动功能将速度从0设置为500。 这有效,但并不能完全满足我的需求。我想要的是以恒定速度运行这个动画10秒然后逐渐减速直到它停止5秒,但我不知道如何编写缓动函数。

我知道你可以通过这样做来编写自己的缓动函数:

$.easing.crazy = function(t, millisecondsSince, startValue, endValue, totalDuration) {
    if(t<0.5) {
        return t*4;
    } else {
        return -2*t + 3;  
    }
};

我需要的部分是做

之类的事情
$.easing.crazy = function(t, millisecondsSince, startValue, endValue, totalDuration) {
    if(t < 10) {
        return 500;
    } else {
        // return a speed that gradually slows down
    }
};

如果有人能帮我解决这个问题,我会非常感激。

更新

感谢火花,我现在设法得到了这个:

$.easing.wheel = function (x, t, b, c, d) {
    return (t == d) ? b + c : c * (-Math.pow(2, -25 * t / d) + 1) + b;
}

但我还有一个问题。这运行15秒,但是前几秒它很好而且很快(我会说3秒),然后其余时间它会慢慢停止。 我需要它快速运行10秒钟,然后运行最后5秒减速。

有谁知道如何修改示例更好?

干杯

0 个答案:

没有答案