Skip to content

Instantly share code, notes, and snippets.

@brianswisher
Created December 7, 2015 15:56
Show Gist options
  • Save brianswisher/b020694e75b3a3ac123b to your computer and use it in GitHub Desktop.
Save brianswisher/b020694e75b3a3ac123b to your computer and use it in GitHub Desktop.
Has scroll collision; returns Boolean is target touches viewable scroll
((target)=>{
function getPos(el){
for (var lx=0, ly=0;
el != null;
lx += el.offsetLeft, ly += el.offsetTop, el = el.offsetParent);
return {x: lx,y: ly};
}
function getScrollAmount(){
const doc = document.documentElement;
return {
left: (window.pageXOffset || doc.scrollLeft) - (doc.clientLeft || 0),
top: (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0)
};
}
function getScrollMax(){
const scrollAmount = getScrollAmount();
return {
right: window.innerWidth + scrollAmount.left,
bottom: window.innerHeight + getScrollAmount().top
};
}
function hasScrollCollision(el){
const pos = getPos(el),
scroll = getScrollMax();
return scroll.right >= pos.x &&
scroll.bottom >= pos.y;
}
return hasScrollCollision(target);
})(document.querySelector("body>div.container"));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment