function createDom(start, count, height) {
var container = document.querySelector('.container');
var div = document.createDocumentFragment();
if (height) {
var p = document.createElement('p');
p.style.height = height + 'px';
div.appendChild(p);
}
for(var i=start, len=start+count; i<len; i++) {
var item = document.createElement('div');
item.className = 'item';
item.innerHTML = i;
div.appendChild(item);
}
container.innerHTML = '';
container.appendChild(div);
}
var start = 0;
var count = 20;
createDom(start, count);
function handleScroller() {
var lastStart = 0;
var item = document.querySelector('.container .item');
var itemStyle = getComputedStyle(item);
var itemHeight = item.offsetHeight + parseInt(itemStyle['marginTop']) + parseInt(itemStyle['marginBottom']);
return function() {
var currentScrollTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);
var fixedScrollTop = currentScrollTop - currentScrollTop % itemHeight;
var start = Math.floor( currentScrollTop/itemHeight );
if (lastStart!==start) {
lastStart = start;
createDom(start, count, fixedScrollTop);
}
}
}
window.addEventListener('scroll', handleScroller(), false);