三、瀑布流中的ajax請求詳解

 html代碼:

 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
  2. <html> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
  5. <title>瀑布流第三步ajax請求實現</title> 
  6. <style> 
  7.     *{margin:0px;padding:0px;} 
  8.     #main .pin{ 
  9.         width:225px; 
  10.         height:auto; 
  11.         padding:15px 0px 0px 15px; 
  12.         float:left; 
  13.     } 
  14.  
  15.     #main .pin .box{ 
  16.         width:205px; 
  17.         height:auto; 
  18.         padding:10px; 
  19.         background:#FFF; 
  20.         border:1px solid #CCC; 
  21.         box-shadow:0px 0px 6px #CCC; 
  22.         border-radius:5px; 
  23.     } 
  24.     #main .pin .box img{ 
  25.         width:205px; 
  26.     } 
  27. </style> 
  28. <!-- 頁面拖拽插件 --> 
  29. <script type="text/javascript" src="./js/drag.js"></script> 
  30. <!-- 瀑布流封裝 --> 
  31. <script type="text/javascript" src="./js/water.js"></script> 
  32. <!-- 引入js --> 
  33. <script type="text/javascript" src="./js/jquery.js"></script> 
  34. </head> 
  35. <body> 
  36.  
  37.     <div id="main"> 
  38.         <div class="pin"><!--每一個小塊--> 
  39.             <div class="box"> 
  40.                 <img src="./img/0.jpg"> 
  41.             </div> 
  42.         </div> 
  43.  
  44.         <div class="pin"><!--每一個小塊--> 
  45.             <div class="box"> 
  46.                 <img src="./img/1.jpg"> 
  47.             </div> 
  48.         </div> 
  49.  
  50.         <div class="pin"><!--每一個小塊--> 
  51.             <div class="box"> 
  52.                 <img src="./img/2.jpg"> 
  53.             </div> 
  54.         </div> 
  55.  
  56.         <div class="pin"><!--每一個小塊--> 
  57.             <div class="box"> 
  58.                 <img src="./img/3.jpg"> 
  59.             </div> 
  60.         </div> 
  61.  
  62.         <div class="pin"><!--每一個小塊--> 
  63.             <div class="box"> 
  64.                 <img src="./img/4.jpg"> 
  65.             </div> 
  66.         </div> 
  67.  
  68.         <div class="pin"><!--每一個小塊--> 
  69.             <div class="box"> 
  70.                 <img src="./img/5.jpg"> 
  71.             </div> 
  72.         </div> 
  73.         <div class="pin"><!--每一個小塊--> 
  74.             <div class="box"> 
  75.                 <img src="./img/6.jpg"> 
  76.             </div> 
  77.         </div> 
  78.          
  79.         <div class="pin"><!--每一個小塊--> 
  80.             <div class="box"> 
  81.                 <img src="./img/7.jpg"> 
  82.             </div> 
  83.         </div> 
  84.          
  85.         <div class="pin"><!--每一個小塊--> 
  86.             <div class="box"> 
  87.                 <img src="./img/8.jpg"> 
  88.             </div> 
  89.         </div> 
  90.         <div class="pin"><!--每一個小塊--> 
  91.             <div class="box"> 
  92.                 <img src="./img/4.jpg"> 
  93.             </div> 
  94.         </div> 
  95.         <div class="pin"><!--每一個小塊--> 
  96.             <div class="box"> 
  97.                 <img src="./img/7.jpg"> 
  98.             </div> 
  99.         </div> 
  100.          
  101.         <div class="pin"><!--每一個小塊--> 
  102.             <div class="box"> 
  103.                 <img src="./img/8.jpg"> 
  104.             </div> 
  105.         </div> 
  106.         <div class="pin"><!--每一個小塊--> 
  107.             <div class="box"> 
  108.                 <img src="./img/9.jpg"> 
  109.             </div> 
  110.         </div> 
  111.          
  112.         <div class="pin"><!--每一個小塊--> 
  113.             <div class="box"> 
  114.                 <img src="./img/0.jpg"> 
  115.             </div> 
  116.         </div> 
  117.  
  118.         <div class="pin"><!--每一個小塊--> 
  119.             <div class="box"> 
  120.                 <img src="./img/1.jpg"> 
  121.             </div> 
  122.         </div> 
  123.  
  124.         <div class="pin"><!--每一個小塊--> 
  125.             <div class="box"> 
  126.                 <img src="./img/2.jpg"> 
  127.             </div> 
  128.         </div> 
  129.  
  130.         <div class="pin"><!--每一個小塊--> 
  131.             <div class="box"> 
  132.                 <img src="./img/5.jpg"> 
  133.             </div> 
  134.         </div> 
  135.         <div class="pin"><!--每一個小塊--> 
  136.             <div class="box"> 
  137.                 <img src="./img/4.jpg"> 
  138.             </div> 
  139.         </div> 
  140.         <div class="pin"><!--每一個小塊--> 
  141.             <div class="box"> 
  142.                 <img src="./img/0.jpg"> 
  143.             </div> 
  144.         </div> 
  145.  
  146.         <div class="pin"><!--每一個小塊--> 
  147.             <div class="box"> 
  148.                 <img src="./img/1.jpg"> 
  149.             </div> 
  150.         </div> 
  151.  
  152.         <div class="pin"><!--每一個小塊--> 
  153.             <div class="box"> 
  154.                 <img src="./img/5.jpg"> 
  155.             </div> 
  156.         </div> 
  157.  
  158.         <div class="pin"><!--每一個小塊--> 
  159.             <div class="box"> 
  160.                 <img src="./img/3.jpg"> 
  161.             </div> 
  162.         </div> 
  163.     </div> 
  164.      
  165. </body> 
  166. </html> 

water.js代碼

 

  1. /** 
  2.  *  
  3.  */ 
  4. window.onload = function(){ 
  5.     //0調用函數 
  6.     waterfall('main','pin'); 
  7.     var ajaxState = true
  8.     window.onscroll = function(){ 
  9.          
  10.         if(checkscrollsite() && ajaxState){ 
  11.             ajaxState = false
  12.             $.ajax({ 
  13.                 type:'GET'
  14.                 url:'request.php'
  15.                 data:''
  16.                 dataType:'json'
  17.                 //請求開始 
  18.                 beforeSend:function(){ 
  19.                     var oParent = document.getElementById('main'); 
  20.                     var aPin = getClassObj(oParent,'pin'); 
  21.                     var lastPinH =aPin[aPin.length-1].offsetTop+aPin[aPin.length-1].offsetHeight; 
  22.                     //加入正在加載圖標 
  23.                     var loadImg = document.createElement('img'); 
  24.                     loadImg.src = './img/load.gif'
  25.                     loadImg.id = 'loadImg'
  26.                     oParent.appendChild(loadImg); 
  27.                     loadImg.style.position = 'absolute'
  28.                     loadImg.style.top = lastPinH+500+'px'
  29.                     loadImg.style.left = Math.floor(oParent.offsetWidth-loadImg.offsetWidth)+'px'
  30.                 }, 
  31.                 //請求中 
  32.                 success:function(data){ 
  33.                      
  34.                     //創建父節點 
  35.                     var oParent = document.getElementById('main'); 
  36.                     for(i in data){ 
  37.                         //alert(data[i].src); 
  38.                         //創建父級的元素節點 
  39.                         var oPin = document.createElement('div'); 
  40.                         oPin.className = 'pin'
  41.                         oParent.appendChild(oPin); 
  42.                         var oBox = document.createElement('div'); 
  43.                         oBox.className = 'box'
  44.                         oPin.appendChild(oBox); 
  45.                         var oImg = document.createElement('img'); 
  46.                         oImg.src = './img/'+data[i].src; 
  47.                         oBox.appendChild(oImg); 
  48.                     } 
  49.                     waterfall('main','pin'); 
  50.                 }, 
  51.                 //請求完成 
  52.                 complete:function(){ 
  53.                     //請求完成之後去掉加載的圖標 
  54.                     document.getElementById('main').removeChild(document.getElementById('loadImg')); 
  55.                     ajaxState = true
  56.                 } 
  57.             }) 
  58.              
  59.         } 
  60.          
  61.     } 
  62.     /** 
  63.     parent 父級   id 
  64.     pin     具體瀑布流塊,class類名 
  65.     */ 
  66.     function waterfall(parent,pin) { 
  67.     //2.定義父級 
  68.     var oParent = document.getElementById(parent); 
  69.     //9.調用函數並定義 
  70.     var aPin = getClassObj(oParent,pin); 
  71.     //alert(aPin.length); 
  72.     var iPinW = aPin[0].offsetWidth; 
  73.     //alert(iPinW); 
  74.     //10.計算頁面可以放下多少個圖片 
  75.     var num = Math.floor(document.documentElement.clientWidth/iPinW); 
  76.     //alert(num); 
  77.     //11.設置父級居中的樣式 
  78.     oParent.style.cssText = 'width:'+num*iPinW+'px;margin:0 auto;'
  79.      
  80.     //12準備一個數組 
  81.     var compareAarr = []; 
  82.     //17定義一個字符串 
  83.     //var str = ''; 
  84.     //13 
  85.     for(var i=0;i<aPin.length;i++){ 
  86.         //22設置第一行的數量 
  87.         if(i<num){ 
  88.              
  89.          
  90.         //獲取每個圖品的offsetHeight值 
  91.         compareAarr[i] = aPin[i].offsetHeight; 
  92.         //17獲取高度的索引 
  93.         //str += i+'=>'+aPin[i].offsetHeight+'\n'; 
  94.         //23 
  95.         }else
  96.             //取數組中的最小高度  compareAarr 
  97.             var minH = Math.min.apply({},compareAarr); 
  98.             //獲取最小的key值 
  99.             var minKey = getMinKey(compareAarr,minH); 
  100.             //定位超出寬度多的那個 
  101.             aPin[i].style.position = 'absolute'
  102.             //取超出的那個圖片的top值 
  103.             aPin[i].style.top = minH + 'px'
  104.             //設置超出那個圖片的left值 
  105.             aPin[i].style.left = aPin[minKey].offsetLeft + 'px'
  106.             //超出最後一個在加一 重新計算最低minH 
  107.             compareAarr[minKey] += aPin[i].offsetHeight; 
  108.         } 
  109.     } 
  110.      
  111.     //實現圖片的拖拽 
  112.     for(var i=num;i<aPin.length;i++){ 
  113.         //絕對定位 
  114.         aPin[i].style.position = 'absolute'
  115.         drag(aPin[i]); 
  116.     } 
  117.      
  118.     /** 
  119.      * 檢測根據瀏覽器的高度加載圖片 
  120.      */ 
  121.     function checkscrollsite(){ 
  122.         var oParent = document.getElementById('main'); 
  123.          
  124.         var aPin = getClassObj(oParent,'pin'); 
  125.         //最後圖片的高度 
  126.         var lastPinH =aPin[aPin.length-1].offsetTop+Math.floor(aPin[aPin.length-1].offsetHeight/2); 
  127.         //最後滾動條的高度 
  128.         var srcollTop = document.documentElement.scrollTop || document.body.scrollTop; 
  129.         // 
  130.         var documentH = document.documentElement.clientHeight; 
  131.         //if(lastPinH<srcollTop+documentH){ 
  132.         //  return true; 
  133.         //}else{ 
  134.         //  return false; 
  135.         //} 
  136.         return lastPinH<srcollTop+documentH?true:false
  137.     } 
  138.     /** 
  139.         獲取數組組最小的鍵值 
  140.         arr數組 
  141.         minH最小鍵值 
  142.     */ 
  143.     //18定義一個函數   獲取數組(compareAarr)中對應的最小的那個高的值(minH) 
  144.     function getMinKey(arr,minH){ 
  145.         for(key in arr){ 
  146.             if(arr[key] == minH){ 
  147.                 return key; 
  148.             } 
  149.         } 
  150.     } 
  151.      
  152.     /** 
  153.         通過class選擇元素 
  154.         parent父級 
  155.         className類名 
  156.     */ 
  157.     //3.定義一個函數 
  158.     function getClassObj(parent,className){ 
  159.         //4.定義匹配父級下面所有的元素 
  160.         var obj = parent.getElementsByTagName('*'); 
  161.         //5.定義一個數組 
  162.         var result = []; 
  163.         //6.循環obj 
  164.         for(var i=0;i<obj.length;i++){ 
  165.             if(obj[i].className == className){ 
  166.                 //7.pushu進result中 
  167.                 result.push(obj[i]); 
  168.             } 
  169.         } 
  170.         //8 
  171.         return result; 
  172.     } 
  173.      

 

request.php 代碼

 

  1. <?php 
  2. header('Content-type:text/html; charset=utf-8'); 
  3. $mysql = mysql_connect('localhost','root',''); 
  4. mysql_query('set names utf8',$mysql); 
  5. mysql_select_db('waterfall',$mysql); 
  6. $rows = mysql_query('select * from waterfall limit 0 , 20'); 
  7. $data = array(); 
  8. while ($row = mysql_fetch_assoc($rows)){ 
  9.     $data[] = $row;  
  10. sleep(3); 
  11. echo json_encode($data); 

 表結構代碼:

 

  1. CREATE TABLE `waterfall` ( 
  2.   `id` int(11) NOT NULL auto_increment, 
  3.   `title` char(128) NOT NULL
  4.   `src` varchar(1024) NOT NULL
  5.   PRIMARY KEY  (`id`) 
  6. ) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=66 ; 

加載需要使用的圖片

 

加載顯示圖片請右鍵另存爲即可用

 

實現效果圖:

 

實現效果圖

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章