Skip to content

Commit

Permalink
A container can now be passed to the method to be used for the calcul…
Browse files Browse the repository at this point in the history
…ation instead of the browser window; working with custom calculation only.
  • Loading branch information
itfourp committed Feb 3, 2016
1 parent 7a8fdba commit 4649021
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions jquery.visible.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,20 @@
* only accounts for vertical position, not horizontal.
*/
var $w = $(window);
$.fn.visible = function(partial,hidden,direction){
$.fn.visible = function(partial,hidden, direction, scrollContainer) {
var $c = scrollContainer || $w;

if (this.length < 1)
return;

var $t = this.length > 1 ? this.eq(0) : this,
t = $t.get(0),
vpWidth = $w.width(),
vpHeight = $w.height(),
vpWidth = $c.width(),
vpHeight = $c.height(),
direction = (direction) ? direction : 'both',
clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true;

if (typeof t.getBoundingClientRect === 'function'){
if (typeof t.getBoundingClientRect === 'function' && !scrollContainer) {

// Use this native browser method, if available.
var rec = t.getBoundingClientRect(),
Expand All @@ -41,10 +42,10 @@
else if(direction === 'horizontal')
return clientSize && hVisible;
} else {

var viewTop = $w.scrollTop(),
// Use this calculation if scrollContainer was passed or native method is not available
var viewTop = $c.scrollTop(),
viewBottom = viewTop + vpHeight,
viewLeft = $w.scrollLeft(),
viewLeft = $c.scrollLeft(),
viewRight = viewLeft + vpWidth,
offset = $t.offset(),
_top = offset.top,
Expand All @@ -55,6 +56,15 @@
compareBottom = partial === true ? _top : _bottom,
compareLeft = partial === true ? _right : _left,
compareRight = partial === true ? _left : _right;
// add offset if scrollContainer was passed
if (scrollContainer) {
var viewOffset = $c.offset();
viewTop += viewOffset.top - $c.scrollTop();
viewBottom += viewOffset.top - $c.scrollTop();
viewLeft += viewOffset.left - $c.scrollLeft();
viewRight += viewOffset.left - $c.scrollLeft();

}

if(direction === 'both')
return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop)) && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
Expand Down

0 comments on commit 4649021

Please sign in to comment.