- Overview
- Documents
bLazy is a lightweight lazy loading and multi-serving image script. It's written in JavaScript why it doesn't have any dependencies for 3rd party libraries like jQuery. bLazy is working in all modern browser including IE7+
WHY BE LAZY?
- bLazy is used on big sites with millions of monthly visitors so it has been tested out in the real world.
- bLazy is written in pure JavaScript why it doesn’t depend on 3rd-party libraries such as jQuery.
- bLazy can lazy load all types of images including background images.
- bLazy can lazy load images depending on screensize (multi-serve images).
- bLazy can serve retina images on retina devices.
- bLazy is lightweight, less than 1.2KB and less than 1.05KB if you don't need IE7- support.
- bLazy is AMD compatible.
-
sex shop
sex shop
sex shop
sex shop
sex shop
seks shop
spanish fly
psikolog
sohbet numara
sohbet hatti
Source: dinbror.dk
1. INCLUDE JS FILE
<script src="blazy.min.js"></script>
2. HTML
<img class="b-lazy" src="placeholder-image.jpg" data-src="image.jpg" data-src-small="small-image.jpg" alt="Image description" />
3. JAVASCRIPT
var bLazy = new Blazy({ breakpoints: [{ width: 420 // Max-width , src: 'data-src-small' }] , success: function(element){ setTimeout(function(){ // We want to remove the loader gif now. // First we find the parent container // then we remove the "loading" class which holds the loader image var parent = element.parentNode; parent.className = parent.className.replace(/\bloading\b/,''); }, 200); } });
4. OPTIONS
Options– key [type] (default value) | |
breakpoints[array] (false) | Multi-serve images based on screen size. |
container [string](window) | If you want to lazy load images inside a scrolling container change the default value to the selector of the container |
error [function(ele, msg)] (false) | Callback for when something goes wrong. There are two error messages, missing and invalid. You’ll get missing if no data-src is defined. Invalid if the data-src is invalid. |
errorClass [string](‘b-error’) | The classname an image will get if something goes wrong. |
offset [int] (100) | The offset controls how early you want the images to be loaded before they’re visible. Default is 100, so 100px before an image is visble it’ll start loading. |
separator [char](‘|’) | Used if you want to pass retina images: data-src=”image.jpg|[email protected]”. |
success [function(ele)] (false) | Callback for when an image has loaded. |
successClass [string](‘b-loaded’) | The classname an image will get when loaded. |
selector[string] (‘.b-lazy’) | Image selector for images that should lazy load. If you want to lazy load all images write ‘img’. You can add multiple selectors separated with comma; ‘.b-lazy, .bLazy, .blazy’. |
src [string] (‘data-src’) | Attribute where the original image source can be found |
You pass the options as an object of key/value pairs:
// Format var bLazy = new Blazy({ key: value , key: value , key: value });
Selector
You can change the selector if you don’t want to add the ‘b-lazy’ class or if you need to have multiple.
// Example var bLazy = new Blazy({ selector: 'img' // all images });
Offset
The offset controls how early you want the images to be loaded before they’re visible. Default is 100, so 100px before an image is visible it’ll start loading.
// Example var bLazy = new Blazy({ offset: 100 // Loads images 100px before they're visible });
Images inside a container
You can also lazy load images inside a scrolling container, just define the selector of the container:
// Example var bLazy = new Blazy({ container: '#scrolling-container' // Default is window });
Callback when image has loaded or fails
If you need to do anything when an image has loaded or fails you can pass a callback function:
// Example var bLazy = new Blazy({ success: function(ele){ // Image has loaded // Do your business here } , error: function(ele, msg){ if(msg === 'missing'){ // Data-src is missing } else if(msg === 'invalid'){ // Data-src is invalid } } });
Retina images
If you’re not doing retina-first don’t worry. It’s easy to serve retina images for retina displays. Just add the source to the retina image in the data-src by using the separator (default is ‘|’) and bLazy will do the rest:
<img class="b-lazy" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src="image.jpg|retina-image.jpg" alt="alt-text" />
Background images
You can also lazy load background images. If the element with the lazy load class (default: ‘.b-lazy’) isn’t an image, the source will be added to the element as a background image:
<div class="b-lazy" data-src="background-image.jpg"</div>
Multi-serve images
You can multi-serve images, so users on smaller devices will get a smaller image served and the page will load faster. If you have more than one it’s important that the widths are ascending like in my example; 420 comes before 768. If you set up a multi rule but your image markup doesn’t have the src attribute on it, it’ll look after the default src, data-src.
// Example var bLazy = new Blazy({ breakpoints: [{ width: 420 // max-width , src: 'data-src-small' } , { width: 768 // max-width , src: 'data-src-medium' }] });
Image markup:
<img class="b-lazy" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src="image.jpg" data-src-small="image-small.jpg" data-src-medium="image-medium.jpg" alt="alt-text" />
Image transitions
Image transitions are not a built-in feature in bLazy.js but you can easily add it with css. When an image has loaded a loaded class (default: b-loaded) is added to the image, so you can add:
.b-lazy { -webkit-transition: opacity 500ms ease-in-out; -moz-transition: opacity 500ms ease-in-out; -o-transition: opacity 500ms ease-in-out; transition: opacity 500ms ease-in-out; max-width: 100%; opacity: 0; } .b-lazy.b-loaded { opacity: 1; }
5. PUBLIC FUNCTIONS
Public functions | |
revalidate() | Revalidates document for visible images. Useful if you add images with scripting or ajax |
load(element) | Forces the given element (image) to load |
destroy() | Unbind events and resets image array |
// Example var bLazy = new Blazy(); bLazy.functionName(); // eg bLazy.revalidate();
6. RESPONSIVE IMAGES
An example on how to lazy load and multi-serve responsive images without having the page reflow.
Markup:
<div class="image-wrapper ratio_16-9"> <img class="b-lazy" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== data-src="image.jpg" data-src-small="image-small.jpg" alt="alt-text" /> <!-- Fallback for non JavaScript browsers --> <noscript><img src="image.jpg" alt="alt-text" /></noscript> </div>
CSS:
.image-wrapper { // Adding a loader and background color. The user will see it // if the image is loading slow. background: #1E1E1E url('loader.gif') center center no-repeat; width: 100% } .ratio_16-9 { // The image has a 16/9 ratio. Until the image has loaded // we need to reserve some space so the page won't reflow. // How to calculate the space (padding-bottom): 9/16*100 = 56.25 // Another example: you have an image 400x250. // So if you want to calculate the space you do: 250/400*100 = 62.5 padding-bottom: 56.25%; height: 0; } .b-lazy { max-width: 100%; }