Skip to content

Instantly share code, notes, and snippets.

@yairEO
Last active February 9, 2019 15:58
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 yairEO/5446b57b4c7ee6e25778 to your computer and use it in GitHub Desktop.
Save yairEO/5446b57b4c7ee6e25778 to your computer and use it in GitHub Desktop.
Find element which is in the middle of the screen
var findMiddleElement = (function(docElm){
var viewportHeight = docElm.clientHeight,
elements = $('div');
return function(e){
var middleElement;
if( e && e.type == 'resize' )
viewportHeight = docElm.clientHeight;
elements.each(function(){
var pos = this.getBoundingClientRect().top;
// if an element is more or less in the middle of the viewport
if( pos > viewportHeight/2.5 && pos < viewportHeight/1.5 ){
middleElement = this;
return false; // stop iteration
}
});
console.log(middleElement);
}
})(document.documentElement);
$(window).on('scroll resize', findMiddleElement);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment