Example waterfall flow layout code

  • 2020-03-30 02:35:41
  • OfStack

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title> Waterfall layout code </title> 
<script type="text/javascript" src="js/jquery.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 section of the body </h3> 
<div id="con1_1"> 
<div class="product_list"> <a href="#"><img src="images/img1.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img2.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img3.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img4.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img5.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img6.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img7.jpg"></a> 
<h2> Picture height </h2> 
</div> 
<div class="product_list"> <a href="#"><img src="images/img8.jpg"></a> 
<h2> Picture height </h2> 
</div> 
</div> 
<h3> The position of this line of text should be used first js So let's figure out the height of this </h3> 
</div> 
</body> 
<script type="text/javascript"> 
 
var margin = 10;//Set the spacing
var li=$(".product_list");//The name of the block
var li_W = li[0].offsetWidth+margin;//Take the actual width of the block
function liuxiaofan(){ 
var h=[];//An array that records the height of a block
var n = 960/li_W|0; 
for(var i = 0;i < li.length;i++) { 
li_H = li[i].offsetHeight;//Get the height of each li
if(i < n) {//N is the li with the most rows, so less than n is the first row
max_H =Math.max.apply(null,h); 
h[i]=li_H;//Put each li into the array
li.eq(i).css("top",0);//The top value of Li in the first row is 0
li.eq(i).css("left",i * li_W);//The left coordinate of the ith li is the width of I times li
} 
else{ 
min_H =Math.min.apply(null,h) ;//Gets the smallest value in the array, the smallest height in the block
minKey = getarraykey(h, min_H);//The smallest value corresponds to the pointer
h[minKey] += li_H+margin ;//Update the height value after adding a new height
li.eq(i).css("top",min_H+margin);//So I'm going to get the Li with the lowest height, and then I'm going to put the next Li under it
li.eq(i).css("left",minKey * li_W); //The left coordinate of the ith li is the width of I times li
} 
$("h2").eq(i).text(" Height: "+li_H);//Write the block height value into the corresponding block H2 heading
} 
max =Math.max.apply(null,h) ; 
$("#con1_1").css("height",max); 
} 
 
function getarraykey(s, v) {for(k in s) {if(s[k] == v) {return k;}}} 
 
window.onload = function() {liuxiaofan();}; 
window.onresize = function() {liuxiaofan();}; 


$(function(){ 
$(".product_list").hover(function(){ 
$(this).css("background-color","#ddd"); 
},function() { 
$(this).css("background-color","#eee"); 
}); 
}); 
</script> 
</html> 

Related articles: