Created
May 1, 2022 13:32
-
-
Save bbottema/e18649bedbb56f2ad777e366f0f724f3 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
<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