Created
March 9, 2017 21:40
-
-
Save BatuhanK/fff1c4fbbc18e53e18eb7436122c0f4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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