Last active
February 9, 2019 15:58
-
-
Save yairEO/5446b57b4c7ee6e25778 to your computer and use it in GitHub Desktop.
Find element which is in the middle of the screen
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
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