Created
April 16, 2021 06:15
-
-
Save mnoskov/4b6508f533c2dac6aa47deadb0e5e5e4 to your computer and use it in GitHub Desktop.
Change url on scrolling autoloaded articles
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
!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