本文实例为大家分享了js实现瀑布流布局的具体代码,供大家参考,具体内容如下
1、html+css+js代码:
<script type="text/javascript">
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';
oparent.appendChild(opin);
var o<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a> = document.createElement('div');
o<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>.className = '<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>';
opin.appendChild(o<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>);
var oimg = document.createElement('img');
oimg.src = './images/' +dataint.data[i].src;
o<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>.appendChild(oimg);
}
waterfall('main','pin');
};
}
}
//parent为父元素的id,pin为子元素id
function waterfall(parent,pin){
var oparent = document.getElementById(parent);
var apin = getclassobj(oparent,pin);//获取id为oparent的类名为pin的元素
var ipinw = apin[0].offsetWidth;
var num = Math.floor(document.documentElement.clientWidth/ipinw);
oparent.style.cssText = 'width:' + ipinw*num + 'px;margin:0 auto;';
var pinharr = [];
for( var i = 0;i < apin.length; i++)
{
var pinh = apin[i].offsetHeight;
if (i < num) {
pinharr[i] = pinh;
}
else{
var minh = Math.min.apply(null,pinharr);
var minhindex = getminhindex(pinharr,minh);
apin[i].style.position = 'absolute';
apin[i].style.top = minh +'px';
apin[i].style.left = apin[minhindex].offsetLeft + 'px';
pinharr[minhindex] += apin[i].offsetHeight;
}
}
}
//获取id为parent的类名为classname的元素
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;
}
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);
var scrollTop = document.documentElement.scrollTop||document.body.scrollTop;
var documenth = document.documentElement.clientHeight;
return(lastpinh<scrollTop + documenth)?true:false;
}
<div class="pin">
<div class="<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>">
<img src="images/16.jpg">
</div>
</div>
<div class="pin">
<div class="<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>">
<img src="images/17.jpg">
</div>
</div>
<div class="pin">
<div class="<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>">
<img src="images/18.jpg">
</div>
</div>
<div class="pin">
<div class="<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>">
<img src="images/19.jpg">
</div>
</div>
<div class="pin">
<div class="<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>">
<img src="images/20.jpg">
</div>
</div>
<div class="pin">
<div class="<a href="https://www.jb51.cc/tag/Box/" target="_blank" class="keywords">Box</a>">
<img src="images/21.jpg">
</div>
</div>