大家帮我看看错哪里了啊,布局效果就不行TT!?

来源:-

鸡肋2016

2016-08-19 16:54

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
<link href="css/瀑布流布局.css" rel="stylesheet" type="text/css" />
<script src="js/瀑布流布局.js" type="text/javascript"></script>
</head>

<body>
<div id="main">
    <div class="pin">
        <div class="box">
            <img src="images/4.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/5.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/6.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/7.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/8.jpg" />
        </div>
    </div><div class="pin">
        <div class="box">
            <img src="images/9.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/10.jpg" />
        </div>
    </div><div class="pin">
        <div class="box">
            <img src="images/11.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/12.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/13.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/14.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/15.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/16.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/17.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/18.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/19.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/20.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/21.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/22.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/23.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/24.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/25.jpg" />
        </div>
    </div>
    <div class="pin">
        <div class="box">
            <img src="images/26.jpg" />
        </div>
    </div>
</div>
</body>
</html>

@charset "utf-8";
/* CSS Document */
*{
 padding:0;
 margin:0;
 }
#main{
 position:relative;
 }
.pin{
 padding:15px 0 0 15px;
 float:left;
 }
.box{
 padding:10px;
 border:1px solid #ccc;
 box-shadow:0 0 6px #ccc;
 border-radius:5px;
 }
.box img{
 width:162px;
 height:auto;
 }

window.onload=function(){
 waterfall('main','pin');
 var dataInt={'data':[{'src':'4.jpg'},{'src':'5.jpg'},{'src':'6.jpg'}]};
 
window.onscroll=function(){
 if(checkscrollside()){
  var oParent=document.getElementById('main');
  for(var i=0;i<dataInt.data.length;i++){
   var aPin=document.createElement('div');
   aPin.className='pin';
   oParent.appendChild(aPin);
   var oBox=document.createElement('div');
   oBox.className='box';
   aPin.appendChild(oBox);
   var oImg=document.creativeElement('img');
   oImg.src='images/'+dataInt.data[i].src;
   oBox.appendChild(oImg);
   }
   waterfall('main','pin');
  };
 }
}
function waterfall(parent,pin){
 var oParent=document.getElementById(parent);
 var oPin=getByClass(oParent,pin);
 var aPinW=oPin[0].offsetWidth;
 var num=Math.floor(document.documentElement.clientWidth/aPinW);
 oParent.style.cssText='width:'+aPinW*num+'px;margin:0 auto';
 
 var pinHArr=[]; //储存每列中 所有块框相加的高度
 for(var i=0;i<oPin.length;i++){
  var PinH=oPin[i].offsetHeight;
  if(i<num){
   pinHArr.push(pinH);
  }else{
   var minH=Math.min.apply(null,pinHArr);
   var minHIndex=getminHIndex(pinHArr,minH);
   oPin[i].style.position='absolute';
   oPin[i].style.top=minH+'px';
   oPin[i].style.left=oPin[minHIndex].offsetLeft+'px';
   pinHArr[minHIndex]+=oPin[i].offsetHeight//更新添加块框后的列高
   }
  }
 }
function checkscrollside(){
 var oParent=document.getElementById('main');
 var oPin=getByClass(oParent,'pin');
 var lastPinH=oPin[oPin.length-1].offsetTop+Math.floor(oPin[oPin.length-1].offsetHeight/2);
 var scrollTop=document.documentElement.scrollTop||document.body.scrollTop;
 var documentH=document.documentElement.clientHeight;
 return(lastPinH<scrollTop+documentH)?true:false;
 }
function getminHIndex(arr,minH){
 for(var i in arr){
  if(arr[i]==minH){
   return i;
   }
  }
 }
function getByClass(parent,pin){
 var elements=parent.getElementsByTagName('*');
 var Pins=[];
 for(i=0;i<elements.length;i++){
  if(elements[i].className==pin){
   Pins.push(elements[i])
   }
  }
 return Pins;
 }  


写回答 关注

1回答

  • 鸡肋2016
    2016-08-19 17:09:13

    知道错哪了,字母大小写弄错了!看了后就才发现 但是检查好麻烦 大家有什么办法检查代码吗?

瀑布流布局

瀑布流布局是网站比较流行的一种布局方式,教你实现三大方式

97759 学习 · 736 问题

查看课程

相似问题