Skip to content

Instantly share code, notes, and snippets.

@geta6
Created November 9, 2013 03:09
Show Gist options
  • Save geta6/7381063 to your computer and use it in GitHub Desktop.
Save geta6/7381063 to your computer and use it in GitHub Desktop.
# https://github.com/luis-almeida/unveil/blob/master/jquery.unveil.js
$.fn.unveil = (th = 0, callback = ->) ->
$w = $ window
images = $ @
@one 'unveil', ->
if (source = @getAttribute 'data-src')
@setAttribute 'src', source
callback()
unveil = =>
inview = @filter ->
$e = $ @
return if $e.is ':hidden'
wt = $w.scrollTop()
wb = wt + $w.height()
et = $e.offset().top
eb = et + $e.height()
return eb >= wt - th and et <= wb + th
loaded = inview.trigger 'unveil'
images = images.not loaded
$w.scroll unveil
$w.resize unveil
setTimeout unveil, 200
return @
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment