原生js实现移动端瀑布流式代码示例
瀑布流布局已成为当今非常普遍的图片展示方式,无论是PC还是手机等移动设备上。最近使用到了“懒加载”,现在更新一般,因为平时主要使移动端的开发所以库文件使用的是zepto.js 。当然也可以和jQuery 通用。
代码如下:
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; }; }
以上所述就是本文给大家分享的全部内容了,希望能够对大家熟练使用javascript有所帮助。
JavaScript计划任务后台运行的方法
即使忘了JavaScript的一切知识,也不会忘记:它是阻塞的。想象一下,你的浏览器里住着一个魔法小精灵,负责浏览器的正常运转。不论渲染HTML,响应菜
JS弹出对话框实现方法(三种方式)
本文实例讲述了JS弹出对话框实现方法。分享给大家供大家参考,具体如下:1.警告框htmlheadscripttype="text/javascript"functiondisp_alert(){alert("我是警告框!!")}
最简单的JavaScript图片轮播代码(两种方法)
通过改变每个图片的opacity属性:素材图片:代码一:!DOCTYPEhtmlhtmllang="en"headmetacharset="UTF-"title最简单的轮播广告/titlestylebody,div,ul,li{margin:;padding:;}ul{list-st
标签:图片,加载,方法,给大家,弹出