jquery插件masonry实现的一个简单的瀑布流

我个人理解,瀑布流就是开始加载多少条,然后再通过scroll事件追加加载数据,我说的可能不好理解,具体结合代码应该能看懂,如果我理解的不好请多多包涵。

css代码:

View Code
1 <style type="text/css" > 
2     .body{ background-color:#eee;}
3     #main{ background-color:White; width:920px; margin:0 auto;}
4     .item{width:225px; float:left; display:inline; margin-right:5px; margin-top:5px;}
5     .item img{ border:0;}
6 </style> 

 

页面布局:

View Code
 1 <div id="main"> 
 2     <div class="item"><a href="javascript:void(0)"><img src="img/1.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
 3     
 4     <div class="item"><a href="javascript:void(0)"><img src="img/2.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
 5     
 6     <div class="item"><a href="javascript:void(0)"><img src="img/3.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
 7     
 8     <div class="item"><a href="javascript:void(0)"><img src="img/4.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
 9     
10     <div class="item"><a href="javascript:void(0)"><img src="img/5.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
11     
12    <div class="item"><a href="javascript:void(0)"><img src="img/6.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
13     
14     <div class="item"><a href="javascript:void(0)"><img src="img/7.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
15     
16     <div class="item"><a href="javascript:void(0)"><img src="img/8.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
17     
18     <div class="item"><a href="javascript:void(0)"><img src="img/9.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
19     
20     <div class="item"><a href="javascript:void(0)"><img src="img/1.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
21     <div class="item"><a href="javascript:void(0)"><img src="img/2.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
22     <div class="item"><a href="javascript:void(0)"><img src="img/5.jpg" alt="" /></a><p>sdfsdfsdfsdfs</p></div>
23 </div>
24 <div id="load" style="display:none; color:Red; text-align:center;">正在加载...</div> 

js代码:首先要引用jquery.js和masonry.js两个js

View Code
 1 <script type="text/javascript" language="javascript" charset="utf-8">
 2     $(document).ready(function(){ 
 3          $("#main").masonry(function(){
 4             itemSelector:'.item'
 5         });
 6         //滚动条事件
 7         $(window).bind('scroll',function(){
 8             if($(document).scrollTop()+$(window).height()>$(document).height()-20){
 9 //                 alert("scrollTop:"+$(document).scrollTop()+" chuangHeight:"+$(window).height()+" he:"+($(document).scrollTop()+$(window).height())+" pageHeight:"+$(document).height());
10                   $("#load").show();   //显示正在加载
11                   setTimeout(function(){
12                     for(var j=0;j<10;j++){  
13                         var str=$('<div class="item"><a href="javascript:void(0)"><img src="img/10.jpg" alt=""/></a><p>横跨包斯蒂芬</p></div>'); 
14                         //加载并重新布局
15                         $("#main").append(str).masonry('appended',str,true);
16                     }
17                   },3000);  //每隔多少时间显示
18             }
19         });
20     });
21 </script>

 

posted @ 2012-12-20 16:06  weych  阅读(294)  评论(0编辑  收藏  举报