1. 程式人生 > >當手機滑到頁面底部時自動載入資料

當手機滑到頁面底部時自動載入資料

方法1:

$(window).scroll(function(){

  var scrollTop = $(this).scrollTop();
  var scrollHeight = $(document).height();
  var windowHeight = $(this).height();
  if(scrollTop + windowHeight == scrollHeight){
  nearbyData();//載入資料的函式
  }

});

方法2:

 var $window = $(window);  
        var $document = $(document);          
        function onScroll() {  
            // 如果視窗底部小於1畫素,就執行載入事件  
            var winHeight = window.innerHeight ? window.innerHeight : $window.height(), // iphone fix  
              closeToBottom = ($window.scrollTop() + winHeight > $document.height() - 0.00000000001);  

            if (closeToBottom) {
  nearbyData();
            }  
        }  
        $window.bind('scroll', onScroll);