2014年3月6日 星期四

Scroll Bottom Ajax

        //離底部有多少距離
        var bottom = parseInt ( $(window).height() / 10 );

        //是否觸發Ajax
        var isLoading = false;

            $(window).scroll(function(){
                if (!isLoading && $(window).scrollTop() + $(window).height() >= $(document).height() - bottom){
                //if (!isLoading && $(window).scrollTop() + screen.height >= $(document).height() - bottom){
                   isLoading = true;
                  
                  doSomeThing();
                }
          });



function doSomeThing(){
             // show loading icon
            $("#loadingIcon").show();

           $.ajax({
                method: "POST",
                url: "",
                dataType: "json",
                async: true,
                data: {
                },
                cache: false,
                success: function (data) {
               //新增內容
               $("#contentList").append(resultHTML);

                    //確認資料是否還有資料 是否要恢復Scroll事件
                    if( parseInt(count) > 0) {
                        isLoading = false;
                        // 取得資料索引遞增
                         AtPage++;
                    }
                    $("#loadingIcon").hide();
                },
                error: function () {
                    isLoading = false;
                    $("#loadingIcon").hide();
                }
            });
}