1. 程式人生 > >一個簡單的瀑布流效果

一個簡單的瀑布流效果

window.onload = function(){ // 獲取元素 var oBox = document.getElementById('box'); var oUl = document.getElementsByTagName('ul'); var vH = document.documentElement.clientHeight; // 迴圈動態新增 creaeElement(); function
creaeElement(){
for(var i = 0 ; i < 21 ; i ++){ // 建立img標籤 var oImg = document.createElement('img'); oImg.src = 'images/' + i + '.jpg'; // 建立li標籤 var oLi = document.createElement('li'); oLi.appendChild(oImg); //獲取最小ul的索引
var minIndex = checkHeight(oUl); oUl[minIndex].appendChild(oLi); } } //滾動事件 window.onscroll = function(){ var sTop = document.documentElement.scrollTop || document.body.scrollTop; //滾動條的高度 + 瀏覽器的高度 = 內容區域的高度
if( sTop + vH > document.body.scrollHeight*0.8 ){ creaeElement(); } } } // 獲取最小ul的索引 function checkHeight(oUl){ //初始化 var height = 1000000; var index = 0; for(var i = 0 ; i < oUl.length ; i++){ var nowHeight = oUl[i].offsetHeight; if( nowHeight < height ){ height = nowHeight; index = i; } } return index ; }