以前我们讲过一篇:JavaScript AJAX stream 瀑布流式显示实例 http://www.111cn.net/wy/js-ajax/69592.htm,现在我们再来多加练习一下。
HTML(注意包裹关系,方便js调用)
代码如下 | 复制代码 |
<body> <div id="main"> <div class="box"> <div class="pic"> <img src="images/0.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/1.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/2.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/3.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/4.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/2.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/3.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/4.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/5.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/6.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/7.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/3.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/4.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/5.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/3.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/4.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/5.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/6.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/7.jpg" alt=""> </div> </div> <div class="box"> <div class="pic"> <img src="images/3.jpg" alt=""> </div> </div> </div> </body> |
LESS(less预编译)
代码如下 | 复制代码 |
* { margin: 0; padding: 0; } #main { position: relative; } |
JavaScript(自己理解的注解)
(函数存在一定瑕疵,仅用于学习理解)
代码如下 | 复制代码 |
window.onload = function () { waterfall("main","box"); //调用自定义函数;作用于main下的每一个box元素; var dataInt = { "data":[{"src":"7.jpg"},{"src":"8.jpg"},{"src":"9.jpg"},{"src":"6.jpg"}]} for (var i = 0; i < dataInt.data.length; i++) { var oPic = document.createElement("div"); var oImg = document.createElement("img"); }; //流式布局主函数,自动调整数据块的位置; var oBoxW = oBoxs[0].offsetWidth; oParent.style.cssText = "width:"+oBoxW*cols+"px;margin:0 auto"; var hArr = []; var index = getMinhIndex(hArr,minH); oBoxs[i].style.position = "absolute"; hArr[index]+=oBoxs[i].offsetHeight; //js原生通过Class获取元素; //获取数组元素的index值; //检测是否具备了滚动加载数据块的条件; var height = document.body.clientHeight || document.documentElement.clientHeight; |