1、html+css+js代码:
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html" charset="utf-8" /> <title>hhh</title> </head> <body> <style type="text/css"> *{ padding: 0; margin: 0; } #main{ position: relative; } .pin{ float: left; padding: 15px 0 0 15px; } .box{ border-radius: 5px; box-shadow: 0 0 6px #ccc; border:1px solid #ccc; padding: 10px; } .box img{ width: 162px; height:auto; } </style> <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 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'); }; } } //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; } </script> </body> <div> <div> <div> <img src="https://www.jb51.net/images/0.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/1.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/2.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/3.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/4.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/5.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/6.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/7.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/8.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/9.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/10.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/11.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/12.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/13.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/14.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/15.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/16.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/17.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/18.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/19.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/20.jpg"> </div> </div> <div> <div> <img src="https://www.jb51.net/images/21.jpg"> </div> </div> </div> </html>
2、思路分析