Rinser/html/js/jquery.unveil.js

78 lines
2.5 KiB
JavaScript
Raw Normal View History

2015-08-07 12:48:37 +00:00
/**
* jQuery Unveil
* A very lightweight jQuery plugin to lazy load images
* http://luis-almeida.github.com/unveil
*
* Licensed under the MIT license.
* Copyright 2013 Luís Almeida
* https://github.com/luis-almeida
*/
2015-08-14 10:16:58 +00:00
;
(function($) {
2015-08-07 12:48:37 +00:00
2015-08-14 10:49:38 +00:00
var blackList = ['feeds.feedburner.com', '.feedsportal.co'];
2015-08-14 10:16:58 +00:00
$.fn.unveil = function(threshold, callback) {
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
var $w = $(window),
th = threshold || 0,
retina = window.devicePixelRatio > 1,
attrib = retina ? "data-src-retina" : "data-src",
images = this,
loaded;
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
this.one("unveil", function() {
var source = this.getAttribute(attrib);
source = source || this.getAttribute("data-src");
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
var flag = false;
2015-08-14 10:27:27 +00:00
for (var item in blackList) {
2015-08-14 10:36:09 +00:00
var u = blackList[item];
2015-08-14 10:40:42 +00:00
if (source.indexOf(u) !== -1) {
2015-08-14 10:16:58 +00:00
flag = true;
}
}
2016-03-15 22:28:40 +00:00
2015-08-14 10:16:58 +00:00
if (source && !flag) {
2016-03-15 22:28:40 +00:00
if (!S(source).startsWith('http')) {
source = 'http://' + S(source).stripLeft('/').s;
}
source = 'http://image.silvrtree.co.uk/1140,fit,q80/' + source;
2016-03-15 22:28:40 +00:00
2015-08-14 10:16:58 +00:00
this.setAttribute("src", source);
if (typeof callback === "function") callback.call(this);
}
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
if (flag) {
2015-08-14 10:36:09 +00:00
this.setAttribute("style", 'display:none !important;');
2015-08-14 10:16:58 +00:00
}
});
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
function unveil() {
var inview = images.filter(function() {
var $e = $(this);
// if ($e.is(":hidden")) return;
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
var wt = $w.scrollTop(),
wb = wt + $w.height(),
et = $e.offset().top,
eb = et + $e.height();
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
return eb >= wt - th && et <= wb + th;
});
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
loaded = inview.trigger("unveil");
images = images.not(loaded);
}
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
$w.on("scroll.unveil resize.unveil lookup.unveil", unveil);
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
unveil();
2015-08-07 12:48:37 +00:00
2015-08-14 10:16:58 +00:00
return this;
};
})(window.jQuery || window.Zepto);