Skip to content

Instantly share code, notes, and snippets.

@andrew8088
Created February 16, 2011 14:07
Show Gist options
  • Save andrew8088/829415 to your computer and use it in GitHub Desktop.
Save andrew8088/829415 to your computer and use it in GitHub Desktop.
This code is an alternate version of the code found in my tutorial on Nettuts+: http://net.tutsplus.com/tutorials/javascript-ajax/implement-twitter-scrolling-without-jquery-2
$(document).bind("scroll", function (e) {
scrolling = true;
});
setInterval(function () {
if (scrolling) {
//console.profile("scroll");
scrolling = false;
var temp_top = win.scrollTop(),
down = scroll_top - temp_top < 0 ? true : false,
top_item = null, child, style, i = 0, distance = 0;
scroll_top = temp_top;
if (prev_item) {
height = height || prev_item.offsetHeight;
if ( down && get_top_offset(prev_item) - scroll_top + height < margin) {
top_item = entries[ entries.indexOf(prev_item) + 1];
} else if ( !down && (get_top_offset(prev_item) - scroll_top - height) > (margin + parent_from_top)) {
top_item = entries[ entries.indexOf(prev_item) - 1];
} else {
top_item = prev_item;
}
} else {
for (i = 0; entries[i]; i++) {
distance = get_top_offset(entries[i]) - scroll_top;
if ( distance < (parent_from_top + margin) && distance > (parent_from_top - margin) ) {
top_item = entries[i];
break;
}
}
}
if (top_item) {
if ( (down && top_item.getAttribute("data-8088-starter")) || ( !down && top_item.getAttribute("data-8088-ender") ) ) {
if (!anti_repeater) {
child = unscrollable[ entries.indexOf(top_item) ];
anti_repeater = child.cloneNode(false);
document.body.appendChild(anti_repeater);
child.style.visibility = "hidden";
style = anti_repeater.style;
style.position = 'fixed';
style.top = img_offset.top + 'px';
style.left= img_offset.left + 'px';
}
}
if ( (down && top_item.getAttribute("data-8088-ender")) || (!down && top_item.getAttribute("data-8088-starter")) ) {
unscrollable[ entries.indexOf(top_item) ].style.visibility = "visible";
if (anti_repeater) { anti_repeater.parentNode.removeChild(anti_repeater); }
anti_repeater = null;
}
}
prev_item = top_item;
//console.profileEnd();
}
}, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment