本来想直接抄JQuery的animate实现方法的,结果没找到只能自己写一个了,后来对比了下滚动速度和性能分析发现差不多
<div style="height: 300px;overflow: auto" id="scroll">
<div style="height: 10000px"></div>
</div>
scrollTop({ dom: document.getElementId('scroll'), top: 100,})
function scrollTop(options) {
if (!options) { return }
var stop = options.top / 20;
var ctop = 0;
var timer = setInterval(function() {
ctop = ctop + stop;
if (ctop > options.top) {
clearInterval(timer)
} else {
options.dom.scroll(0, ctop)
}
}, 20)
}