Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added image url and onerror, onload events on callbacks #47

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 32 additions & 32 deletions blazy.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*!
hey, [be]Lazy.js - v1.3.1 - 2015.02.01
hey, [be]Lazy.js - v1.3.1 - 2015.02.01
A lazy loading and multi-serving image script
(c) Bjoern Klinggaard - @bklinggaard - http://dinbror.dk/blazy
*/
Expand All @@ -10,20 +10,20 @@
} else if (typeof exports === 'object') {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
// like Node.
module.exports = blazy();
} else {
// Browser globals. Register bLazy on window
root.Blazy = blazy();
}
})(this, function () {
'use strict';

//vars
var source, options, viewport, images, count, isRetina, destroyed;
//throttle vars
var validateT, saveViewportOffsetT;

// constructor
function Blazy(settings) {
//IE7- fallback for missing querySelectorAll support
Expand Down Expand Up @@ -59,23 +59,23 @@
viewport.top = 0 - options.offset;
viewport.left = 0 - options.offset;
//throttle, ensures that we don't call the functions too often
validateT = throttle(validate, 25);
validateT = throttle(validate, 25);
saveViewportOffsetT = throttle(saveViewportOffset, 50);

saveViewportOffset();
saveViewportOffset();

//handle multi-served image src
each(options.breakpoints, function(object){
if(object.width >= window.screen.width) {
source = object.src;
return false;
}
});

// start lazy load
initialize();
initialize();
}

/* public functions
************************************/
Blazy.prototype.revalidate = function() {
Expand All @@ -97,7 +97,7 @@
images.length = 0;
destroyed = true;
};

/* private helper functions
************************************/
function initialize(){
Expand All @@ -116,9 +116,9 @@
bindEvent(window, 'scroll', validateT);
}
// And finally, we start to lazy load. Should bLazy ensure domready?
validate();
validate();
}

function validate() {
for(var i = 0; i<count; i++){
var image = images[i];
Expand All @@ -127,13 +127,13 @@
images.splice(i, 1);
count--;
i--;
}
}
}
if(count === 0) {
Blazy.prototype.destroy();
}
}

function loadImage(ele, force){
// if element is visible
if(force || (ele.offsetWidth > 0 && ele.offsetHeight > 0)) {
Expand All @@ -147,15 +147,15 @@
ele.removeAttribute(object.src);
});
ele.removeAttribute(options.src);
img.onerror = function() {
if(options.error) options.error(ele, "invalid");
img.onerror = function(event) {
if(options.error) options.error(ele, "invalid", src, event);
ele.className = ele.className + ' ' + options.errorClass;
};
img.onload = function() {
};
img.onload = function(event) {
// Is element an image or should we add the src as a background image?
ele.nodeName.toLowerCase() === 'img' ? ele.src = src : ele.style.backgroundImage = 'url("' + src + '")';
ele.className = ele.className + ' ' + options.successClass;
if(options.success) options.success(ele);
ele.nodeName.toLowerCase() === 'img' ? ele.src = src : ele.style.backgroundImage = 'url("' + src + '")';
ele.className = ele.className + ' ' + options.successClass;
if(options.success) options.success(ele, src, event);
};
img.src = src; //preload image
} else {
Expand All @@ -164,10 +164,10 @@
}
}
}

function elementInView(ele) {
var rect = ele.getBoundingClientRect();

return (
// Intersection
rect.right >= viewport.left
Expand All @@ -176,46 +176,46 @@
&& rect.top <= viewport.bottom
);
}

function isElementLoaded(ele) {
return (' ' + ele.className + ' ').indexOf(' ' + options.successClass + ' ') !== -1;
}

function createImageArray(selector) {
var nodelist = document.querySelectorAll(selector);
count = nodelist.length;
//converting nodelist to array
for(var i = count; i--; images.unshift(nodelist[i])){}
}

function saveViewportOffset(){
viewport.bottom = (window.innerHeight || document.documentElement.clientHeight) + options.offset;
viewport.right = (window.innerWidth || document.documentElement.clientWidth) + options.offset;
}

function bindEvent(ele, type, fn) {
if (ele.attachEvent) {
ele.attachEvent && ele.attachEvent('on' + type, fn);
} else {
ele.addEventListener(type, fn, false);
}
}

function unbindEvent(ele, type, fn) {
if (ele.detachEvent) {
ele.detachEvent && ele.detachEvent('on' + type, fn);
} else {
ele.removeEventListener(type, fn, false);
}
}

function each(object, fn){
if(object && fn) {
var l = object.length;
for(var i = 0; i<l && fn(object[i], i) !== false; i++){}
}
}

function throttle(fn, minDelay) {
var lastCall = 0;
return function() {
Expand All @@ -227,6 +227,6 @@
fn.apply(images, arguments);
};
}

return Blazy;
});
2 changes: 1 addition & 1 deletion blazy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.