jquery實現簡單瀑布流代碼

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
 <html>  
 <head>  
 <meta http-equiv="content-type" content="text/html; charset=utf-8" />  
 <title>waterfall flow</title>  
 <script type="text/javascript" src="../jquery-1.8.0.min.js" /></script>  
 <style type="text/css" >  
     body{margin:0px;}  
     #main{width:840px;margin:0 auto;}  
     .flow{float:left;width:200px;margin:5px;background:#ABC;}  
 </style>  
 <script type="text/javascript" >  
     $(document).ready(function(){  
         // 初始化內容  
         for(var i = 0 ; i < 3 ; i++){  
             $(".flow").each(function(){  
                 $(this).append("<div style=\"width:90%;height:"+getRandom(200,300)+"px;margin:5px auto;background:#159;\"></div>");  
             });  
         }  
           
         $(window).scroll(function(){  
             // 被捲去的高度  
             var scrollTop = document.body.scrollTop||document.documentElement.scrollTop;  
             // 頁面高度  
             var pageHeight = $(document).height();  
             // 可視區域高度  
             var viewHeight = $(window).height();  
             //alert(viewHeight);  
             //當滾動到底部時  
             if((scrollTop+viewHeight)>(pageHeight-20)){  
                 if(scrollTop<1000){//防止無限制的增長  
                     for(var i = 0 ; i < 2 ; i++){  
                         $(".flow").each(function(){  
                             $(this).append("<div style=\"width:90%;height:"+getRandom(200,300)+"px;margin:5px auto;background:#159;\"></div>");  
                         });  
                     }  
                 }  
             }  
         });  
     });  
         /*  
     * 獲取指定範圍隨機數  
     * @param min,最小取值  
     * @param max,最大取值  
     */  
       
     function getRandom(min,max){  
         //x上限,y下限   
         var x = max;   
         var y = min;   
         if(x<y){  
             x=min;  
             y=max;  
         }  
         var rand = parseInt(Math.random() * (x - y + 1) + y);  
         return rand;  
     }  
   
 </script>  
 </head>  
 <body>  
 <div id="main">  
     <div class="flow" ></div>  
     <div class="flow" ></div>  
     <div class="flow" ></div>  
     <div class="flow" ></div>  
 </div>  
 </body>  
 </html>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章