Skip to content

Commit d734ea4

Browse files
committed
Code cleanup, demo update, added DLL.js to npm.
1 parent b81234b commit d734ea4

File tree

13 files changed

+214
-100
lines changed

13 files changed

+214
-100
lines changed

README.md

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
# Double Lazy Load | dll.js
2-
Double Lazy Load for images and background images. The name of the script lies in what it does, so check below or see the demo.
2+
Double Lazy Load for images and background images.
33

4+
# CDN
5+
Thanks to jsdelivr, we have CDN link <a href="http://www.jsdelivr.com/#!dll.js">here</a>.
6+
7+
# npm
8+
```
9+
npm install dll.js
10+
```
11+
12+
# Demo
13+
Right <a href="http://thednp.github.io/dll.js/">here</a>
14+
15+
# Base syntax
16+
```js
17+
new dll('selector',callback);
18+
```
19+
420
# What it does
521
* Do lazy loading for an element that is subject to lazy load via data-src, or
622
* Do to all child items of a given element
723
* Do <code>backgroundImage</code> to elements other than <code>&lt;img&gt;</code> if they have <code>data-src</code> attribute.
824
* Do callback when load event is triggered for one element, or for the last child element of a given parent.
925
* Do lazy load for all items having <code>data-src</code> attribute.
1026

11-
# CDN
12-
Thanks to jsdelivr, we have CDN link <a href="http://www.jsdelivr.com/#!dll.js">here</a>.
27+
# Works with
28+
Any valid selector or no selector.
29+
```js
30+
new dll('.uniqueClassName'); // lazy loads an element with a given class and it's children if any have data-src
1331

14-
# Demo
15-
Right <a href="http://thednp.github.io/dll.js/">here</a>
32+
new dll('#uniqueID'); // lazy loads an element with a given ID and it's children if any have data-src
1633

17-
# Base usage
18-
<pre>new dll('selector',callback);</pre>
19-
20-
<b>Works with</b> any valid selector or no selector.
21-
<pre>new dll('validSelector'); // run dll for an element with any valid selector #myElement / .myElementClass</pre>
22-
<pre>new dll(); // lazy loads any items with data-src from the entire page</pre>
34+
new dll(); // lazy loads any items with data-src from the entire page
35+
```
2336

2437
# Other examples
25-
If you link the script in the site <code>&lt;head&gt;</code>, you should do this
26-
<pre>
38+
If your script is in your site head, you should do this
39+
```js
2740
window.addEventListener('load', loadFunctionExample, false);
2841
function loadFunctionExample(){
2942
new dll('[data-src]', myFunction);
3043
}
31-
</pre>
44+
```
3245

3346
If you want to lazy load on scroll
34-
<pre>
47+
```js
3548
window.addEventListener('scroll', scrollExample, false);
3649
function scrollExample(){
3750
var el = document.getElementById('myItem');
@@ -42,18 +55,17 @@ window.addEventListener('scroll', scrollExample, false);
4255
//do some stuff when loading finished
4356
}
4457
}
45-
</pre>
58+
```
4659

4760
# A nasty example
48-
Lazy load a parent <code>&lt;div id="myElement" data-src="..image.png"&gt;</code> with many elements inside subject to dll.js object:
49-
<pre>
61+
Lazy load a parent `<div id="myElement" data-src="..image.png">` with many elements inside subject to dll.js object:
62+
```js
5063
var el = document.getElementById('myElement'); //this is a parent
5164
new dll(el, callback)
5265
function callback(){
5366
console.log('I just finished lazy loading the last element for #myElement')
5467
}
55-
56-
</pre>
68+
```
5769

5870
#License
5971
<a href="https://github.com/thednp/dll.js/blob/master/LICENSE">MIT License</a>
Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
// utility
2+
var elementInViewport = function(el) { //check element is in viewport
3+
var rect = el.getBoundingClientRect();
4+
return (
5+
rect.top >= 0
6+
&& rect.left >= 0
7+
&& rect.top <= (window.innerHeight || document.documentElement.clientHeight)
8+
)
9+
};
10+
111
//generate a random image from http://dummy-image-generator.com/
212

313
var fl = document.querySelector('.fill'),
@@ -66,22 +76,23 @@ for ( var i=0, itl=itms2.length; i<itl; i++ ){
6676
//some DLL demo
6777
new dll('.cover',pageLoadFinished);
6878
function pageLoadFinished() {
69-
var loader = document.querySelector('.page-loader'), loaderImg = loader.getElementsByTagName('IMG')[0], isIE = document.documentElement.classList.contains('ie');
79+
var loader = document.querySelector('.page-loader'), loaderImg = loader.getElementsByTagName('IMG')[0],
80+
isIE = document.documentElement.classList.contains('ie');
7081
setTimeout(function() {
71-
new KUTE.Animate(loaderImg, {from: { opacity:1 }, to: { opacity:0}, duration: 1500, easing: 'linear' });
82+
KUTE.fromTo(loaderImg, { opacity:1 }, { opacity:0}, { duration: 1500, easing: 'linear'}).start();
7283
loader.getElementsByTagName('P')[0].innerHTML = 'Done, now other examples..'
7384
}, 500);
7485
setTimeout(function() {
7586
//play some KUTE tweens
7687
if ( !isIE ) {
77-
new KUTE.Animate(loader, {from: { translate: {y: 0}, opacity:1 }, to: { translate: {y: -1000}, opacity:0}, duration: 700, easing: 'exponentialIn' });
78-
new KUTE.Animate('.site-wrapper', {from: { translate: { y: 550 }, opacity: 0 }, to: { translate: { y: 0}, opacity: 1 }, delay: 700, duration: 1200, easing: 'exponentialInOut' })
88+
KUTE.fromTo(loader,{ translate: [0,0], opacity:1 }, { translate: [0,-1000], opacity:0}, { duration: 700, easing: 'easingExponentialIn' }).start();
89+
KUTE.fromTo('.site-wrapper', { translate: [0,550], opacity: 0 }, { translate: [0,0], opacity: 1 }, { delay: 700, duration: 1200, easing: 'easingExponentialInOut' }).start()
7990
} else {
80-
new KUTE.Animate(loader, {from: { position: {top: '50%'}, opacity:1 }, to: { position: {top: '-5%'}, opacity:0}, duration: 700, easing: 'exponentialIn' });
81-
new KUTE.Animate('.site-wrapper', {from: { position: {top: 550}, opacity: 0 }, to: { position: { top: 0}, opacity: 1 }, delay: 700, duration: 1200, easing: 'exponentialInOut' })
91+
KUTE.fromTo(loader, { position: {top: '50%'}, opacity:1 }, { position: {top: '-5%'}, opacity:0}, { duration: 700, easing: 'easingExponentialIn' }).start();
92+
KUTE.fromTo('.site-wrapper', { position: {top: 550}, opacity: 0 }, { position: { top: 0}, opacity: 1 }, { delay: 700, duration: 1200, easing: 'easingExponentialInOut' }).start()
8293
}
83-
new KUTE.Animate('.cover', {from: { opacity: 0 }, to: { opacity: 1 }, delay: 2000, duration: 2000, easing: 'exponentialIn' })
84-
new KUTE.Animate('.navbar-wrapper', {from: { opacity: 0 }, to: { opacity: 1 }, delay: 4000, duration: 2000, easing: 'linear' })
94+
KUTE.fromTo('.cover', { opacity: 0 }, { opacity: 1 }, { delay: 2000, duration: 2000, easing: 'easingExponentialIn' }).start()
95+
KUTE.fromTo('.navbar-wrapper', { opacity: 0 }, { opacity: 1 }, { delay: 4000, duration: 2000, easing: 'linear' }).start()
8596
}, 3000)
8697
}
8798

@@ -105,7 +116,7 @@ function clickExample(e){
105116
for ( var i = 0; i<il; i++ ) {
106117
var dl = 350*i;
107118
fn = i===(il-1) ? finished : '';
108-
new KUTE.Animate(itms[i], {from: { scale: 1.5, opacity: 0 }, to: { scale: 1, opacity: 1 }, delay: dl, duration: 700, easing: 'exponentialOut', finish: fn });
119+
KUTE.fromTo(itms[i], { scale: 1.5, opacity: 0 }, { scale: 1, opacity: 1 }, { delay: dl, duration: 700, easing: 'easingExponentialOut', finish: fn }).start();
109120
}
110121
btn.innerHTML = 'Animating...';
111122
function finished(){
@@ -128,22 +139,23 @@ function scrollExample(){
128139
if ( !el.classList.contains('loaded') ){
129140
el.classList.add('loaded');
130141
var itms = el.getElementsByTagName('IMG'), il = itms.length;
131-
new KUTE.Animate(el, {from: { opacity: 0 }, to: { opacity: 1 }, delay: 500, duration: 2000, easing: 'exponentialOut' });
142+
KUTE.fromTo(el, { opacity: 0 }, { opacity: 1 }, { delay: 500, duration: 2000, easing: 'easingExponentialOut' }).start();
132143
for ( var i = 0; i<il; i++ ) {
133144
var dl = parseInt(250*i + 2500);
134-
new KUTE.Animate(itms[i], {from: { scale: 0.2, opacity: 0 }, to: { scale: 1, opacity: 1 }, delay: dl, duration: 500, easing: 'backOut' });
145+
KUTE.fromTo(itms[i], { scale: 0.2, opacity: 0 }, { scale: 1, opacity: 1 }, { delay: dl, duration: 500, easing: 'easingBackOut' }).start();
135146
}
136147
}
137148
}
138149
}
139150

140151
//scroll top?
141-
var toTop = document.getElementById('toTop');
152+
var toTop = document.getElementById('toTop'),
153+
toTopTween = KUTE.to( 'window', { scroll: 0 }, { easing: 'easingQuarticOut', duration : 1500 } );
142154
toTop.addEventListener('click',topHandler,false);
143155

144156
function topHandler(e){
145157
e.preventDefault();
146-
new KUTE.Animate( 'div', { to : { scroll: 0 }, easing: 'quarticOut', duration : 1500 } );
158+
toTopTween.start();
147159
}
148160

149161
//syntax highlighter

build.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// DLL.js | Minify
2+
// Minify script for the js files in root folder
3+
// Usage: npm run min
4+
5+
var fs = require('fs');
6+
var path = require('path');
7+
var uglify = require('uglify-js');
8+
var pack = require('./package.json');
9+
var version = 'v'+pack.version;
10+
var license = pack.license+'-License';
11+
12+
console.log('Minifying DLL.js ' + version + '..');
13+
14+
// Helper Functions:
15+
function minify(srcPath, writePath) {
16+
fs.writeFile(writePath,
17+
('// DLL.js ' + version + ' | © dnp_theme | ' + license + '\n'
18+
+ uglify.minify(srcPath).code), function (err) {
19+
if (err) return handleError(err);
20+
console.log(srcPath+' is done.');
21+
});
22+
}
23+
function handleError(err) {
24+
console.error(err);
25+
process.exit(1);
26+
}
27+
28+
// Minify file
29+
minify('dll.js', 'dist/dll.min.js');

dist/dll.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)