1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
Animation.prototype.start = function (selector, func) { var that = this; var target = this.store[selector]; target.status = 'running';
that.update({x: 0, y: 0}, selector); };
Animation.prototype.update = function (position, selector) { var target = this.store[selector], that = this, timeUsed, positionX, positionY; if (!target || !target.queue.length) { target.status = 'pending'; return; };
target.element.style.left = position.x + 'px'; target.element.style.top = position.y + 'px';
target.positionStart = { x: position.x, y: position.y, }; target.positionEnd = { x: position.x + target.queue[0].x, y: position.y + target.queue[0].y, }; target.timeStart = null;
var callback = function (time) { if (target.timeStart === null) target.timeStart = time; timeUsed = time - target.timeStart; if (timeUsed >= target.queue[0].duration) { target.queue.shift(); that.step(target.element, target.positionEnd.x, target.positionEnd.y); target.status = 'running'; var position = { x: parseInt(target.element.style.left), y: parseInt(target.element.style.top), }; that.update(position, selector); return; } positionX = target.queue[0].func( timeUsed, target.positionStart.x, target.positionEnd.x - target.positionStart.x, target.queue[0].duration, ); positionY = target.queue[0].func( timeUsed, target.positionStart.y, target.positionEnd.y - target.positionStart.y, target.queue[0].duration, ); that.step(target.element, positionX, positionY);
requestAnimationFrame(callback); };
requestAnimationFrame(callback); };
|