<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:///TR/xhtml1/DTD/">
<html xmlns="http:///1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Waterfall Flow Layout Code</title>
<script type="text/javascript" src="js/"></script>
<style type="text/css">
body, div, img, h1, h2, h3, h4, h5, h6 { padding:0px; margin:0px; }
img { border:none; }
.wrapper { width:960px; margin:0 auto; }
#con1_1 { position:relative; }
#con1_1 .product_list { position:absolute; left:0px; top:0px; padding:10px; background:#eee; }
.product_list img { width:200px; display:block; }
.product_list h2 { padding:5px 0px; font-size:12px; text-align:center; color:#333; }
</style>
</head>
<body>
<div class="wrapper">
<h3>This is the title part of the text</h3>
<div >
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
<div class="product_list"> <a href="#"><img src="images/"></a>
<h2>Picture height</h2>
</div>
</div>
<h3>The position of this line of text must first be used to calculate the height of the above content using js</h3>
</div>
</body>
<script type="text/javascript">
/*
Principle: 1. Put all the height values of li in the array
2. The tops of the first line are 0
3. Calculate which li is the smallest value of height
4. Put the next li under that li
*/
var margin = 10;//Set the spacing
var li=$(".product_list");//Block name
var li_W = li[0].offsetWidth+margin;//Fetch the actual width of the block
function liuxiaofan(){
var h=[];//Array that records the height of the block
var n = 960/li_W|0;
for(var i = 0;i < ;i++) {
li_H = li[i].offsetHeight;//Get the height of each li
if(i < n) {//n is the li with the most lines, so less than n is the first line
max_H =(null,h);
h[i]=li_H;//Put each li into the array
(i).css("top",0);//The top value of Li in the first line is 0
(i).css("left",i * li_W);//The left coordinate of the i-th li is the width of i*li
}
else{
min_H =(null,h) ;//Get the minimum value in the array, the one with the smallest height value in the block
minKey = getarraykey(h, min_H);//Pointer corresponding to the smallest value
h[minKey] += li_H+margin ;//Add height value after adding a new height
(i).css("top",min_H+margin);//First get the Li with the smallest height, and then put the next li under it
(i).css("left",minKey * li_W); //The left coordinate of the i-th li is the width of i*li
}
$("h2").eq(i).text("height:"+li_H);//Write the block height value into the corresponding block H2 title
}
max =(null,h) ;
$("#con1_1").css("height",max);
}
/* Use the for in operation to return the corresponding number of items of a certain value in the array (for example, calculate the smallest height value is the number in the array) */
function getarraykey(s, v) {for(k in s) {if(s[k] == v) {return k;}}}
/*Be sure to use onload here, because the image does not know the height value if it is not loaded*/
= function() {liuxiaofan();};
= function() {liuxiaofan();};
$(function(){
$(".product_list").hover(function(){
$(this).css("background-color","#ddd");
},function() {
$(this).css("background-color","#eee");
});
});
</script>
</html>