Jquery 無限往下滾動


這是仿照騰訊的微薄上的一個效果。滾動條可以無限的網下滾動並且無刷不斷從數據庫中獲取新的數據。

  <style type="text/css">
   body{ font-family: "Trebuchet MS",verdana,arial;}
   #loading{ display:none; font-weight:bold;color:#FF0000;}
   p { padding:10px;}
  </style>

<p id="loading">loading data... </p> 

$(function(){ 
     var isOK=true;//記錄上次訪問是否已經結束,如果ajax也有線程就好了 
     var scrollH=0;//判斷是往上滾還是往下滾 
     var  intI=1; 
  
    // loading層是固定在頁腳的記錄牌 
  
     $(".loading").css({"right":"2","bottom":0}); 
     $(".loading") 
     .ajaxStart(function(){ 
        isOK=false;//執行ajax的時候把isOK設置成false防止第一次沒有執行完的情況下執行第二次易出錯 
        $("#loading2").show(); 
          }) 
     .ajaxStop(function(){ 
         isOK=true; 
          $("#loading2").hide(); 
         }) 
      
    $(window).scroll(function(){ 
     //控制load層      
      document.getElementByIdx_x_x("loading").style.top=document.documentElement.scrollTop+"px"; 
        //觸法ajax條件  可以換算成百分比更好 
   if($(document).height()-$(window).scrollTop()-document.documentElement.clientHeight<240){ 
      //當前位置比上次的小就是往上滾動不要執行ajax代碼塊 
      if(scrollH>document.documentElement.scrollTopY) 
      { 
        $(".loading").append("<br/>向上滾不執行") 
        scrollH=document.documentElement.scrollTop;//記錄新位置 
       return; 
       } 
      if(isOK)//如果是第一次或者上次執行完成了就執行本次 
      { 
  
         scrollH=document.documentElement.scrollTop;//記錄新位置 
         $(".loading").append("<br/>~~<span style='background:#red'>執行了ajax。。。。。</span><br/>") 
         isOK=false; 
      $.ajax({ 
          type:"POST", 
          dataType: 'xml', 
          url:"http://localhost:49302/MY100/2010/WebService1.asmx/getDS", 
          error:function(e){ 
            $(".main").append('發生了錯誤:'+e) 
           }, 
          success:function(data){ 
           try{ 
            $(data).find("Table").each(function(i){ 
                  $(".main").append("結果:"+$(this).children('txtTitle').text()+"<br/>");  
                  $(".main").append("結果:"+$(this).children('txtBody1').text()+"<br/>");  
                  $(".main").append("結果:"+$(this).children('txtBody2').text()+"<br/>");  
                  $(".main").append("結果:"+$(this).children('txtBody3').text()+"<br/>"); 
                  $(".main").append("結果:"+$(this).children('txtBody4').text()+"<br/>"); 
                  $(".main").append("結果:"+$(this).children('ID').text()+"<br/>"); 
                  })//each 
            } 
           catch(e){ 
             $(".main").append("<p>"+e+"</p>") 
            } 
           }//success 
          })//ajax 
         }//if(isOK) 
         else
         { 
           $(".loading").append("<br/>~~你是向下滾了,但是上次還沒有執行完畢,等等吧<br/>") 
          } 
      }// 觸法ajax條件   
     })//scroll 
    })//Jquery 結束處 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM