上述代码拷贝到项目中即可使用,使用方式:
//使用默认参数new lazyload();//使用自定义参数new lazyload({ wrapper: '.article-content', selector: '.image', src: 'data-image', defaultSrc: 'example.com/static/images/default.png'});若在 IE8 中使用,没有 map 函数时,请在引用插件前加入下列处理 map 函数兼容性的代码:
// 实现 ECMA-262, Edition 5, 15.4.4.19// 参考: http://es5.github.com/#x15.4.4.19if (!Array.prototype.map) { Array.prototype.map = function(callback, thisArg) { var T, A, k; if (this == null) { throw new TypeError(" this is null or not defined"); } // 1. 将O赋值为调用map方法的数组. var O = Object(this); // 2.将len赋值为数组O的长度. var len = O.length >>> 0; // 3.如果callback不是函数,则抛出TypeError异常. if (Object.prototype.toString.call(callback) != "[object Function]") { throw new TypeError(callback + " is not a function"); } // 4. 如果参数thisArg有值,则将T赋值为thisArg;否则T为undefined. if (thisArg) { T = thisArg; } // 5. 创建新数组A,长度为原数组O长度len A = new Array(len); // 6. 将k赋值为0 k = 0; // 7. 当 k < len 时,执行循环. while (k < len) { var kValue, mappedValue; //遍历O,k为原数组索引 if (k in O) { //kValue为索引k对应的值. kValue = https://www.isolves.com/it/cxkf/yy/js/2019-09-19/O[k]; // 执行callback,this指向T,参数有三个.分别是kValue:值,k:索引,O:原数组. mappedValue = callback.call(T, kValue, k, O); // 返回值添加到新数组A中. A[k] = mappedValue; } // k自增1 k++; } // 8. 返回新数组A return A; };}
【使用原生的js实现简易的图片延时加载】
推荐阅读
- 干货 TCP/IP加速原理详解
- 在我们日常使用中,选择喷墨打印机还是激光打印机呢?
- 远程桌面管理工具novnc的搭建和使用
- 基于辅助功能的镜像劫持攻击原理
- 使用python实现UDP编程
- 软件架构-解密电商系统-秒杀的原理和开发思路
- 全国最美刘氏宗祠大集合!
- 《汉家江湖》昆仑山怎么开
- 厉害!谷城一地入选全国乡村特色产业亿元村
- 妙控鼠标2使用教程 妙控鼠标2有哪些手势
