mirror of https://github.com/toddmotto/echo.git
Todd Motto
11 years ago
9 changed files with 187 additions and 245 deletions
@ -1,4 +1,17 @@
|
||||
# Node |
||||
node_modules |
||||
|
||||
## OS X |
||||
.DS_Store |
||||
.tmp |
||||
.sass-cache |
||||
.AppleDouble |
||||
.LSOverride |
||||
Icon |
||||
._* |
||||
.Spotlight-V100 |
||||
.Trashes |
||||
|
||||
## Windows |
||||
Thumbs.db |
||||
ehthumbs.db |
||||
Desktop.ini |
||||
$RECYCLE.BIN/ |
||||
|
@ -1,87 +1,104 @@
|
||||
/*! |
||||
* Gruntfile.js configuration |
||||
/* |
||||
* Gruntfile.js |
||||
* @author Todd Motto |
||||
* @version 1.0.0 |
||||
*/ |
||||
|
||||
'use strict'; |
||||
|
||||
module.exports = function ( grunt ) { |
||||
var LIVERELOAD_PORT = 35729; |
||||
|
||||
/* |
||||
* Dynamically load the npm tasks |
||||
*/ |
||||
require( 'matchdep' ).filterDev('grunt-*').forEach( grunt.loadNpmTasks ); |
||||
var lrSnippet = require('connect-livereload')({ |
||||
port: LIVERELOAD_PORT |
||||
}); |
||||
|
||||
/* |
||||
* Grunt init |
||||
*/ |
||||
grunt.initConfig({ |
||||
|
||||
/* |
||||
* Grunt JSON for project |
||||
*/ |
||||
pkg: grunt.file.readJSON( 'package.json' ), |
||||
|
||||
/* |
||||
* Credit banner |
||||
*/ |
||||
tag: { |
||||
banner: "/*!\n" + |
||||
" * <%= pkg.title %>\n" + |
||||
" * @version <%= pkg.version %>\n" + |
||||
" * @author <%= pkg.author[0].name %> <%= pkg.author[1].url %>\n" + |
||||
" * Project: <%= pkg.homepage %>\n" + |
||||
" *\n" + |
||||
" * <%= pkg.description %>\n" + |
||||
" * Copyright <%= pkg.year %>." + |
||||
" <%= pkg.licenses[0].type %> licensed.\n" + |
||||
" */\n" |
||||
}, |
||||
var mountFolder = function (connect, dir) { |
||||
return connect.static(require('path').resolve(dir)); |
||||
}; |
||||
|
||||
/* |
||||
* jsHint |
||||
*/ |
||||
jshint: { |
||||
files: ["src/echo.js"], |
||||
options: { |
||||
jshintrc: ".jshintrc" |
||||
} |
||||
}, |
||||
module.exports = function (grunt) { |
||||
|
||||
/* |
||||
* Concat |
||||
*/ |
||||
concat: { |
||||
dist: { |
||||
src: ["src/echo.js"], |
||||
dest: "dist/echo.js" |
||||
}, |
||||
options: { |
||||
banner: "<%= tag.banner %>" |
||||
} |
||||
}, |
||||
require('matchdep').filterDev('grunt-*').forEach(grunt.loadNpmTasks); |
||||
|
||||
/* |
||||
* UglifyJS |
||||
*/ |
||||
uglify: { |
||||
files: { |
||||
src: ["dist/echo.js"], |
||||
dest: "dist/echo.min.js" |
||||
}, |
||||
options: { |
||||
banner: "<%= tag.banner %>" |
||||
} |
||||
grunt.initConfig({ |
||||
pkg: grunt.file.readJSON('package.json'), |
||||
project: { name: 'echo' }, |
||||
connect: { |
||||
options: { |
||||
port: 9000, |
||||
hostname: 'localhost' |
||||
}, |
||||
livereload: { |
||||
options: { |
||||
middleware: function (connect) { |
||||
return [lrSnippet, mountFolder(connect, 'dist')]; |
||||
} |
||||
} |
||||
} |
||||
}, |
||||
tag: { |
||||
banner: '/*!\n' + |
||||
' * <%= pkg.name %> v<%= pkg.version %>\n' + |
||||
' * <%= pkg.description %>\n' + |
||||
' * Project: <%= pkg.homepage %>\n' + |
||||
' * by <%= pkg.author.name %>: <%= pkg.author.url %>\n' + |
||||
' * Copyright.' + |
||||
' <%= pkg.licenses[0].type %> licensed.\n' + |
||||
' */\n' |
||||
}, |
||||
jshint: { |
||||
options: { |
||||
jshintrc: '.jshintrc' |
||||
}, |
||||
files: 'src/<%= project.name %>.js', |
||||
}, |
||||
concat: { |
||||
dist: { |
||||
src: ['src/<%= project.name %>.js'], |
||||
dest: 'dist/<%= project.name %>.js' |
||||
}, |
||||
options: { |
||||
banner: '<%= tag.banner %>' |
||||
} |
||||
}, |
||||
uglify: { |
||||
files: { |
||||
src: ['dist/<%= project.name %>.js'], |
||||
dest: 'dist/<%= project.name %>.min.js' |
||||
}, |
||||
options: { |
||||
banner: '<%= tag.banner %>' |
||||
} |
||||
}, |
||||
open: { |
||||
server: { |
||||
path: 'http://localhost:<%= connect.options.port %>' |
||||
} |
||||
}, |
||||
watch: { |
||||
concat: { |
||||
files: 'src/{,*/}*.js', |
||||
tasks: ['concat:dist', 'uglify'] |
||||
}, |
||||
livereload: { |
||||
options: { |
||||
livereload: LIVERELOAD_PORT |
||||
}, |
||||
files: [ |
||||
'{,*/}*.html', |
||||
'dist/{,*/}*.js' |
||||
] |
||||
} |
||||
} |
||||
}); |
||||
|
||||
}); |
||||
|
||||
/* |
||||
* Register tasks |
||||
*/ |
||||
grunt.registerTask("default", [ |
||||
"jshint", |
||||
"concat", |
||||
"uglify" |
||||
]); |
||||
grunt.registerTask('default' , [ |
||||
'jshint', |
||||
'concat:dist', |
||||
'uglify', |
||||
'connect:livereload', |
||||
'open', |
||||
'watch' |
||||
]); |
||||
|
||||
}; |
@ -1,92 +1,41 @@
|
||||
/*! |
||||
* Echo |
||||
* @version 1.1.0 |
||||
* @author Todd Motto http://toddmotto.com
|
||||
* Echo v1.2.0 |
||||
* Lazy-loading images with data-* attributes |
||||
* Project: https://github.com/toddmotto/echo
|
||||
* |
||||
* Raw JavaScript lazy-loading images with HTML5 data-* attributes. |
||||
* Copyright 2013. MIT licensed. |
||||
* by Todd Motto: http://toddmotto.com
|
||||
* Copyright. MIT licensed. |
||||
*/ |
||||
window.echo = (function (window, document, undefined) { |
||||
window.Echo = (function (window, document, undefined) { |
||||
|
||||
'use strict'; |
||||
|
||||
/* |
||||
* Constructor function |
||||
*/ |
||||
var Echo = function (elem) { |
||||
this.elem = elem; |
||||
}; |
||||
|
||||
/* |
||||
* Images for echoing |
||||
*/ |
||||
var echoStore = []; |
||||
var store; |
||||
|
||||
/* |
||||
* Element in viewport logic |
||||
*/ |
||||
var scrolledIntoView = function (element) { |
||||
var coords = element.getBoundingClientRect(); |
||||
var _inView = function (img) { |
||||
var coords = img.getBoundingClientRect(); |
||||
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight)); |
||||
}; |
||||
|
||||
/* |
||||
* Changing src attr logic |
||||
*/ |
||||
var echoSrc = function (img, callback) { |
||||
img.src = img.getAttribute('data-echo'); |
||||
if (callback) { |
||||
callback(); |
||||
} |
||||
}; |
||||
|
||||
/* |
||||
* Remove loaded item from array |
||||
*/ |
||||
var removeEcho = function (element, index) { |
||||
if (echoStore.indexOf(element) !== -1) { |
||||
echoStore.splice(index, 1); |
||||
} |
||||
}; |
||||
|
||||
/* |
||||
* Echo the images and callbacks |
||||
*/ |
||||
var echoImages = function () { |
||||
for (var i = 0; i < echoStore.length; i++) { |
||||
var self = echoStore[i]; |
||||
if (scrolledIntoView(self)) { |
||||
echoSrc(self, removeEcho(self, i)); |
||||
var _pollImages = function () { |
||||
for (var i = 0; i < store.length; i++) { |
||||
var self = store[i]; |
||||
if (_inView(self)) { |
||||
self.src = self.getAttribute('data-echo'); |
||||
if (store.indexOf(self) !== -1) { |
||||
store.splice(i, 1); |
||||
} |
||||
} |
||||
} |
||||
}; |
||||
|
||||
/* |
||||
* Prototypal setup |
||||
*/ |
||||
Echo.prototype = { |
||||
init : function () { |
||||
echoStore.push(this.elem); |
||||
} |
||||
var init = function () { |
||||
store = [].slice.call(document.querySelectorAll('[data-echo]')); |
||||
_pollImages(); |
||||
window.onscroll = _pollImages; |
||||
}; |
||||
|
||||
/* |
||||
* Initiate the plugin |
||||
*/ |
||||
var lazyImgs = document.querySelectorAll('img[data-echo]'); |
||||
for (var i = 0; i < lazyImgs.length; i++) { |
||||
new Echo(lazyImgs[i]).init(); |
||||
} |
||||
|
||||
/* |
||||
* Bind the events |
||||
*/ |
||||
if (document.addEventListener) { |
||||
document.addEventListener('DOMContentLoaded', echoImages, false); |
||||
} else { |
||||
window.onload = echoImages; |
||||
} |
||||
window.onscroll = echoImages; |
||||
return { |
||||
init: init |
||||
}; |
||||
|
||||
})(window, document); |
||||
|
@ -1,10 +1,8 @@
|
||||
/*! |
||||
* Echo |
||||
* @version 1.1.0 |
||||
* @author Todd Motto http://toddmotto.com
|
||||
* Echo v1.2.0 |
||||
* Lazy-loading images with data-* attributes |
||||
* Project: https://github.com/toddmotto/echo
|
||||
* |
||||
* Raw JavaScript lazy-loading images with HTML5 data-* attributes. |
||||
* Copyright 2013. MIT licensed. |
||||
* by Todd Motto: http://toddmotto.com
|
||||
* Copyright. MIT licensed. |
||||
*/ |
||||
window.echo=function(a,b){"use strict";var c=function(a){this.elem=a},d=[],e=function(c){var d=c.getBoundingClientRect();return(d.top>=0&&d.left>=0&&d.top)<=(a.innerHeight||b.documentElement.clientHeight)},f=function(a,b){a.src=a.getAttribute("data-echo"),b&&b()},g=function(a,b){-1!==d.indexOf(a)&&d.splice(b,1)},h=function(){for(var a=0;a<d.length;a++){var b=d[a];e(b)&&f(b,g(b,a))}};c.prototype={init:function(){d.push(this.elem)}};for(var i=b.querySelectorAll("img[data-echo]"),j=0;j<i.length;j++)new c(i[j]).init();b.addEventListener?b.addEventListener("DOMContentLoaded",h,!1):a.onload=h,a.onscroll=h}(window,document); |
||||
window.Echo=function(a,b){"use strict";var c,d=function(c){var d=c.getBoundingClientRect();return(d.top>=0&&d.left>=0&&d.top)<=(a.innerHeight||b.documentElement.clientHeight)},e=function(){for(var a=0;a<c.length;a++){var b=c[a];d(b)&&(b.src=b.getAttribute("data-echo"),-1!==c.indexOf(b)&&c.splice(a,1))}},f=function(){c=[].slice.call(b.querySelectorAll("[data-echo]")),e(),a.onscroll=e};return{init:f}}(window,document); |
@ -1,22 +1,35 @@
|
||||
{ |
||||
"name": "Echo", |
||||
"title": "Echo", |
||||
"description": "Raw JavaScript lazy-loading images with HTML5 data-* attributes.", |
||||
"author": [ |
||||
{ "name" : "Todd Motto" }, |
||||
{ "url" : "http://toddmotto.com" } |
||||
], |
||||
"version": "1.2.0", |
||||
"homepage": "https://github.com/toddmotto/echo", |
||||
"licenses": [ |
||||
{ "type": "MIT" } |
||||
], |
||||
"author": "Todd Motto", |
||||
"year" : "2013", |
||||
"version": "1.1.0", |
||||
"description": "Lazy-loading images with data-* attributes", |
||||
"author": { |
||||
"name": "Todd Motto", |
||||
"email": "todd@toddmotto.com", |
||||
"url": "http://toddmotto.com", |
||||
"github" : "https://github.com/toddmotto" |
||||
}, |
||||
"licenses": [{ |
||||
"type": "MIT" |
||||
}], |
||||
"repository": { |
||||
"type": "git", |
||||
"url": "http://github.com/toddmotto/echo.git" |
||||
}, |
||||
"devDependencies": { |
||||
"grunt": "~0.4.1", |
||||
"grunt-contrib-jshint": "~0.7.1", |
||||
"grunt-contrib-concat": "~0.3.0", |
||||
"grunt-contrib-uglify": "~0.2.0", |
||||
"grunt-contrib-jshint": "~0.4.3", |
||||
"grunt-contrib-uglify": "~0.2.7", |
||||
"grunt-contrib-connect": "~0.3.0", |
||||
"grunt-contrib-watch": "~0.4.4", |
||||
"connect-livereload": "~0.2.0", |
||||
"grunt-open": "~0.2.0", |
||||
"matchdep": "~0.1.2" |
||||
}, |
||||
"peerDependencies": { |
||||
"grunt-cli": "*" |
||||
} |
||||
} |
||||
|
@ -1,83 +1,34 @@
|
||||
window.echo = (function (window, document, undefined) { |
||||
window.Echo = (function (window, document, undefined) { |
||||
|
||||
'use strict'; |
||||
|
||||
/* |
||||
* Constructor function |
||||
*/ |
||||
var Echo = function (elem) { |
||||
this.elem = elem; |
||||
}; |
||||
|
||||
/* |
||||
* Images for echoing |
||||
*/ |
||||
var echoStore = []; |
||||
var store; |
||||
|
||||
/* |
||||
* Element in viewport logic |
||||
*/ |
||||
var scrolledIntoView = function (element) { |
||||
var coords = element.getBoundingClientRect(); |
||||
var _inView = function (img) { |
||||
var coords = img.getBoundingClientRect(); |
||||
return ((coords.top >= 0 && coords.left >= 0 && coords.top) <= (window.innerHeight || document.documentElement.clientHeight)); |
||||
}; |
||||
|
||||
/* |
||||
* Changing src attr logic |
||||
*/ |
||||
var echoSrc = function (img, callback) { |
||||
img.src = img.getAttribute('data-echo'); |
||||
if (callback) { |
||||
callback(); |
||||
} |
||||
}; |
||||
|
||||
/* |
||||
* Remove loaded item from array |
||||
*/ |
||||
var removeEcho = function (element, index) { |
||||
if (echoStore.indexOf(element) !== -1) { |
||||
echoStore.splice(index, 1); |
||||
} |
||||
}; |
||||
|
||||
/* |
||||
* Echo the images and callbacks |
||||
*/ |
||||
var echoImages = function () { |
||||
for (var i = 0; i < echoStore.length; i++) { |
||||
var self = echoStore[i]; |
||||
if (scrolledIntoView(self)) { |
||||
echoSrc(self, removeEcho(self, i)); |
||||
var _pollImages = function () { |
||||
for (var i = 0; i < store.length; i++) { |
||||
var self = store[i]; |
||||
if (_inView(self)) { |
||||
self.src = self.getAttribute('data-echo'); |
||||
if (store.indexOf(self) !== -1) { |
||||
store.splice(i, 1); |
||||
} |
||||
} |
||||
} |
||||
}; |
||||
|
||||
/* |
||||
* Prototypal setup |
||||
*/ |
||||
Echo.prototype = { |
||||
init : function () { |
||||
echoStore.push(this.elem); |
||||
} |
||||
var init = function () { |
||||
store = [].slice.call(document.querySelectorAll('[data-echo]')); |
||||
_pollImages(); |
||||
window.onscroll = _pollImages; |
||||
}; |
||||
|
||||
/* |
||||
* Initiate the plugin |
||||
*/ |
||||
var lazyImgs = document.querySelectorAll('img[data-echo]'); |
||||
for (var i = 0; i < lazyImgs.length; i++) { |
||||
new Echo(lazyImgs[i]).init(); |
||||
} |
||||
|
||||
/* |
||||
* Bind the events |
||||
*/ |
||||
if (document.addEventListener) { |
||||
document.addEventListener('DOMContentLoaded', echoImages, false); |
||||
} else { |
||||
window.onload = echoImages; |
||||
} |
||||
window.onscroll = echoImages; |
||||
return { |
||||
init: init |
||||
}; |
||||
|
||||
})(window, document); |
||||
|
Loading…
Reference in new issue