Skip to content

Commit

Permalink
add horizontal check
Browse files Browse the repository at this point in the history
  • Loading branch information
frozeman committed Jul 11, 2013
1 parent 3522eea commit 5546b75
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 35 deletions.
13 changes: 6 additions & 7 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@ Optionally, you can sepecify a second parameter to the `.visible` plugin, which
whether it's within the viewport too.

$('#element:visible').visible( false, true );


Optionally, you can add a third parameter to specify the direction to check for visibility. This can either be 'horizontal', 'vertical' or 'both'.
Default is to 'both'.

$('#element').visible( false, false, 'horizontal' );


Demos
-----
Expand All @@ -40,9 +45,3 @@ See the blog article:

- [Checking if an element is visible on-screen using jQuery](http://www.teamdf.com/web/jquery-element-onscreen-visibility/194/)


Limitations
-----------

Currently the plugin only checks for vertical positioning and scroll. We're planning on adding horizontal support soon,
with the ability to optionally run the check on horizontal, vertical or both planes.
2 changes: 1 addition & 1 deletion df-visible.jquery.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "df-visible",
"version" : "1.1.0",
"version" : "1.2.0",
"title" : "Element Onscreen Visibility",
"author" : {
"name" : "Team DF",
Expand Down
62 changes: 37 additions & 25 deletions jquery.visible.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,41 @@
(function($){

/**
* Copyright 2012, Digital Fusion
* Licensed under the MIT license.
* http://teamdf.com/jquery-plugins/license/
*
* @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.
*/
$.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));
/**
* Copyright 2012, Digital Fusion
* Licensed under the MIT license.
* http://teamdf.com/jquery-plugins/license/
*
* @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.
*/
$.fn.visible = function(partial,hidden,direction){

var $t = $(this).eq(0),
t = $t.get(0),
$w = $(window),
viewTop = $w.scrollTop(),
viewBottom = viewTop + $w.height(),
viewLeft = $w.scrollLeft(),
viewRight = viewLeft + $w.width(),
_top = $t.offset().top,
_bottom = _top + $t.height(),
_left = $t.offset().left,
_right = _left + $t.width(),
compareTop = partial === true ? _bottom : _top,
compareBottom = partial === true ? _top : _bottom,
compareLeft = partial === true ? _right : _left,
compareRight = partial === true ? _left : _right,
clientSize = hidden === true ? t.offsetWidth * t.offsetHeight : true,
direction = (direction) ? direction : 'both';

if(direction === 'both')
return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop)) && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
else if(direction === 'vertical')
return !!clientSize && ((compareBottom <= viewBottom) && (compareTop >= viewTop));
else if(direction === 'horizontal')
return !!clientSize && ((compareRight <= viewRight) && (compareLeft >= viewLeft));
};

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

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

0 comments on commit 5546b75

Please sign in to comment.