Skip to content

Instantly share code, notes, and snippets.

@aradnom
Created November 5, 2013 07:43
Show Gist options
  • Save aradnom/7315309 to your computer and use it in GitHub Desktop.
Save aradnom/7315309 to your computer and use it in GitHub Desktop.
Small jQuery extension for filtering array of items by visibility in container (not just DOM visibility, but visible to user in the specified container). Filters based on left and right boundaries only.
// Return array of items visible in container. This means items which are not
// only viewable in the DOM, but also viewable to the user (inside the container).
// Note this will only check left and right boundaries and also that it will return
// elements which are partially visible (one foot in the door).
$.fn.visibleInContainer = (function ( container ) {
var left = container.offset().left,
right = left + container.outerWidth();
var visible = $.grep( this, function ( el ) {
var el_left = $(el).offset().left,
el_right = el_left + $(el).outerWidth();
return ( ( el_left <= right && el_left >= left ) || ( el_right >= left && el_right <= right ) );
});
return $( visible );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment