“瀑布流式”图片懒加载代码示例
2015-12-16 11:13
435 查看
最近项目使用到了“懒加载”,现在更新一般,因为平时主要使移动端的开发所以库文件使用的是
代码如下:
zepto.js。当然也可以和
jQuery通用。
代码如下:
[code]/** * Created by zhiqiang on 2015/10/14. * hpuhouzhiqiang@gmail.com * 图片的懒加载 **/ function loadImgLazy(node) { var lazyNode = $('[node-type=imglazy]', node), mobileHeight, lazyOffSetHeight, tempHeight, currentNodeTop, imgObject, imgDataSrc, localUrl; localUrl = location.href; // 获取当前浏览器可视区域的高度 mobileHeight = $(window).height(); return function(co) { var conf = { 'loadfirst': true, 'loadimg': true }; for (var item in conf) { if (item in co) { conf[item] = co[item]; } } var that = {}; var _this = {}; /** * [replaceImgSrc 动态替换节点中的src] * @param {[type]} tempObject [description] * @return {[type]} [description] */ _this.replaceImgSrc = function(tempObject) { var srcValue; $.each(tempObject, function(i, item) { imgObject = $(item).find('img[data-lazysrc]'); imgObject.each(function(i) { imgDataSrc = $(this).attr('data-lazysrc'); srcValue = $(this).attr('src'); if (srcValue == '#') { if (imgDataSrc) { $(this).attr('src', imgDataSrc); $(this).removeAttr('data-lazysrc'); } } }); }); }; /** * 首屏判断屏幕上是否出现imglazy节点,有的话就加载图片 * @param {[type]} i) { currentNodeTop [description] * @return {[type]} [description] */ _this.loadFirstScreen = function() { if (conf.loadfirst) { lazyNode.each(function(i) { currentNodeTop = $(this).offset().top; if (currentNodeTop < mobileHeight + 800) { _this.replaceImgSrc($(this)); } }); } }; //当加载过首屏以后按照队列加载图片 _this.loadImg = function() { if (conf.loadimg) { $(window).on('scroll', function() { var imgLazyList = $('[node-type=imglazy]', node); for (var i = 0; i < 5; i++) { _this.replaceImgSrc(imgLazyList.eq(i)); } }); } }; that = { replaceImgSrc: _this.replaceImgSrc(), mobileHeight: mobileHeight, objIsEmpty: function(obj) { for (var item in obj) { return false; } return true; }, destory: function() { if (_this) { $.each(_this, function(i, item) { if (item && item.destory) { item.destory(); } }); _this = null; } $(window).off('scroll'); } }; return that; }; }
相关文章推荐
- 23种设计模式C++实例之组合模式
- Eclipse上GIT插件EGIT使用手册
- C#中关于线程启动运行带多参数方法的操作
- mongo常用语法
- 创建自定义Qt类型
- Eclipse 下编写jni hello world
- Mentally grasp large project
- java类型转换
- java学习笔记-设计模式15(模板方法模式)
- 数据模型
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- pat考试准备一_两个数字和的标准化输出
- 23种设计模式C++实例之桥接模式
- 开发中遇到C++ 问题,记在这里,方便以后查看
- php中return die exit用法梳理
- python处理文件夹中同一类型
- yii框架 widgets 内部传递参数使用方案
- java连接sqlserver数据源配置
- php 实物回滚
- java方面的文章收藏