jquery实现简单瀑布流代码

测试环境:ie8 ff13.0.1 chrome22

可以将分页获取的内容依次填入四个div中,瀑布流的分页可以以多页(比如5页)为单位二次分页,这样可以减少后台算法的复杂度

[html]view plaincopy

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html>
  4. <head>
  5. <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  6. <title>waterfall flow</title>
  7. <script type="text/javascript" src="../jquery-1.8.0.min.js" /></script>
  8. <style type="text/css" >
  9. body{margin:0px;}
  10. #main{width:840px;margin:0 auto;}
  11. .flow{float:left;width:200px;margin:5px;background:#ABC;}
  12. </style>
  13. <script type="text/javascript" >
  14. $(document).ready(function(){
  15. // 初始化内容
  16. for(var i = 0 ; i < 3 ; i++){
  17. $(".flow").each(function(){
  18. $(this).append("<div width:90%;height:"+getRandom(200,300)+"px;margin:5px auto;background:#159;\"></div>");
  19. });
  20. }
  21. $(window).scroll(function(){
  22. // 被卷去的高度
  23. var scrollTop = document.body.scrollTop||document.documentElement.scrollTop;
  24. // 页面高度
  25. var pageHeight = $(document).height();
  26. // 可视区域高度
  27. var viewHeight = $(window).height();
  28. //alert(viewHeight);
  29. //当滚动到底部时
  30. if((scrollTop+viewHeight)>(pageHeight-20)){
  31. if(scrollTop<1000){//防止无限制的增长
  32. for(var i = 0 ; i < 2 ; i++){
  33. $(".flow").each(function(){
  34. $(this).append("<div width:90%;height:"+getRandom(200,300)+"px;margin:5px auto;background:#159;\"></div>");
  35. });
  36. }
  37. }
  38. }
  39. });
  40. });
  41. /*
  42. * 获取指定范围随机数
  43. * @param min,最小取值
  44. * @param max,最大取值
  45. */
  46. function getRandom(min,max){
  47. //x上限,y下限
  48. var x = max;
  49. var y = min;
  50. if(x<y){
  51. x=min;
  52. y=max;
  53. }
  54. var rand = parseInt(Math.random() * (x - y + 1) + y);
  55. return rand;
  56. }
  57. </script>
  58. </head>
  59. <body>
  60. <div >
  61. <div class="flow" ></div>
  62. <div class="flow" ></div>
  63. <div class="flow" ></div>
  64. <div class="flow" ></div>
  65. </div>
  66. </body>
  67. </html>