Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions jquery.visible.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,26 @@
*
* @author Sam Sehnert
* @desc A small plugin that checks whether elements are within
* the user visible viewport of a web browser.
* only accounts for vertical position, not horizontal.
* the user visible viewport of a web browser,
* or within the visible area of a containing element.
*/
var $w = $(window);
$.fn.visible = function(partial,hidden,direction){
var _$w = $(window);
$.fn.visible = function(partial,hidden,direction,container){

if (this.length < 1)
return;

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

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

// Use this native browser method, if available.
// Use this native browser method if available, if window is our container.
var rec = t.getBoundingClientRect(),
tViz = rec.top >= 0 && rec.top < vpHeight,
bViz = rec.bottom > 0 && rec.bottom <= vpHeight,
Expand All @@ -40,11 +41,13 @@
return clientSize && vVisible;
else if(direction === 'horizontal')
return clientSize && hVisible;

} else {

var viewTop = $w.scrollTop(),
var wOffset = container ? $w.offset() : null,
viewTop = container ? wOffset.top : $w.scrollTop(),
viewBottom = viewTop + vpHeight,
viewLeft = $w.scrollLeft(),
viewLeft = container ? wOffset.left : $w.scrollLeft(),
viewRight = viewLeft + vpWidth,
offset = $t.offset(),
_top = offset.top,
Expand All @@ -65,4 +68,4 @@
}
};

})(jQuery);
})(jQuery);
2 changes: 1 addition & 1 deletion jquery.visible.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.