刚刚通过requestAnimationFrame学习了动画,想到了这个问题。我做了一个简单的动画方法:
animate: function(callbackObj, duration) {
var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame;
var startTime = 0, percentage = 0, animationTime = 0;
duration = duration*1000 || 1000;
var animate = function(timestamp) {
if (startTime === 0) {
startTime = timestamp;
} else {
animationTime = timestamp - startTime;
}
if (typeof callbackObj.start === 'function' && startTime === timestamp) {
callbackObj.start();
requestAnimationFrame(animate);
} else if (animationTime < duration) {
if (typeof callbackObj.progress === 'function') {
percentage = animationTime / duration;
callbackObj.progress(percentage);
}
requestAnimationFrame(animate);
} else if (typeof callbackObj.done === 'function'){
callbackObj.done();
}
};
return requestAnimationFrame(animate);
},
它基本上是一种递归方法,每次屏幕刷新时都会更新。该方法接受一个回调对象,该对象具有.start、.progress和.done属性下的函数。
我稍微修改了你的代码:
_autoScroll: function() {
var sequenceObj = {};
var seconds = 2;
var rangeInPixels = 500;
sequenceObj.progress = (function(percentage) {
this.$.itemList.scroll(0, this.easeInOutQuad(percentage)*rangeInPixels);
}).bind(this);
this.animate(sequenceObj, seconds);
},
我从 Robert Penner 的缓动函数中得到了 easeInOut:
easeInOutQuad: function (t) { return t<.5 ? 2*t*t : -1+(4-2*t)*t },
和中提琴:
http://jsfiddle.net/5uLhu6qa/
这并不完全是您所要求的,但这是您可以继续的开始。