Skip to content

Instantly share code, notes, and snippets.

@bbottema
Created May 1, 2022 13:32
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 bbottema/e18649bedbb56f2ad777e366f0f724f3 to your computer and use it in GitHub Desktop.
Save bbottema/e18649bedbb56f2ad777e366f0f724f3 to your computer and use it in GitHub Desktop.
<script>
document.addEventListener("DOMContentLoaded", function() {
sortChildnodes(document.documentElement);
function sortChildnodes(ele) {
if (ele.append) {
ele.childNodes.forEach(sortChildnodes);
var allList = [...ele.childNodes];
var sortList = allList.filter(checkSortCondition);
if (sortList.length > 1) {
sortList.sort((a, b) => y(a) - y(b))
ele.replaceChildren();
var shiftingSortList = [...sortList];
allList.forEach(child => ele.appendChild(sortList.includes(child) ? shiftingSortList.shift() : child));
}
}
}
function checkSortCondition(ele) {
return ele instanceof Element && window.getComputedStyle(ele).position === 'absolute';
}
function y(ele) {
return ele.getBoundingClientRect().top;
}
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment