Trying to understand how this code works to detect when an element in viewport
so first we get getBoundingClientRect()
of the element and then check if the top in that DOMRect is less than window.innerHeight
that means element in viewport?
function checkVisibleSection(){
var minor = window.innerHeight,
section = null;
//---Select the section closest to the top
[].forEach.call(sections, function(item){
var offset = item.getBoundingClientRect();
if(Math.abs(offset.top) < minor){
minor = Math.abs(offset.top);
section = item;
}
});
so in this snippet why using Math.abs is a must? It does not give correct answer without Math.abs also why did they reassign the minor to offset.top ?