Skip to content

Instantly share code, notes, and snippets.

@mnoskov
Created April 16, 2021 06:15
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 mnoskov/4b6508f533c2dac6aa47deadb0e5e5e4 to your computer and use it in GitHub Desktop.
Save mnoskov/4b6508f533c2dac6aa47deadb0e5e5e4 to your computer and use it in GitHub Desktop.
Change url on scrolling autoloaded articles
!function() {
var wrap = document.querySelector('.tdb-autoload-wrap'),
base = location.href;
if (wrap) {
window.addEventListener('scroll', function(e) {
var wndTop = window.scrollY,
wndBottom = wndTop + window.innerHeight,
wndHeight = window.innerHeight,
currentArticle,
currentFill;
Array.prototype.forEach.call(wrap.children, function(article) {
if (!currentArticle) {
currentArticle = article;
currentFill = 0.5;
return;
}
var artTop = article.offsetTop,
artBottom = artTop + article.scrollHeight,
fill = (Math.min(artBottom, wndBottom) - Math.max(artTop, wndTop)) / wndHeight;
if (fill > currentFill) {
currentArticle = article;
currentFill = fill;
}
});
if (!wrap.currentArticle || currentArticle != wrap.currentArticle) {
wrap.currentArticle = currentArticle;
if (currentArticle.tagName.toLowerCase() == 'iframe') {
history.replaceState('', '', currentArticle.src.replace(/[\?&]tdb_action=tdb_ajax/, ''));
} else {
history.replaceState('', '', base);
}
}
});
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment