移动端 延迟加载echo.js的使用
浏览器支持ie8+
<img src="img/blank.gif" alt="" data-echo="img/album-1.jpg">
<script>
window.echo = (function (window, document) { 'use strict'; /*
* Constructor function
*/
var Echo = function (elem) {
this.elem = elem;
this.render();
this.listen();
}; /*
* Images for echoing
*/
var echoStore = []; /*
* Element in viewport logic
*/
var scrolledIntoView = function (element) {
var coords = element.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));
} //if (scrolledIntoView(self)) {
// echoSrc(self, removeEcho(self, i));
// continue;
// }
//
// i++;
// }
//if i == 0 and image echoStore[0] is now in the view, than handler it. After splice(i, 1); the next image u got to check is still echoStore[0], but the next loop check from 1 (i=1).and this cuz the following puzzle that some guys menstioned.
}
}; /*
* Prototypal setup
*/
Echo.prototype = {
init : function () {
echoStore.push(this.elem);
},
render : function () {
if (document.addEventListener) {
document.addEventListener('DOMContentLoaded', echoImages, false);
} else {
window.onload = echoImages;
}
},
listen : function () {
window.onscroll = echoImages;
}
}; /*
* Initiate the plugin
*/
var lazyImgs = document.querySelectorAll('img[data-echo]');
for (var i = 0; i < lazyImgs.length; i++) {
new Echo(lazyImgs[i]).init();
} // [].forEach.call(document.querySelectorAll('img[data-echo]'), function (img) {
// new Echo(img).init();
//} })(window, document);
</script>
使用的方法
<script>
Echo.init({
offset: 1100, //距离可视区
throttle: 50 //延迟时间
});
</script>
移动端 延迟加载echo.js的使用的更多相关文章
- Echo.js – 简单易用的 JavaScript 图片延迟加载插件
Echo.js 是一个独立的延迟加载图片的 JavaScript 插件.Echo.js 不依赖第三方库,压缩后不到1KB大小. 延迟加载是提高网页首屏显示速度的一种很有效的方法,当图片元素进入窗口可视 ...
- 延迟加载外部js文件,延迟加载图片(jquery.lazyload.js和echo,js)
js里一说到延迟加载,大都离不开两种情形,即外部Js文件的延迟加载,以及网页图片的延迟加载: 1.首先简单说一下js文件的3种延迟加载方式: (1)<script type="text ...
- echo.js 延迟加载图片控件
echo.js的github地址:https://github.com/toddmotto/echo echo是一个独立的JavaScript.轻量级的.延迟图片加载插件,echo压缩后体积不到1 ...
- ECHO.js 纯javascript轻量级延迟加载
演示 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf ...
- 简单的JavaScript图像延迟加载库Echo.js
插件描述:和 Lazy Load 一样,Echo.js 也是一个用于图像延迟加载 JavaScript.不同的是 Lazy Load 是基于 jQuery 的插件,而 Echo.js 不依赖于 jQu ...
- 延迟加载图片控件--echo.js
echo.js的github地址:https://github.com/toddmotto/echo echo是一个独立的JavaScript.轻量级的.延迟图片加载插件,echo压缩后体积不到1 ...
- 简单的Javascript图片延迟加载库Echo.js
简介: 和 Lazy Load 一样,Echo.js 也是一个用于图像延迟加载 JavaScript.不同的是 Lazy Load 是基于 jQuery 的插件,而 Echo.js 不依赖于 jQue ...
- 图片懒加载 echo.js
(function (root, factory) { if (typeof define === 'function' && define.amd) { define(functio ...
- 如何使用echo.js实现图片的懒加载(整理)
如何使用echo.js实现图片的懒加载(整理) 一.总结 一句话总结:a.在img标签中添加data-echo属性加载真实图片:<img class="loading" sr ...
随机推荐
- testing and SQA_动态白盒測试
一.软件測试技术: 黑盒:在不知道程序内部结构,仅仅知道程序结构的情况下採用的測试技术或策略. 白盒:在知道程序内部结构的情况下採用的測试技术或策略. 两种測试方法从不同的角度出发,反映了软件的不同側 ...
- hdu 1398 Square Coins(生成函数,完全背包)
pid=1398">链接:hdu 1398 题意:有17种货币,面额分别为i*i(1<=i<=17),都为无限张. 给定一个值n(n<=300),求用上述货币能使价值 ...
- 【ECSHOP插件】商品颜色尺寸仿淘宝选择功能免费发布
先放效果图,如此实用的功能是不是迫不及待的要添加到自己的网店中了呢 牵涉到的修改文件(default模板为例) /themes/default/style.css /themes/default/ ...
- POJ 1276 Cash Machine(多重背包)
Cash Machine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24132 Accepted: 8446 Descrip ...
- Activity详细解释(生命周期、以各种方式启动Activity、状态保存,等完全退出)
一.什么是Activity? 简单的说:Activity或者悬浮于其它窗体上的交互界面. 在一个应用程序中通常由多个Activity构成.都会在Manifest.xml中指定一个主的Activity, ...
- Xamarin 手动安装步骤+破解
原文:Xamarin 手动安装步骤+破解 由于Xamarin运行的时候有很多依赖项,所以现在在官网上下载的XamarinInstall只是一个下载安装器,并不是软件包.它会自动下载安装所需的依赖软件, ...
- USACO comehome Dijkstra
USER: Kevin Samuel [kevin_s1] TASK: comehome LANG: C++ Compiling... Compile: OK Executing... Test 1: ...
- Vector Clock理解
背景近期在重读"Dynamo: Amazon's Highly Available Key-value Store"(经典好文,推荐!).文章4.4 中聊到了Data Versio ...
- IOSi科研OS7
具体的使用说明的适应
新近.我进行了项目iOS7适应,它有没有用7.0SDK它是由于老project采用iOS7.0存在一些问题,以这个机会,我专门整理改编iOS7需要注意的几个地方. 记录,如下面: 一,iOS7 ...
- activity点击时各种方法的区别
用到不同方法时候某些系统有不太一样的情况: public class MainActivity extends Activity { private static String TAG = " ...