深入学习js瀑布流布局
本文实例为大家分享了js瀑布流布局学习资料,供大家参考,具体内容如下
特点:等宽不等高。
实现方式:Javascript/Jquery/CSS3多栏布局。
样例网站:花瓣网-->分类
一、JS实现瀑布流
index.html:页面结构
<!DOCTYPEhtml> <htmllang="en"> <head> <metacharset="UTF-8"> <title>瀑布流布局</title> <linkrel="stylesheet"href="styles/layout.css"> </head> <body> <divid="main"> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/0.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/1.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/2.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/3.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/4.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/5.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/6.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/7.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/8.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/9.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/10.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/11.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/12.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/13.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/14.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/15.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/16.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/17.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/18.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/19.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/20.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/21.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/22.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/23.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/24.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/25.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/26.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/27.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/28.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/29.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/30.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/31.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/32.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/33.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/34.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/35.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/36.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/37.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/38.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/39.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/40.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/41.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/42.jpg"alt=""></div></div> <divclass="box"><divclass="pic"><imgsrc="../waterFall/pic/43.jpg"alt=""></div></div> </div> <scriptsrc="scripts/waterfall.js"></script> </body> </html>
layout.css:页面元素样式
*{
pdding:0;
margin:0;
}
div#main{
position:relative;
}
div.box{
padding:15px0015px;
float:left;
}
div.pic{
padding:10px;
border:1pxsolid#ccc;
border-radius:5px;
box-shadow:005px#ccc;
}
.picimg{
height:auto;
width:165px;
}
waterfall.js
window.onload=function(){
waterFall('main','box');
//模拟后台相应数据json
vardataInt={
"data":
[
{"src":"0.jpg"},
{"src":"1.jpg"},
{"src":"2.jpg"},
{"src":"3.jpg"},
{"src":"4.jpg"},
{"src":"5.jpg"},
{"src":"6.jpg"}
]
}
window.onscroll=function(){
if(checkScrollSlide){
//将数据块渲染到当前页面的尾部
varoParent=document.getElementById("main");
for(vari=0;i<dataInt.data.length;i++){
varoBox=document.createElement("div");
oBox.className="box";
oParent.appendChild(oBox);
varoPic=document.createElement("div");
oPic.className="pic";
oBox.appendChild(oPic);
varimg=document.createElement("img");
img.setAttribute("src",dataInt.data[i]);
img.src="pic/"+dataInt.data[i].src;
oPic.appendChild(img);
}
dataInt=null;//清空数据块,防止无限加载
waterFall('main','box');//对页面新元素进行布局渲染
}
}
}
functionwaterFall(parent,box){
//将main下的class为box的所有元素取出来
varoParent=document.getElementById(parent);
varoBoxs=getByClass(oParent,box);
console.log(oBoxs.length);
//计算整个页面显示的列数(页面宽/box宽)
varoBoxW=oBoxs[0].offsetWidth;
//console.log(oBoxW);
varcols=Math.floor(document.documentElement.clientWidth/oBoxW);
//console.log(cols);
//设置main的宽
oParent.style.cssText="width:"+oBoxW*cols+"px;margin:0auto;"
varhArr=[];//存放每列高度的数组
for(vari=0;i<oBoxs.length;i++){
if(i<cols){
hArr.push(oBoxs[i].offsetHeight);
}else{
varminH=Math.min.apply(null,hArr);//获取当前数组最小高度值
//console.log(minH);
varindex=getMinhIndex(hArr,minH);//获取数组最小高度的索引
//console.log(index);
oBoxs[i].style.position="absolute";//将之后的图片依次绝对定位
oBoxs[i].style.top=minH+"px";
oBoxs[i].style.left=index*oBoxW+"px";//计算新图片所在的位置并赋值
hArr[index]+=oBoxs[i].offsetHeight;//变化数组列的高度值,因为加上了一张图片
}
//console.log(hArr);
}
}
//根据class获取元素
functiongetByClass(parent,clsName){
varboxArr=newArray(),//用来存储获取到的所有class为box的元素
oElements=parent.getElementsByTagName("*");
for(vari=0;i<oElements.length;i++){
if(oElements[i].className==clsName){
boxArr.push(oElements[i]);
}
}
returnboxArr;
}
functiongetMinhIndex(arr,val){
for(vari=0;i<arr.length;i++){
if(arr[i]==val){
returni;
}
}
}
//检测是否具备滚动条加载数据块条件
functioncheckScrollSlide(){
varoParent=document.getElementById("main");
varoBoxs=getByClass(oParent,"box");
varlastBoxH=oBoxs[oBoxs.length-1].offsetTop+Math.floor(oBoxs[oBoxs.length-1].offsetHeight/2);
varscrollTop=document.body.scrollTop||document.documentElement.scrollTop;//混杂模式和标准模式下的scrollTop获取
//console.log(scrollTop);
varheight=document.body.clientHeight||document.documentElement.clientHeight;//混杂模式和标准模式下的浏览器窗口高度获取
return(lastBoxH<scrollTop+height)?true:false;//检测最后一个box高度是否小于滚动高度+窗口高度,返回布尔值
}
二、JQuery
$(window).on("load",function(){
waterfall('main','pin');
vardataInt={'data':[{'src':'1.jpg'},{'src':'2.jpg'},{'src':'3.jpg'},{'src':'4.jpg'}]};
window.onscroll=function(){
if(checkscrollside()){
$.each(dataInt.data,function(index,value){
var$oPin=$('<div>').addClass('pin').appendTo($("#main"));
var$oBox=$('<div>').addClass('box').appendTo($oPin);
$('<img>').attr('src','./images/'+$(value).attr('src')).appendTo($oBox);
});
waterfall();
};
}
});
/*
parend父级id
pin元素id
*/
functionwaterfall(parent,pin){
var$aPin=$("#main>div");
variPinW=$aPin.eq(0).width();//一个块框pin的宽
varnum=Math.floor($(window).width()/iPinW);//每行中能容纳的pin个数【窗口宽度除以一个块框宽度】
//oParent.style.cssText='width:'+iPinW*num+'px;margin:0auto;';//设置父级居中样式:定宽+自动水平外边距
$("#main").css({
'width:':iPinW*num,
'margin':'0auto'
});
varpinHArr=[];//用于存储每列中的所有块框相加的高度。
$aPin.each(function(index,value){
varpinH=$aPin.eq(index).height();
if(index<num){
pinHArr[index]=pinH;//第一行中的num个块框pin先添加进数组pinHArr
}else{
varminH=Math.min.apply(null,pinHArr);//数组pinHArr中的最小值minH
varminHIndex=$.inArray(minH,pinHArr);
$(value).css({
'position':'absolute',
'top':minH+15,
'left':$aPin.eq(minHIndex).position().left
});
//数组最小高元素的高+添加上的aPin[i]块框高
pinHArr[minHIndex]+=$aPin.eq(index).height()+15;//更新添加了块框后的列高
}
});
}
functioncheckscrollside(){
var$aPin=$("#main>div");
varlastPinH=$aPin.last().get(0).offsetTop+Math.floor($aPin.last().height()/2);//创建【触发添加块框函数waterfall()】的高度:最后一个块框的距离网页顶部+自身高的一半(实现未滚到底就开始加载)
varscrollTop=$(window).scrollTop()//注意解决兼容性
vardocumentH=$(document).height();//页面高度
return(lastPinH<scrollTop+documentH)?true:false;//到达指定高度后返回true,触发waterfall()函数
}
三、CSS多栏布局
.container{
-webkit-column-width:160px;
-moz-column-width:160px;
-webkit-column-gap:5px;
-moz-column-gap:5px;
}
/*数据块砖块*/
.containerdiv{width:160px;
margin:4px0;}
【css3和js实现方法比较】
--css3方式--
1:不需要计算,浏览器自动计算,只需设置1列宽,性能高
2:列宽随着浏览器宽口大小进行改变,用户体验不好;
3:图片排序按照垂直顺序排列,打乱图片显示顺序
4.图片加载还是需要js
--js方式--
js实现的瀑布流不会有上面的缺点,但是性能相对要差!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。