求指教!??!


打完代碼后出現這樣
window.onload=function(){
????waterfall('main','pin');
????var?dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]};
????
????window.onscroll=function(){
????????if(checkscrollside()){
????????????var?oParent?=?document.getElementById('main');//?父級對象
????????????for(var?i=0;i<dataInt.data.length;i++){
????????????????var?oPin=document.createElement('div');?//添加?元素節點
????????????????oPin.className='pin';???????????????????//添加?類名?name屬性
????????????????oParent.appendChild(oPin);??????????????//添加?子節點
????????????????var?oBox=document.createElement('div');
????????????????oBox.className='box';
????????????????oPin.appendChild(oBox);
????????????????var?oImg=document.createElement('img');
????????????????oImg.src='./images/'+dataInt.data[i].src;
????????????????oBox.appendChild(oImg);
????????????}
????????????waterfall('main','pin');
????????};
????}
}
/*
????parend?父級id
????pin?元素id
*/
function?waterfall(parent,pin){
????var?oParent=document.getElementById(parent);//?父級對象
????var?aPin=getClassObj(oParent,pin);//?獲取存儲塊框pin的數組aPin
????var?iPinW=aPin[0].offsetWidth;//?一個塊框pin的寬
????var?num=Math.floor(document.documentElement.clientWidth/iPinW);//每行中能容納的pin個數【窗口寬度除以一個塊框寬度】
????oParent.style.cssText='width:'+iPinW*num+'px;margin:0?auto;';//設置父級居中樣式:定寬+自動水平外邊距
????var?pinHArr=[];//用于存儲?每列中的所有塊框相加的高度。
????for(var?i=0;i<aPin.length;i++){//遍歷數組aPin的每個塊框元素
????????var?pinH=aPin[i].offsetHeight;
????????if(i<num){
????????????pinHArr[i]=pinH;?//第一行中的num個塊框pin?先添加進數組pinHArr
????????}else{
????????????var?minH=Math.min.apply(null,pinHArr);//數組pinHArr中的最小值minH
????????????var?minHIndex=getminHIndex(pinHArr,minH);
????????????aPin[i].style.position='absolute';//設置絕對位移
????????????aPin[i].style.top=minH+'px';
????????????aPin[i].style.left=aPin[minHIndex].offsetLeft+'px';
????????????//數組?最小高元素的高?+?添加上的aPin[i]塊框高
????????????pinHArr[minHIndex]+=aPin[i].offsetHeight;//更新添加了塊框后的列高
????????}
????}
}
/****
????*通過父級和子元素的class類?獲取該同類子元素的數組
????*/
function?getClassObj(parent,className){
????var?obj=parent.getElementsByTagName('*');//獲取?父級的所有子集
????var?pinS=[];//創建一個數組?用于收集子元素
????for?(var?i=0;i<obj.length;i++)?{//遍歷子元素、判斷類別、壓入數組
????????if?(obj[i].className==className){
????????????pinS.push(obj[i]);
????????}
????};
????return?pinS;
}
/****
????*獲取?pin高度?最小值的索引index
????*/
function?getminHIndex(arr,minH){
????for(var?i?in?arr){
????????if(arr[i]==minH){
????????????return?i;
????????}
????}
}
function?checkscrollside(){
????var?oParent=document.getElementById('main');
????var?aPin=getClassObj(oParent,'pin');
????var?lastPinH=aPin[aPin.length-1].offsetTop+Math.floor(aPin[aPin.length-1].offsetHeight/2);//創建【觸發添加塊框函數waterfall()】的高度:最后一個塊框的距離網頁頂部+自身高的一半(實現未滾到底就開始加載)
????var?scrollTop=document.documentElement.scrollTop||document.body.scrollTop;//注意解決兼容性
????var?documentH=document.documentElement.clientHeight;//頁面高度
????return?(lastPinH<scrollTop+documentH)?true:false;//到達指定高度后?返回true,觸發waterfall()函數
}
2016-09-28
1 整個window.onload少個};
2 新增的iPicture的className賦值賦錯了 ?你給iParent賦值了pic
3 檢測高度加載時 ?scrollTop的取值錯誤 ?讀不出來 ?正確寫法
var scrollH=document.documentElement.scrollTop || document.body.scrollTop;
2016-09-28
這個才是我打的·代碼
window.onload=function(){ waterfall(); var?dataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]}; window.onscroll=function(){ if(checkLoad){ var?mainBox=document.getElementById('main'); for(var?i=0;i<dataInt.data.length;i++){ var?iParent=document.createElement("div"); iParent.className="box"; mainBox.appendChild(iParent); var?iPicture=document.createElement("div"); iParent.className="pic"; iParent.appendChild(iPicture); var?oImg=document.createElement("img"); oImg.src="images/"+dataInt.data[i].src; iPicture.appendChild(oImg);}//不要加冒號 console.log(oImg.style); } waterfall(); } } function?getByClass(cls,parent){ var?oParent=parent?document.getElementById(parent):document; var?eles=oParent.getElementsByTagName("*"); var?result=[]; for(var?i=0;i<eles.length;i++){ if(eles[i].className==cls) result.push(eles[i]); } return?result; } function?getMinIndex(array,value){ for(var?i=0;i<array.length;i++){ if(array[i]==value){ return?i; } } } function?waterfall(){ var?oWidth=document.body.clientWidth; var?oBoxs=getByClass("box","main"); var?oBoxW=oBoxs[0].offsetWidth; var?cols=Math.floor(oWidth/oBoxW); var?hArr=[]; var?oParent=document.getElementById('main'); oParent.style.cssText='width:'+oBoxW*cols+'px;margin:0?auto;'; for(var?i=0;i<oBoxs.length;i++){ if(i<cols){ hArr.push(oBoxs[i].offsetHeight); } else{ var?minH=Math.min.apply(null,hArr); var?j=getMinIndex(hArr,minH); oBoxs[i].style.position='absolute'; oBoxs[i].style.top=minH+'px'; oBoxs[i].style.left=j*oBoxW+'px'; hArr[j]+=oBoxs[i].offsetHeight; } } } function?checkLoad(){ var?oParent=document.getElementById("main"); var?oBoxs=getByClass('box','main'); var?lastBox=oBoxs[oBoxs.length-1]; var?lastBoxH=lastBox.offsetTop+Math.floor(lastBox.offsetHeight/2); var?height=document.body.clientHeight||document.documentElement.clientHeight; var?scrollH=document.scrollTop; var?totalH=height+scrollH; return?(lastBoxH<totalH)?true:false; }2016-09-27
我測試你的代碼沒發現這個問題 有點尷尬