1. 程式人生 > >jQuery頁面滾動底部加載數據

jQuery頁面滾動底部加載數據

his attr 操作 tps ber http console 地址 cti

$(window).scroll(function () {
var scrollTop = $(this).scrollTop();
var scrollHeight = $(document).height();
var windowHeight = $(this).height();
if (scrollTop + windowHeight == scrollHeight) {
   //此處是滾動條到底部時候觸發的事件,在這裏寫要加載的數據,或者是拉動滾動條的操作
  //shlar page = Number($("#redgiftNextPage").attr(‘currentpage‘)) + 1;
  //redgiftList(page);
   //$("#redgiftNextPage").attr(‘currentpage‘, page + 1);

    console.log("到底了,發起請求")
  }
});

解析:
判斷滾動條到底部,需要用到DOM的三個屬性值,即scrollTop、clientHeight、scrollHeight。
scrollTop為滾動條在Y軸上的滾動距離。
clientHeight為內容可視區域的高度。
scrollHeight為內容可視區域的高度加上溢出(滾動)的距離。
從這個三個屬性的介紹就可以看出來,滾動條到底部的條件即為scrollTop + clientHeight == scrollHeight。(兼容不同的瀏覽器)。

原頁面地址:https://www.cnblogs.com/chen-lhx/p/5121360.html

jQuery頁面滾動底部加載數據