Skip to content

Instantly share code, notes, and snippets.

@BatuhanK
Created March 9, 2017 21:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BatuhanK/fff1c4fbbc18e53e18eb7436122c0f4d to your computer and use it in GitHub Desktop.
Save BatuhanK/fff1c4fbbc18e53e18eb7436122c0f4d to your computer and use it in GitHub Desktop.
var latestTweetIndex = 0;
function scrollToNextElement(e) {
if(e.keyCode == 32 && e.target == document.body) {
e.preventDefault();
var streamItems = document.querySelectorAll('.stream-items>.stream-item');
console.log('streamItems', streamItems.length);
latestTweetIndex = findLatestVisibleTweetIndex(streamItems);
console.log('latestTweetIndex--', latestTweetIndex);
window.scrollBy(0, streamItems[latestTweetIndex].getBoundingClientRect().bottom - (window.innerHeight || document.documentElement.clientHeight));
}
}
function isElementInViewport (el) {
if (typeof jQuery === "function" && el instanceof jQuery) {
el = el[0];
}
var rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
);
}
function findLatestVisibleTweetIndex(streamItems) {
for (var i=latestTweetIndex; i < streamItems.length; i++) {
if(!isElementInViewport(streamItems[i]) && i>latestTweetIndex) {
return i;
}
}
return latestTweetIndex;
}
window.addEventListener('keydown', scrollToNextElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment