首页 > 代码库 > 瀑布流布局
瀑布流布局
//html文件 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>瀑布流布局</title> <link rel="stylesheet" type="text/css" href="http://www.mamicode.com/STYLE.css"> </head>
<style>
*{
margin: 0;
padding:0;
}
#main{
position: relative;
}
.box{
float: left;
}
.pic{
padding: 10px;
border-radius: 5px;
border: 1px solid #ccc;
box-shadow: 0 0 5px #ccc;
}
.pic img {
width: 165px;
height: auto;
}
</style>
<body> <div id="main"> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_00.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_01.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_02.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_03.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_04.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_05.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_06.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_07.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_08.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_09.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_010.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_011.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_012.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_013.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_014.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_015.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_016.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_017.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_018.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/P_019.jpg" ></div> </div> <div class="box"> <div class="pic"><img src="http://www.mamicode.com/images/toTop.gif" ></div> </div> </div> <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> <script src="http://www.mamicode.com/script.js" type="text/javascript"></script> </body> </html>
//JQuery文件 $(window).on(‘load‘,function(){ waterfull(); //实现加载 var dateInt={"data":[{"src":‘P_00.jpg‘},{"src":‘P_01.jpg‘},{"src":‘P_02.jpg‘},{"src":‘P_03.jpg‘}]}; $(window).on(‘scroll‘,function(){ if(checkScrollSlide){ $.each(dateInt.data,function(key,value){ var oBox = $(‘<div>‘).addClass(‘box‘).appendTo($(‘#main‘)); var oPic = $(‘<div>‘).addClass(‘pic‘).appendTo($(oBox)); var oImg = $(‘<img>‘).attr(‘src‘,‘./images/‘+$(value).attr(‘src‘)).appendTo($(oPic)); }) } waterfull(); }) }); function waterfull(){ var $boxs = $(‘#main>div‘); var w = $boxs.eq(0).outerWidth(); var cols = Math.floor($(window).width()/w); $(‘main‘).width(w*cols).css(‘margin‘,‘0 auto‘); var hArr = []; $boxs.each(function(index,value){ var h = $boxs.eq(index).outerHeight() if(index<cols){ hArr[index]=h; }else { var minH = Math.min.apply(null,hArr); var minHindex = $.inArray(minH,hArr); $(value).css({ ‘position‘:‘absolute‘, ‘top‘:minH+‘px‘, ‘left‘:minHindex*w+‘px‘ }) hArr[minHindex] += $boxs.eq(index).outerHeight(); } }) } function checkScrollSlide(){ var $lastbox = $(‘#main>div‘).last(); var lastBoxDis = $lastbox.offset().top+Math.floor($lastbox.ouertHeight()/2); var scrollTop = $(window).scrollTop(); var documentH = $(window).height(); return (lastBoxDis<scrollTop+documentH)?true:false; }
瀑布流布局
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。