DLL.js lazy loaded a 4K image then triggered a callback function. Here's what was actually in place:
// when finished do some callback
function imageLoadFinished() {
// doin some cool stuff, you gonna check the scripts.js
}
// we lazy load to the .cover class element
DLL('.cover',imageLoadFinished);
Unlike the examples below, this is an example where the target element has no child elements and is the only subject to DLL lazy load.
In this example, we target the parent .testCol1
element with our function, and DLL lazy loads it's inner elements.
What happened here? DLL.js lazy loaded an element's children and triggered a function when last item loaded. The code looks like this:
//listening to button click
var btn = document.querySelector('.btn');
// the target element
var el = document.querySelector('.testCol1');
// when load is complete, we run this callback function
function callback(){
// do some animation for the element and all children having data-src attribute
// add loaded class
el.classList.add('loaded')
}
// the click handler
function clickExample(){
// if element is not loaded, we initiate dll.js
if ( !el.classList.contains('loaded') ){
DLL(el, callback);
}
}
// you know the thing
btn.addEventListener('click', clickExample, false);
In this example, we target the parent .img-wrapper
element with our function, and DLL lazy loads it and it's inner elements. And now we'll scroll down a bit.
What happened here? DLL.js lazy loaded an element with all it's children and triggered a function when last item loaded. The code looks like this:
// the target element
var el = document.querySelector('.testCol2');
// when load is complete, run this callback function
function callback(){
//do some animation for the element's children having data-src attribute
//add a class to parent element, to make sure things only work once
el.classList.add('.loaded');
}
// your scroll handler
function scrollHandler(){
//when element is in viewport, we initiate dll.js
if ( isElementInViewport(el) && !el.classList.contains('loaded') ){
DLL(el, callback);
}
}
// another listener, yeey
window.addEventListener('scroll', scrollHandler, false);
Grab the isElementInViewport
right here.
Similar to the above example, but this time we load a bunch of videos. Check notes below to get a glimpse on how it works:
DLL.js will only target videos with the following structure:
<video id="myVideo" poster="video/my-video.jpg">
<source data-src="video/my-video.mp4" type="video/mp4">
</video>
If your content has the right structure, you can do everything else just like the usual:
// grab a video
var myVideo = document.getElementById('myVideo');
// initialize for a single video element
DLL(myVideo, callback);
// OR
// grab a wrapper element
var myWrapper = document.getElementById('myWrapper');
// initialize for and element and/or all child elements
DLL(myWrapper, callback);
Both of the above produce the same effect.
data-src
attribute, ideally an <img>
or a layer element with no child elements<img>
and other elements as well, to update the background-image
data-src
attributeDLL();
elementInViewport
function you can lazy load elements on scroll, like the example abovesrc
attribute and another one visible with data-src
attributeDLL();
or DLL('body');
unless you really want to do that, you may want to change what happens with each element alonelazy loading a random 4K image...