Skip to content

Instantly share code, notes, and snippets.

@nurbek-ab
Forked from jumplee/isScrolledIntoView.js
Created October 7, 2015 06:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nurbek-ab/ed3241ba4d4754bc1019 to your computer and use it in GitHub Desktop.
Save nurbek-ab/ed3241ba4d4754bc1019 to your computer and use it in GitHub Desktop.
function isScrolledIntoView(elem)
{
var $elem = $(elem);
var $window = $(window);
var docViewTop = $window.scrollTop();
var docViewBottom = docViewTop + $window.height();
var elemTop = $elem.offset().top;
var elemBottom = elemTop + $elem.height();
return ((elemBottom <= docViewBottom) && (elemTop >= docViewTop));
}
@dlewand691
Copy link

Thank you for this!

Question about it.... how can I add a threshold to it so the change doesn't happen immediately after entering or leaving the viewport? For example, if the top of an element is up to 100px outside or the bottom is up to -100px inside the viewport, then trigger the change(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment