Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
64 additions
and
66 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,69 +1,67 @@ | ||
(function($) { | ||
this.LazyLoad = new Class({ | ||
var LazyLoad = new Class({ | ||
|
||
Implements: [Options,Events], | ||
|
||
/* additional options */ | ||
options: { | ||
range: 200, | ||
image: 'blank.gif', | ||
resetDimensions: true, | ||
elements: 'img', | ||
container: window | ||
}, | ||
|
||
/* initialize */ | ||
initialize: function(options) { | ||
|
||
Implements: [Options,Events], | ||
|
||
/* additional options */ | ||
options: { | ||
range: 200, | ||
image: 'blank.gif', | ||
resetDimensions: true, | ||
elements: 'img', | ||
container: window | ||
}, | ||
|
||
/* initialize */ | ||
initialize: function(options) { | ||
|
||
/* vars */ | ||
this.setOptions(options); | ||
this.container = $(this.options.container); | ||
this.elements = $$(this.options.elements); | ||
this.containerHeight = this.container.getSize().y; | ||
this.start = 0; | ||
/* vars */ | ||
this.setOptions(options); | ||
this.container = document.id(this.options.container); | ||
this.elements = $$(this.options.elements); | ||
this.containerHeight = this.container.getSize().y; | ||
this.start = 0; | ||
|
||
/* find elements remember and hold on to */ | ||
this.elements = this.elements.filter(function(el) { | ||
/* reset image src IF the image is below the fold and range */ | ||
if(el.getPosition(this.container).y > this.containerHeight + this.options.range) { | ||
el.store('oSRC',el.get('src')).set('src',this.options.image); | ||
if(this.options.resetDimensions) { | ||
el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''}); | ||
} | ||
return true; | ||
} | ||
},this); | ||
|
||
/* find elements remember and hold on to */ | ||
this.elements = this.elements.filter(function(el) { | ||
/* reset image src IF the image is below the fold and range */ | ||
if(el.getPosition(this.container).y > this.containerHeight + this.options.range) { | ||
el.store('oSRC',el.get('src')).set('src',this.options.image); | ||
if(this.options.resetDimensions) { | ||
el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''}); | ||
/* create the action function */ | ||
var action = function() { | ||
var cpos = this.container.getScroll().y; | ||
if(cpos > this.start) { | ||
this.elements = this.elements.filter(function(el) { | ||
if((this.container.getScroll().y + this.options.range + this.containerHeight) >= el.getPosition(this.container).y) { | ||
if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); } | ||
if(this.options.resetDimensions) { | ||
el.set({ | ||
width: el.retrieve('oWidth'), | ||
height: el.retrieve('oHeight') | ||
}); | ||
} | ||
this.fireEvent('load',[el]); | ||
return false; | ||
} | ||
return true; | ||
} | ||
},this); | ||
|
||
/* create the action function */ | ||
var action = function() { | ||
var cpos = this.container.getScroll().y; | ||
if(cpos > this.start) { | ||
this.elements = this.elements.filter(function(el) { | ||
if((this.container.getScroll().y + this.options.range + this.containerHeight) >= el.getPosition(this.container).y) { | ||
if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); } | ||
if(this.options.resetDimensions) { | ||
el.set({ | ||
width: el.retrieve('oWidth'), | ||
height: el.retrieve('oHeight') | ||
}); | ||
} | ||
this.fireEvent('load',[el]); | ||
return false; | ||
} | ||
return true; | ||
},this); | ||
this.start = cpos; | ||
} | ||
this.fireEvent('scroll'); | ||
/* remove this event IF no elements */ | ||
if(!this.elements.length) { | ||
this.container.removeEvent('scroll',action); | ||
this.fireEvent('complete'); | ||
} | ||
}.bind(this); | ||
|
||
/* listen for scroll */ | ||
this.container.addEvent('scroll',action); | ||
} | ||
}); | ||
})(document.id); | ||
},this); | ||
this.start = cpos; | ||
} | ||
this.fireEvent('scroll'); | ||
/* remove this event IF no elements */ | ||
if(!this.elements.length) { | ||
this.container.removeEvent('scroll',action); | ||
this.fireEvent('complete'); | ||
} | ||
}.bind(this); | ||
|
||
/* listen for scroll */ | ||
this.container.addEvent('scroll',action); | ||
} | ||
}); |