Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit add7595

Browse files
initial commit
1 parent b699ac1 commit add7595

File tree

10 files changed

+294
-3
lines changed

10 files changed

+294
-3
lines changed

.gulp/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var fs = require('fs');
2+
var onlyScripts = require('./utils/scriptFilter');
3+
var tasks = fs.readdirSync('./.gulp/tasks/').filter(onlyScripts);
4+
5+
tasks.forEach(function(task) {
6+
require('./tasks/' + task.replace('.js','') );
7+
});

.gulp/tasks/scripts.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var gulp = require('gulp'),
2+
path = require('path'),
3+
uglify = require('gulp-uglify'),
4+
notify = require('gulp-notify'),
5+
rename = require('gulp-rename'),
6+
handleErrors = require('../utils/handleErrors'),
7+
source_folder = 'src',
8+
destination_folder = 'min';
9+
10+
gulp.task('scripts', function(){
11+
return gulp.src(source_folder + '/*.js')
12+
.pipe(rename({suffix: '.min'}))
13+
.pipe(uglify({
14+
preserveComments: 'some'
15+
}))
16+
.pipe(gulp.dest(destination_folder))
17+
.pipe(notify({ message: 'Scripts task complete' }))
18+
.on('error', handleErrors);
19+
});

.gulp/tasks/setWatch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var gulp = require('gulp');
2+
3+
gulp.task('setWatch', function() {
4+
global.isWatching = true;
5+
});

.gulp/tasks/watch.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var gulp = require('gulp');
2+
3+
gulp.task('watch', ['setWatch'], function() {
4+
gulp.watch('src/*', ['scripts']);
5+
});

.gulp/utils/handleErrors.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var notify = require("gulp-notify");
2+
3+
module.exports = function() {
4+
5+
var args = Array.prototype.slice.call(arguments);
6+
7+
// Send error to notification center with gulp-notify
8+
notify.onError({
9+
title: "Compile Error",
10+
message: "<%= error.message %>"
11+
}).apply(this, args);
12+
13+
// Keep gulp from hanging on this task
14+
this.emit('end');
15+
};

.gulp/utils/scriptFilter.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var path = require("path");
2+
3+
// Filters out non .coffee and .js files. Prevents
4+
// accidental inclusion of possible hidden files
5+
module.exports = function(name) {
6+
return /(\.(js|coffee)$)/i.test(path.extname(name));
7+
};

README.md

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,46 @@
1-
lazy-responsive-images.js
2-
=========================
1+
# `easyLazyImages()`
32

4-
Lazy load images
3+
A global method for configuring lazy-loaded images.
4+
5+
## Usage
6+
7+
`easyLazyImages` takes the following arguments:
8+
9+
* **`load_at` (mixed)** - a unitless pixel width to lazy load images at or an array of strings representing named breakpoints for use with [`getActiveMQ`](https://github.com/easy-designs/easy-resize-watchers.js)
10+
* **`selector` (string, optional)** - an alternate selector to use for finding images to lazy-load (default: '[data-image-src]') *Note: The selected element must meet match the markup pattern*
11+
12+
This script assumes an element with the following attributes:
13+
14+
* `data-imag-src` - The file path to the default image source
15+
* `data-image-alt` (optional) - The string value for the alt attribute of the lazy-loaded image (default: '')
16+
* `data-image-srcset` (optional) - The `srcset` attribute value you want to set for responsive images
17+
18+
For example:
19+
20+
<div data-image-src="image.jpg"></div>
21+
22+
Or
23+
24+
<div data-image-src="image.jpg"
25+
data-image-alt="Some alternative text"></div>
26+
27+
To use the script, you simply call the method with the size you want to load the images at:
28+
29+
window.easyLazyImages( 400 );
30+
31+
This would lazy load images at 400px or above.
32+
33+
If you are using named breakpoints via [`getActiveMQ()`](https://github.com/easy-designs/easy-resize-watchers.js), you can supply the named breakpoints in an array:
34+
35+
window.easyLazyImages( ['break-3', 'break-4'] );
36+
37+
If any of these named breakpoints are employed, the images will be lazy-loaded.
38+
39+
If you want to configure a few different lazy-loaded scenarios, you can supply a second argument to refine the selector:
40+
41+
window.easyLazyImages( ['break-3', 'break-4'], 'main [data-image-src]' );
42+
window.easyLazyImages( ['break-4'], 'aside [data-image-src]' );
43+
44+
## Demo
45+
46+
See [this Codepen](http://codepen.io/aarongustafson/full/XJJoeR/) ([source](http://codepen.io/aarongustafson/pen/XJJoeR))

gulpfile.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
gulpfile.js
3+
===========
4+
Rather than manage one giant configuration file responsible
5+
for creating multiple tasks, each task has been broken out into
6+
its own file in gulp/tasks. Any file in that folder gets automatically
7+
required by the loop in ./gulp/index.js (required below).
8+
9+
To add a new task, simply add a new task file to gulp/tasks.
10+
*/
11+
12+
require('./.gulp');

min/easy-lazy-images.min.js

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

src/easy-lazy-images.js

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
/*! Easy Lazy Images (c) Aaron Gustafson (@AaronGustafson). MIT License. https://github.com/easy-designs/easy-lazy-images.js */
2+
3+
/* easyLazyImages()
4+
*
5+
* A global method for configuring lazy-loaded images.
6+
*
7+
* Takes the following arguments:
8+
*
9+
* @arg load_at (mixed)
10+
* a unitless pixel width to lazy load images at or
11+
* an array of strings representing named breakpoints
12+
* for use with getActiveMQ[1]
13+
* @arg selector (string, optional)
14+
* an alternate selector to use for finding images
15+
* to lazy-load (default: '[data-image-src]')
16+
* Note: The selected element must meet match the
17+
* markup pattern
18+
*
19+
* This script assumes an element with the following
20+
* attributes:
21+
*
22+
* `data-imag-src`
23+
* The file path to the default image source
24+
* `data-image-alt` (optional)
25+
* The string value for the alt attribute of the
26+
* lazy-loaded image (default: '')
27+
* `data-image-srcset` (optional)
28+
* The `srcset` attribute value you want to set
29+
* for responsive images
30+
*
31+
* For example:
32+
*
33+
* <div data-image-src="image.jpg"></div>
34+
*
35+
* Or
36+
*
37+
* <div data-image-src="image.jpg"
38+
* data-image-alt="Some alternative text"></div>
39+
*
40+
* To use the script, you simply call the method with the size you
41+
* want to load the images at:
42+
*
43+
* window.easyLazyImages( 400 );
44+
*
45+
* This would lazy load images at 400px or above.
46+
*
47+
* If you are using named breakpoints via `getActiveMQ()`[1], you
48+
* can supply the named breakpoints in an array:
49+
*
50+
* window.easyLazyImages( ['break-3', 'break-4'] );
51+
*
52+
* If any of these named breakpoints are employed, the images will
53+
* be lazy-loaded.
54+
*
55+
* If you want to configure a few different lazy-loaded scenarios,
56+
* you can supply a second argument to refine the selector:
57+
*
58+
* window.easyLazyImages( ['break-3', 'break-4'], 'main [data-image-src]' );
59+
* window.easyLazyImages( ['break-4'], 'aside [data-image-src]' );
60+
*
61+
* [1] https://github.com/easy-designs/easy-resize-watchers.js
62+
*
63+
**/
64+
(function(window){
65+
66+
// Dependencies
67+
if ( ! ( 'watchResize' in window ) ||
68+
! ( 'querySelectorAll' in document ) ){ return; }
69+
70+
var $img = document.createElement('img'),
71+
src_attr = 'data-image-src',
72+
srcset_attr = 'data-image-srcset',
73+
alt_attr = 'data-image-alt',
74+
imaged_attr = 'data-image-loaded',
75+
default_selector = '[' + src_attr + ']',
76+
not_imaged = ':not([' + imaged_attr + '])';
77+
78+
window.easyLazyImages = function( load_at, selector ){
79+
80+
var current_size = 0;
81+
82+
selector = selector || default_selector;
83+
84+
// default to pixel values
85+
function getSize( size )
86+
{
87+
current_size = size;
88+
}
89+
function shouldLoad()
90+
{
91+
return current_size >= load_at;
92+
}
93+
94+
// Support named breakpoints
95+
if ( load_at instanceof Array )
96+
{
97+
if ( ! ( 'getActiveMQ' in window ) )
98+
{
99+
console.log('easyLazyImages() requires getActiveMQ() to work with named breakpoints');
100+
return;
101+
}
102+
103+
getSize = function(){
104+
current_size = window.getActiveMQ();
105+
};
106+
shouldLoad = function(){
107+
var i = load_at.length;
108+
while ( i-- )
109+
{
110+
if ( current_size == load_at[i] )
111+
{
112+
return true;
113+
}
114+
}
115+
return false;
116+
};
117+
}
118+
119+
function createImage( $el )
120+
{
121+
var $lazy_img = $img.cloneNode( true ),
122+
src = $el.getAttribute( src_attr ),
123+
srcset = $el.getAttribute( srcset_attr ),
124+
alt = $el.getAttribute( alt_attr ) || '';
125+
126+
$lazy_img.setAttribute( 'src', src );
127+
$lazy_img.setAttribute( 'alt', alt );
128+
129+
if ( srcset )
130+
{
131+
$lazy_img.setAttribute( 'srcset', srcset );
132+
}
133+
134+
return $lazy_img;
135+
}
136+
137+
function addImage( $el )
138+
{
139+
$el.appendChild(
140+
createImage( $el )
141+
);
142+
$el.setAttribute( imaged_attr, '' );
143+
}
144+
145+
// watch for size changes
146+
window.watchResize( getSize );
147+
window.watchResize(function(){
148+
149+
// Don’t load if the necessary sizes aren’t met
150+
if ( ! shouldLoad() ){ return; }
151+
152+
var $els = document.querySelectorAll( selector + not_imaged ),
153+
e = 0,
154+
e_len = $els.length,
155+
$el,
156+
$img;
157+
158+
if ( ! e_len ) { return; }
159+
160+
for ( ; e < e_len; e++ )
161+
{
162+
$el = $els[e];
163+
// only images with the data-image-src attribute
164+
if ( !! $el.getAttribute( src_attr ) )
165+
{
166+
addImage( $el );
167+
}
168+
}
169+
170+
$els = null;
171+
$el = null;
172+
173+
});
174+
175+
};
176+
177+
}(window));

0 commit comments

Comments
 (0)