AJAX+json+jquery实现预加载瀑布流布局

代码片段(1)[全屏查看所有代码]

1. [代码][JavaScript]代码     跳至 [1] [全屏预览]

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<!doctype html>
<html>
<head>
<meta charset= "utf-8" >
<title>定宽Jquery+AJAX+JSON瀑布流布局(每行代码都有详细注释)</title>
<style type= "text/css" >
body, ul, li, h3 { margin: 0; padding: 0; list-style: none; font: bold 12px "微软雅黑" ; }
/*瀑布流布局样式*/
#lxf-box { position: relative; width: 1000px; margin:0 auto;}
#lxf-box li { background: #fff; border: solid 1px #ccc; text-align: center; padding: 10px; float: left;}
h3 { padding-top: 8px; }
img { width: 200px; height: auto; display: block; border: 0 }
/*css3动画 注由于是css3制作的所以兼容性不保证 要想兼容性好 请自己做成gif动画加载图*/
/*li { -webkit-transition: all .7s ease-out .1s; -moz-transition: all .7s ease-out; -o-transition: all .7s ease-out .1s; transition: all .7s ease-out .1s }*/
#loading { display:none; line-height: 30px; background: #000; color:#fff; text-align: center; height: 30px; width: 100%; position:fixed; bottom:0; opacity:0.8;}
</style>
<script src= "http://www.iiwnet.com/templets/niu/js/jquery.min.js" type= "text/javascript" ></script>
</head>
<body>
<h1 color= "red" >预览无效果请刷新</h1>
<ul id= "lxf-box" >
   <li><a href= "http://www.iiwnet.com/div_css/342.html" ><img src= "http://www.iiwnet.com/uploads/allimg/120814/1-120Q411544TX.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/ajax/237.html" ><img src= "http://www.iiwnet.com/uploads/allimg/120801/1-120P1223013157.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/js_ad/271.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120808/1-120PP00915a2.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/js_texiao/312.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120812/1-120Q2150022G8.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/js_pic/191.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120722/1-120H2144003129.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/js_pic/318.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120812/1-120Q2161941b2.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/div_css/341.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120814/1-120Q4113240U2.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/div_css/350.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120814/125411K11-2.png" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/div_css/350.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120814/1254113249-12.png" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/div_css/349.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120814/12500a292-1.png" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/php_base/336.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120813/1-120Q3145U0938.jpg" ></a>
     <h3>图片标题</h3>
   </li>
   <li><a href= "http://www.iiwnet.com/div_css/344.html/" ><img src= "http://www.iiwnet.com/uploads/allimg/120814/12353B521-0.jpg" ></a>
     <h3>图片标题</h3>
   </li>
</ul>
<div id= "loading" >正在加载……</div>
<script>
/*
原理:1.把所有的li的高度值放到数组里面
      2.第一行的top都为0
  3.计算高度值最小的值是哪个li
  4.把接下来的li放到那个li的下面
<a href="http://iiwnet.com/js_web/">网页特效代码</a>:http://iiwnet.com
编写时间:2012年6月9日
*/
function iiwnet(){ //定义成函数便于调用
var wrap = document.getElementById( "lxf-box" )
var margin = 10; //这里设置间距
var li=$( "li" ); //这里是区块名称
varli_W = li[0].offsetWidth+margin; //取区块的实际宽度(包含间距,这里使用源生的offsetWidth函数,不适用jQuery的width()函数是因为它不能取得实际宽度,例如元素内有pandding就不行了)
var h=[]; //记录区块高度的数组
     li.css( "position" , "absolute" );
var n = wrap.offsetWidth/li_W|0; //容器的宽度除以区块宽度就是一行能放几个区块
for ( var i = 0;i < li.length;i++) { //有多少个li就循环多少次
li_H = li[i].offsetHeight; //获取每个li的高度
if (i < n) { //n是一行最多的li,所以小于n就是第一行了
h[i]=li_H; //把每个li放到数组里面
li.eq(i).css( "top" ,0); //第一行的Li的top值为0
li.eq(i).css( "left" ,i * li_W); //第i个li的左坐标就是i*li的宽度
}
else {
min_H =Math.min.apply( null ,h) ; //取得数组中的最小值,区块中高度值最小的那个
minKey = getarraykey(h, min_H); //最小的值对应的指针
h[minKey] += li_H+margin ; //加上新高度后更新高度值
li.eq(i).css( "top" ,min_H+margin); //先得到高度最小的Li,然后把接下来的li放到它的下面
li.eq(i).css( "left" ,minKey * li_W); //第i个li的左坐标就是i*li的宽度
}
$( "h3" ).eq(i).text( "编号:" +i+ ",高度:" +li_H); //把区块的序号和它的高度值写入对应的区块H3标题里面
$( "li" ).animate({opacity:1});
}
}
/* 使用for in运算返回数组中某一值的对应项数(比如算出最小的高度值是数组里面的第几个) */
function getarraykey(s, v) { for (k in s) { if (s[k] == v) { return k;}}}
/*这里一定要用onload,因为图片不加载完就不知道高度值*/
window.onload = function () {iiwnet();};
/*浏览器窗口改变时也运行函数*/
window.onresize = function () {iiwnet();};
/**********************************************************************/
/*无限加载*/
var i=1;
function getMore(){
$( "#loading" ).show();
var json = "http://www.iiwnet.com/images/jstx/img.js" ;
$.getJSON(json, function (data){
$.each(data, function (i){
var url=data[i].url;
var html= "<li style='opacity:0'><a href='http://www.iiwnet.com/'><img src=" +url+ " ></a><h3>图片标题</h3></li>" ;
$( "#lxf-box" ).append(html);
$( "#loading" ).hide();
});
iiwnet();
i=1
});
};
/*滚动到底部的时候*/
  $(window).bind( "scroll" , function (){
if ( $(document).scrollTop() + $(window).height() > $(document).height() - 10 && i==1) {
i=0;
getMore();
}
  });
</script>
</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值