Created
October 20, 2016 12:05
-
-
Save rossriley/8e2014fbee2d72077e6d126821d3f842 to your computer and use it in GitHub Desktop.
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($) { | |
$.belowthefold = function(element, settings) { | |
var fold = $(window).height() + $(window).scrollTop(); | |
return fold <= $(element).offset().top - settings.threshold; | |
}; | |
$.abovethetop = function(element, settings) { | |
var top = $(window).scrollTop(); | |
return top >= $(element).offset().top + $(element).height() - settings.threshold; | |
}; | |
$.rightofscreen = function(element, settings) { | |
var fold = $(window).width() + $(window).scrollLeft(); | |
return fold <= $(element).offset().left - settings.threshold; | |
}; | |
$.leftofscreen = function(element, settings) { | |
var left = $(window).scrollLeft(); | |
return left >= $(element).offset().left + $(element).width() - settings.threshold; | |
}; | |
$.inviewport = function(element, settings) { | |
var $element = $(element); | |
var offset = $element.offset(); | |
var $window = $(window); | |
var windowTop = $window.scrollTop(); | |
var threshold = settings.threshold; | |
if (offset.top - threshold < windowTop) { | |
if (offset.top + $element.height() + threshold >= windowTop) { | |
// top edge below the window's top | |
} else { | |
return false; | |
} | |
} else { | |
if (offset.top - threshold <= windowTop + $window.height()) { | |
// bottom edge above the window's bottom | |
} else { | |
return false; | |
} | |
} | |
var windowLeft = $window.scrollLeft(); | |
if (offset.left - threshold < windowLeft) { | |
if (offset.left + $element.width() + threshold >= windowLeft) { | |
// left edge be on the left side of the window's left edge | |
} else { | |
return false; | |
} | |
} else { | |
if (offset.left - threshold <= windowLeft + $window.width()) { | |
// right edge be on the right side of the window's right edge | |
} else { | |
return false; | |
} | |
} | |
return true; | |
}; | |
$.extend($.expr[':'], { | |
"below-the-fold": function(a, i, m) { | |
return $.belowthefold(a, {threshold : 0}); | |
}, | |
"above-the-top": function(a, i, m) { | |
return $.abovethetop(a, {threshold : 0}); | |
}, | |
"left-of-screen": function(a, i, m) { | |
return $.leftofscreen(a, {threshold : 0}); | |
}, | |
"right-of-screen": function(a, i, m) { | |
return $.rightofscreen(a, {threshold : 0}); | |
}, | |
"in-viewport": function(a, i, m) { | |
return $.inviewport(a, {threshold : 0}); | |
} | |
}); | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment