您可以使用 translate3d 或类似的 CSS 和 3 个容器来获得类似的结果:
html:
<input id="run" type="button" value="Run">
<ul style="list-style:none;">
<li style="position:relative; overflow:hidden;">
<div id="content" style="background-color:#ff0000; width:100px; height:100px;">
<p>this is content</p>
</div>
</li>
</ul>
<p id="totalFrames">0</p>
Javascript:
var transform = /(Chrome|Safari)/.test(navigator.userAgent) ? '-webkit-transform' : 'transform',
run = document.getElementById('run'),
content = document.getElementById('content'),
container = document.getElementById('container'),
ticks = document.getElementById('totalFrames'),
curPos = 0,
end = 100;
function tick() {
var dist = (end - curPos) * .05;
curPos += dist;
if (Math.abs(dist) > .01) {
ticks.innerHTML = requestAnimationFrame(tick, null);
} else {
curPos = end;
end = -curPos;
}
content.style[transform] = 'translate3d(0, ' + curPos + 'px, 0)';
container.style[transform] = 'translate3d(0, ' + -curPos + 'px, 0)';
}
run.addEventListener('click', tick);
jsfiddle:http: //jsfiddle.net/my4Sv/7/