Created
June 4, 2013 15:10
-
-
Save githiro/5706653 to your computer and use it in GitHub Desktop.
JS: smaller lazy-load inspired by lazyload
This file contains hidden or 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
| (function($, window, document, undefined) { | |
| $.fn.lazyload = function(options) { | |
| var $window = $(window), | |
| elements = this, | |
| settings = { | |
| threshold : 0, | |
| fadein_speed : "200", | |
| container : window, | |
| data_attribute : "src", | |
| appear : null, | |
| load : null | |
| }, | |
| containerIsWindow = settings.container === undefined || settings.container === window, | |
| $container = containerIsWindow ? $window : $(settings.container); | |
| if(options) $.extend(settings, options); | |
| function belowthefold(element, settings) { | |
| var fold; | |
| if (containerIsWindow) { | |
| fold = window.innerHeight + (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); | |
| } else { | |
| fold = $(settings.container).offset().top + $(settings.container).height(); | |
| } | |
| return fold <= $(element).offset().top - settings.threshold; | |
| }; | |
| function abovethetop(element, settings) { | |
| var fold; | |
| if (containerIsWindow) { | |
| fold = (window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop); | |
| } else { | |
| fold = $(settings.container).offset().top; | |
| } | |
| return fold >= $(element).offset().top + settings.threshold + $(element).height(); | |
| }; | |
| function update() { | |
| elements.each(function() { | |
| var $this = $(this); | |
| if (abovethetop(this, settings)) { | |
| /* Nothing. */ | |
| } else if (!belowthefold(this, settings) ) { | |
| $this.trigger("appear"); | |
| } | |
| }); | |
| }; | |
| $container.bind("scroll", function() { | |
| return update(); | |
| }); | |
| $window | |
| .bind("resize", function() { | |
| update(); | |
| }) | |
| .load(function() { | |
| update(); | |
| }); | |
| if ((/iphone|ipod|ipad.*os 5/gi).test(navigator.appVersion)) { | |
| $window.bind("pageshow", function(event) { | |
| if (event.originalEvent.persisted) { | |
| elements.each(function() { | |
| $(this).trigger("appear"); | |
| }); | |
| } | |
| }); | |
| } | |
| return this.each(function() { | |
| var self = this, | |
| $self = $(self); | |
| self.loaded = false; | |
| $self | |
| .one("appear", function() { | |
| if (!this.loaded) { | |
| if (settings.appear) { | |
| var elements_left = elements.length; | |
| settings.appear.call(this, elements_left, settings); | |
| } | |
| var img = new Image, | |
| src = $self.data(settings.data_attribute); | |
| img.onload = function() { | |
| $self | |
| .css("opacity", .001) | |
| .attr("src",src); | |
| setTimeout(function() { | |
| $self.animate({opacity: 1}, settings.fadein_speed); | |
| }, 0); | |
| self.loaded = true; | |
| /* Remove image from array so it is not looped next time. */ | |
| var temp = $.grep(elements, function(element) { | |
| return !element.loaded; | |
| }); | |
| elements = $(temp); | |
| if (settings.load) { | |
| var elements_left = elements.length; | |
| settings.load.call(self, elements_left, settings); | |
| } | |
| }; | |
| img.onerror = function() { | |
| //Write your error handling code here. | |
| }; | |
| img.src = src;//Consequently, this fires onload. | |
| } | |
| }) | |
| .bind("scroll", function() { | |
| if (!self.loaded) $self.trigger("appear"); | |
| }); | |
| }); | |
| }; | |
| })(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment