Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Check if element is visible in viewport and scroll if necessary
$.extend $.expr[":"],
inView: (a) ->
# cf.
st = (document.documentElement.scrollTop or document.body.scrollTop)
ot = $(a).offset().top
wh = (if (window.innerHeight and window.innerHeight < $(window).height()) then window.innerHeight else $(window).height())
ot > st and ($(a).height() + ot) < (st + wh)
$.fn.scroll_if_necessary = ->
if this.length && not @is(":inView")
$("body").animate({scrollTop: $(this).offset().top}, 200)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment