Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在jQuery动画中向前跳?_Jquery - Fatal编程技术网

在jQuery动画中向前跳?

在jQuery动画中向前跳?,jquery,Jquery,有没有办法在时间上向前/向后跳转jQuery动画 例如,如果我已将某个元素上的动画设置为10秒,我可以跳转到该动画中的“5秒”吗?最好用百分比来设置。 您可以尝试在动画或动画中设置队列 如果你能提供一个例子,你肯定会得到一个很好的提示;) 可以停止当前动画,将动画对象的状态设置为初始状态和最终状态之间的一半,然后将新动画启动为原始最终状态,但设置时间为一半 这将跳到动画的一半位置,然后继续从那里 这里有一个工作示例:。除了百分比功能之外,它应该拥有您想要的一切: 演示: jQuery: var

有没有办法在时间上向前/向后跳转jQuery动画

例如,如果我已将某个元素上的动画设置为10秒,我可以跳转到该动画中的“5秒”吗?最好用百分比来设置。

您可以尝试在动画或动画中设置队列


如果你能提供一个例子,你肯定会得到一个很好的提示;)

可以停止当前动画,将动画对象的状态设置为初始状态和最终状态之间的一半,然后将新动画启动为原始最终状态,但设置时间为一半

这将跳到动画的一半位置,然后继续从那里


这里有一个工作示例:。

除了百分比功能之外,它应该拥有您想要的一切:

演示:

jQuery:

var time = 10000; // Animation speed in ms
var opacity = 0; // Final desired opacity


function jumpAnimate(setOpacityTo, newOpacity, speed){  // I created a function to simplify things
    $("#fade").css("opacity", setOpacityTo).animate({
        "opacity": newOpacity
    }, {
        duration: speed // I used speed instead of time because time is already a global variable
    });
}

$("#jump").click(function(){
    var goTo = $("#jump-to").val(); // Get value from the text input

    var setOpacity = (1 / time) * (time - goTo); /* The opacity that the element should be set at, i.e. the actual jump
    Math is as follows: initial opacity / the speed of the original animation in ms * the time minus the desired animation stop in ms (basically the remaining time past the desired point) */

    $("#fade").stop(); // Stop the original animation after the math is finished so that we have minimal delay

    jumpAnimate(setOpacity, opacity, time - goTo); // Start a new animation
});

jumpAnimate(1, opacity, time);​ // Start the initial animation

你不能使用文档化的方法(我确信有一种方法可以使用未文档化的方法来完成,但我不推荐),但是你可以停止一个队列,让它在队列中的下一个方法上继续。你也可以创建一个插件,只需做动画已经在做的数学运算,停止,然后从选定点继续。如果你给我一个演示,我就做。@a.M.K thx!我完成了它,你只需将动画速度除以100,然后乘以你想要达到的任何百分比,就可以计算出百分比。还需要破解缓和函数。@jederikb-线性缓和最简单-无需破解。请参见我在回答中添加的工作示例。