Scroll the page down to gradually reveal images via lazy load. If you are
interested in HTML5 Web Components
you may want to check a similar example,
wrapped as a custom HTML tag.
'
]
},
/*
* Scroll threshold
* @number threshold
* @public
**/
threshold: 0,
/*
* Application root
* @string appRoot path to the AcidJs.ImgLazyload folder
* @public
**/
appRoot: "",
/*
* Image width
* @number imageWidth default image width
* @public
**/
imageWidth: 480,
/*
* Image height
* @number imageWidth default image height
* @public
**/
imageHeight: 360,
/*
* URLS used by the class
* @object URLS
* @public
**/
URLS: {
baseCss: "AcidJs.ImgLazyload/styles/ImgLazyload.css"
},
/*
* HTML attributes used by the class
* @object ATTRS
* @public
**/
ATTRS: {
src: "data-src"
},
/*
* CSS classes used by the class
* @object CSS_CLASSES
* @public
**/
CSS_CLASSES: {
base: "acidjs-imglazyload",
item: "acidjs-imglazyload-img"
},
/*
* Initialize the class
* @method init
* @public
**/
init: function() {
this.loadCss("baseCss");
this.bind();
this.lazyLoadImages(); // support server-rendered image lists automatically
},
/*
* Request JSON data file, containing an image list
* @method loadImages
* @public
* @param config
* {
* boundingBox: {jQueryDomObject} [required] HTML DOM node which will be used to render the list
* url: {String} [required] path to the data file
* imageWidth: {String} [optional] default: 480
* imageHeight: {String} [optional] default: 360
* }
* @return void
**/
loadImages: function(config) {
var
that = this;
$.ajax({
url: config.url,
dataType: "json",
success: function(data) {
config.images = data.images;
that.renderList(config);
},
error: function() {
//@todo handle error
}
});
},
/*
* Render an image list
* @method renderList
* @public
* @param config
* {
* boundingBox: {jQueryDomObject} [required] HTML DOM node which will be used to render the list
* url: {String} [required] path to the data file
* imageWidth: {String} [optional] default: 480
* imageHeight: {String} [optional] default: 360,
* images: [{
* src: {String} path to the image
* caption: {String} [optional] image caption
* }]
* }
* @return void
**/
renderList: function(config) {
var
html = [],
that = this,
boundingBox = config.boundingBox;
if(!boundingBox.length) {
//@todo display message
return;
}
$.each(config.images, function(i) {
var
image = config.images[i];
html.push(that.compile("base", {
src: image.src || "",
caption: image.caption || "",
appRoot: that.appRoot,
imageWidth: config.imageWidth || that.imageWidth,
imageHeight: config.imageHeight || that.imageHeight
}));
});
html = html.join("");
boundingBox.html("
" + html + "
").addClass(this.CSS_CLASSES.base);
this.lazyLoadImages();
},
/*
* Compile a template and return the HTML
* @method compile
* @public
* @param name {String} [required] name of the template that will be used
* @param data {Object} [required] key/value pairs of template placeholders and values that should be replaced against these placeholders
* @return String
**/
compile: function(name, data) {
var
html = this.TEMPLATES[name] || [];
data = data || {};
html = html.join("");
for(var key in data) {
if(data.hasOwnProperty(key)) {
var
value = data[key],
regexp = new RegExp("{{" + key + "}}", "g");
html = html.replace(regexp, value);
}
}
return html;
},
/*
* Class event handlers
* @method bind
* @public
* @return void
**/
bind: function() {
var
that = this;
WINDOW.bind("scroll", function() {
that.lazyLoadImages();
});
},
/*
* Lazily load images
* @method lazyLoadImages
* @public
* @return void
**/
lazyLoadImages: function() {
var
that = this,
attrs = this.ATTRS,
src = attrs.src,
images = $("img[" + src + "]");
images.each(function() {
var
image = $(this);
if(that.isImageIntoView(image)) {
image.attr("src", image.attr(src))
.removeAttr(src);
}
});
},
/*
* Check if an image is into viewport
* @method isImageIntoView
* @public
* @param image {jQueryDomObject}
* @return Boolean
**/
isImageIntoView: function(image) {
var
threshold = this.threshold,
documentViewTop = WINDOW.scrollTop() + threshold,
documentViewBottom = documentViewTop + WINDOW.height(),
elementTop = image.offset().top - threshold,
elementBottom = elementTop + image.height();
return ((elementBottom <= documentViewBottom) && (elementTop >= documentViewTop));
},
/*
* Asynchronously load a CSS file from the server
* @method loadCss
* @param name {String} this.URLS[name]
* @private
* @return void
**/
loadCss: function(name) {
var
css = document.createElement("link"),
id = this.CSS_CLASSES.base + "-" + name.toLowerCase() + "-css";
css.setAttribute("rel", "stylesheet");
css.setAttribute("href", this.appRoot + this.URLS[name]);
css.setAttribute("id", id);
if($("#" + id).length <= 0) {
document.getElementsByTagName("head")[0].appendChild(css);
}
}
};
/*
* add to the window.AcidJs namespace
**/
window.AcidJs.ImgLazyload = ImgLazyload;
})();
This website uses cookies to ensure you get the best browsing experience.
By using it you agree to my cookie handling policy. Love this bar?
Get it here for free.
Please, disable your ad blocker
This website is made possible by displaying online advertisements to the visitors.
To view the content, please, disable your ad blocker,
then refresh the page.