Skip to content

Instantly share code, notes, and snippets.

@adelpro
Forked from terkel/jquery.lazyload.js
Created February 9, 2019 14:29
Show Gist options
  • Select an option

  • Save adelpro/aea8615c15ec78af3aabc6b67edfa5ff to your computer and use it in GitHub Desktop.

Select an option

Save adelpro/aea8615c15ec78af3aabc6b67edfa5ff to your computer and use it in GitHub Desktop.
jQuery Lazy Load plugin
/*!
* jQuery Lazy Load plugin v0.9.2
* https://gist.github.com/1520842
*
* Copyright (c) 2012 Takeru Suzuki - http://terkel.jp/
* Licensed under the MIT license - http://www.opensource.org/licenses/MIT
*/
(function (window, $) {
$.fn.lazyLoad = function (options) {
var opts = $.extend({}, $.fn.lazyLoad.defaults, options);
return this.each(function () {
var $window = $(window),
$this = $(this).css({ opacity: 0 }),
didScroll = false;
$window.bind('scroll', function () {
didScroll = true;
});
setInterval(function () {
if (didScroll) {
var y = $window.scrollTop(),
h = $window.height(),
offsetTop = $this.offset().top;
if ((y + h) > (offsetTop - opts.threshold)) {
$this.animate({ opacity: 1 }, opts.duration, opts.easing);
}
didScroll = false;
}
}, 200);
$window.trigger('scroll');
});
}
$.fn.lazyLoad.defaults = {
threshold: 0,
duration: 400,
easing: 'swing'
};
})(window, jQuery);
/*!
* jQuery Lazy Load plugin v0.9.2
* https://gist.github.com/1520842
*
* Copyright (c) 2012 Takeru Suzuki - http://terkel.jp/
* Licensed under the MIT license - http://www.opensource.org/licenses/MIT
*/
(function(c,a){a.fn.lazyLoad=function(d){var e=a.extend({},a.fn.lazyLoad.defaults,d);return this.each(function(){var b=a(c),g=a(this).css({opacity:0}),f=!1;b.bind("scroll",function(){f=!0});setInterval(function(){if(f){var a=b.scrollTop(),c=b.height(),d=g.offset().top;a+c>d-e.threshold&&g.animate({opacity:1},e.duration,e.easing);f=!1}},200);b.trigger("scroll")})};a.fn.lazyLoad.defaults={threshold:0,duration:400,easing:"swing"}})(window,jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment