-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazyLoading.coffee
30 lines (30 loc) · 1.08 KB
/
jquery.lazyLoading.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
( ($) ->
$.fn.lazyLoading = (options) ->
settings = $.extend({}, {sourceAttr:"data-src",effectTime:1000}, options)
$this = this
checking = false
checkImages = ->
if checking
return
checking = true
$($this)
.filter (i)->
$($this[i]).attr(settings.sourceAttr) isnt undefined
.each (i,v) ->
rect = v.getBoundingClientRect()
if rect.top >= 0 and rect.left >= 0 and rect.top <= (window.innerHeight || document.documentElement.clientHeight)
$(v).css("opacity",0)
src = v.getAttribute(settings.sourceAttr)
v.removeAttribute(settings.sourceAttr)
v.src = src
$(v).animate({"opacity":1},settings.effectTime)
return
checking = false
return
checkImages()
$(window).scroll ->
checkImages()
return
return this
return
)(jQuery)