- return this.each(function() {
- var self = this;
- /* TODO: use .data() instead of .attr() */
- $(self).attr("original", $(self).attr("src"));
- if ("scroll" != settings.event
- || $.belowthefold(self, settings)
- || $.rightoffold(self, settings)) {
- if (settings.placeholder) {
- $(self).attr("src", settings.placeholder);
- } else {
- $(self).removeAttr("src");
- }
- self.loaded = false;
- } else {
- self.loaded = true;
- }
-
- /* When appear is triggered load original image. */
- $(self).one("appear", function() {
- if (!this.loaded) {
- $("<img />")
- .bind("load", function() {
- $(self)
- .hide()
- .attr("src", $(self).attr("original"))
- [settings.effect](settings.effectspeed);
- self.loaded = true;
- })
- .attr("src", $(self).attr("original"));
- };
- });
-
- /* When wanted event is triggered load original image */
- /* by triggering appear. */
- if ("scroll" != settings.event) {
- $(self).bind(settings.event, function(event) {
- if (!self.loaded) {
- $(self).trigger("appear");
+ function belowViewport(container, element, threshold) {
+ return $(container).offset().top + $(container).height() + threshold <= $(element).offset().top;
+ }
+
+ function checkScroll() {
+ if (Math.abs(container.scrollTop() - container.data('lazyload:lastCheckedScrollTop')) > settings.scrollThreshold) {
+ container.data('lazyload:lastCheckedScrollTop', container.scrollTop());
+
+ $(pattern, container).each(function() {
+ if (aboveViewport(container, this, settings.threshold)
+ || belowViewport(container, this, settings.threshold)) {
+ $(this).html(settings.placeholder);
+ } else {
+ $(this).html('');
+ var self = this;
+ $('<img src="' + $(this).attr('src') + '" width="' + $(this).width() + '" />').load(function() {
+ if ($(this).height() > $(self).height()) {
+ $(self).height($(this).height());
+ }
+ }).appendTo(this);