Skip to content

Instantly share code, notes, and snippets.

@vangog
Created January 13, 2014 19:21
Show Gist options
  • Select an option

  • Save vangog/8406331 to your computer and use it in GitHub Desktop.

Select an option

Save vangog/8406331 to your computer and use it in GitHub Desktop.
function showInViewport(elem, offset) {
if (document.documentElement.getBoundingClientRect) {
var left = parseInt(elem.css('left'), 10),
top = parseInt(elem.css('top'), 10);
elem.css('visibility', 'hidden').show();
var rect = elem[0].getBoundingClientRect();
if (rect.left < offset)
elem.css('left', offset - rect.left + left);
else if (rect.right > window.innerWidth - offset)
elem.css('left', window.innerWidth - rect.right - offset + left);
if (rect.top < offset)
elem.css('top', offset - rect.top + top);
else if (rect.bottom > window.innerHeight - offset)
elem.css('top', window.innerHeight - rect.bottom - offset + top);
elem.hide().css('visibility', 'visible');
}
elem.fadeIn(fadeSpeed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment