Created
March 19, 2017 20:43
-
-
Save Tusko/2db7fa2a98b9402bb69ff5c62559a54c to your computer and use it in GitHub Desktop.
Check if element is visible (in viewport(
This file contains hidden or 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($) { | |
| $.fn.visible = function(partial, hidden) { | |
| var $t = $(this).eq(0), | |
| t = $t.get(0), | |
| $w = $(window), | |
| viewTop = $w.scrollTop(), | |
| viewBottom = viewTop + $w.height(), | |
| _top = $t.offset().top, | |
| _bottom = _top + $t.height(), | |
| compareTop = partial === true ? _bottom : _top, | |
| compareBottom = partial === true ? _top : _bottom, | |
| clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true; | |
| return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop)); | |
| }; | |
| })(jQuery); | |
| /* | |
| if( $('.el').visible(true) ) { | |
| //your code here | |
| } | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment