Created
December 23, 2015 13:02
-
-
Save ThierryA/5b070cd76d03d4eca2b3 to your computer and use it in GitHub Desktop.
jQuery: is visible on 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
(function ($) { | |
"use strict"; | |
function isSceenVisible( element ) { | |
var $el = $( element ); | |
// Stop here if the element isn't visible. | |
if ( !$el.is( ':visible' ) || '0' == $el.css( 'opacity' ) ) | |
return false; | |
var viewportTop = $(window).scrollTop(), | |
viewportHeight = $(window).height(), | |
viewportBottom = viewportTop + viewportHeight, | |
top = $el.offset().top, | |
height = $el.height(), | |
bottom = top + height; | |
switch ( true ) { | |
case top >= viewportTop && top < viewportBottom: | |
return true; | |
break; | |
case bottom > viewportTop && bottom <= viewportBottom: | |
return true; | |
break; | |
case height > viewportHeight && top <= viewportTop && bottom >= viewportBottom: | |
return true; | |
break; | |
} | |
return false; | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment