简体   繁体   English

更改jQuery队列中动画的缓动功能

[英]Change easing functions on animations in jQuery queue

I have an animation linked to scroll position. 我有一个动画链接到滚动位置。 Whenever the the user scrolls up or down, an animation is triggered for that position to move an element within the view window. 每当用户上下滚动时,都会触发该位置的动画,以在视图窗口内移动元素。 If the user scrolls farther, these animations need to queue so that the element moves smoothly along the path. 如果用户进一步滚动,则这些动画需要排队,以便元素沿路径平滑移动。

var target = getAnimation();
var props = {
    left: [target.x, target.easing],
    top: target.y
};

$("#ball").animate(props, 400, "easeInOutQuad");

The problem with this is that when multiple animations get queued, the ball slows and speeds up in a bad way. 问题在于,当多个动画排入队列时,球会以不良的方式放慢速度并加速。 What I'd like to do is something like this: 我想做的是这样的:

var target = getAnimation();
var props = {
    left: [target.x, target.easing],
    top: target.y
};

var ball = $("#ball"), queue = ball.queue();

if(ball.queue().length) {
    for(var i = 1, len = queue.length; i < len; i++) {
        //modify all the other queued animations to use linear easing
    }
    ball.animate(props, 400, "easeOutQuad");
}
else {
    ball.animate(props, 400, "easeInQuad");
}

By starting with an easeIn function, using linear in the middle, and easeOut at the end, I get a much smoother animation. 通过从easyIn函数开始,在中间使用线性,并在最后使用easyOut,我获得了更加流畅的动画。 Is there anyway I can access and modify the animations in the queue? 无论如何,我可以访问和修改队列中的动画吗?

Edit: 编辑:

Here is a fiddle to demonstrate what I'm trying to achieve: https://jsfiddle.net/reesewill/mtepvguw/ 这是一个小提琴,用来演示我要实现的目标: https : //jsfiddle.net/reesewill/mtepvguw/

In the fiddle, I am using linear easing, but I'd really like the general affect to be more like easeInOutQuad. 摆弄,我正在使用线性缓动,但我真的希望总体效果更像是easyInOutQuad。 However, because I allow queueing, I can't just apply that easing function without it messing up the whole effect (change the linear to easeInOutQuad and click queue a few times quickly to see). 但是,因为允许排队,所以我不能只应用缓动函数而不会弄乱整个效果(将线性更改为easeInOutQuad,然后快速单击几次以查看队列)。 Thus, I need something like the above to create the general impression of easeInOutQuad. 因此,我需要类似上面的内容来创建easyInOutQuad的总体印象。

i tried, you can do it with create new (re-ordered) queue 我试过了,您可以通过创建新的(重新排序)队列来做到这一点

download source http://api.jquery.com/queue/ 下载源http://api.jquery.com/queue/
Example: Set a queue array to delete the queue. 示例:设置队列数组以删除队列。

and replace start event with my, its worked. 并用我的开始事件代替了它的工作。

But functions in queue are stored in array of functions. 但是队列中的函数存储在函数数组中。 You need to know order of original queue of animations which you want to changed :( Or you can create new optimalized queue. 您需要知道要更改的原始动画队列的顺序:(或者您可以创建新的优化队列。

$( "#start" ).click(function() {
  $( "div" )
    .show( "slow" )
    .animate({ left: "+=50" }, 5000 )
    .animate({ top: "+=50" }, 5000 )
    .queue(function() {
      $( this ).addClass( "newcolor" ).dequeue();
    })
    .animate({ left: '-=50' }, 1500 )
    .queue(function() {
      $( this ).removeClass( "newcolor" ).dequeue();
    })
    .slideUp();


    // get current queue
    var currQueue = $( "div" ).queue( "fx");

    // create new queue and change order or add/remove animations
    var newQueue = [];
      newQueue.push(currQueue[1]);
      newQueue.push(currQueue[3]); // changed
      newQueue.push(currQueue[2]); // changed
      newQueue.push(currQueue[5]);

    // set new queue to element
    $("div").queue("fx", newQueue);

    console.log($("div").queue("fx"));
}); 

more info found in jquery documentation jQuery文档中找到更多信息

.queue( [queueName ], newQueue ) .queue([queueName],newQueue)
Description: Manipulate the queue of functions to be executed, once for each matched element. 说明:对每个匹配的元素操纵一次要执行的功能队列。

important is second parameter newQueue 重要的是第二个参数newQueue

i hope it helps 我希望它会有所帮助

Note , $(selector) .queue() returns a reference to the animation queue, an Array . 注意, $(selector) .queue()返回对动画队列的引用Array This reference can be modified with standard array methods. 可以使用标准数组方法修改此引用。 See also .dequeue() . 另请参见.dequeue()

Try utilizing 尝试利用

Array.prototype.splice() Array.prototype.splice()

Summary 摘要

The splice() method changes the content of an array by removing existing elements and/or adding new elements. splice()方法通过删除现有元素和/或添加新元素来更改数组的内容。

Syntax 句法

array.splice(start, deleteCount[, item1[, item2[, ...]]]) Parameters array.splice(start,deleteCount [,item1 [,item2 [,...]]])参数

start 开始

Index at which to start changing the array. 开始更改数组的索引。 If greater than the length of the array, actual starting index will be set to the length of the array. 如果大于数组的长度,则实际的起始索引将设置为数组的长度。 If negative, will begin that many elements from the end. 如果为负,则将从头开始那么多元素。

deleteCount deleteCount

An integer indicating the number of old array elements to remove. 一个整数,指示要删除的旧数组元素的数量。 If deleteCount is 0, no elements are removed. 如果deleteCount为0,则不会删除任何元素。 In this case, you should specify at least one new element. 在这种情况下,您应该至少指定一个新元素。 If deleteCount is greater than the number of elements left in the array starting at start, then all of the elements through the end of the array will be deleted. 如果deleteCount大于开始时数组中剩余的元素数,则将删除数组末尾的所有元素。

itemN itemN

The element to add to the array. 要添加到数组中的元素。 If you don't specify any elements, splice() will only remove elements from the array. 如果不指定任何元素,则splice()只会从数组中删除元素。

Returns 返回

An array containing the deleted elements. 包含已删除元素的数组。 If only one element is removed, an array of one element is returned. 如果仅删除一个元素,则返回一个元素的数组。 If no elements are removed, an empty array is returned. 如果没有删除任何元素,则返回一个空数组。

See also Array.prototype.concat() 另请参见Array.prototype.concat()


 var elem = $("body") , msg = function() { return "<br />" + "queue length:" + $(this).queue("abc").length }; elem.queue("abc", [ function(next) { $(this).append(msg.call(this)); next() }, function(next) { $(this).append(msg.call(this)); next() }, function(next) { $(this).append(msg.call(this)); next() } ]); elem.append(msg.call(elem)); // do stuff, // replace `function` within `abc` queue, // change `easing` options within replacement function elem.queue("abc").splice(1, 1, function(next) { $(this).append("<br />" + "`function` at index `1` within `abc` queue " + "replaced with new `function`" + msg.call(this)); next() }); elem.append("<br />" + "after `.splice()` , before `.concat()`" + msg.call(elem)); // do stuff, // `concat` functions onto `abc` queue` var arr = elem.queue("abc").concat( function(next) { $(this).append(msg.call(this)); next() }, function(next) { $(this).append(msg.call(this)); next() }, function() { $(this).append(msg.call(this) + "<br />" + "done"); } ); elem.queue("abc", arr); elem.append("<br />" + "after `.concat()`" + msg.call(elem)); elem.dequeue("abc"); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"> </script> 

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

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