纯js鼠标滚动时候动态加载新页面数据

// jQuery我们可以这样使用
$(window).scroll(function(){
  if ($(window).scrollTop() == $(document).height() - $(window).height()){
     page++;
     loadPage(page);
  }
});

 

//纯js我们可以这样做
window.onscroll = function() {
      var scrollTop = document.body.scrollTop;
      var offsetHeight = document.body.offsetHeight;
      var scrollHeight = document.body.scrollHeight;
      if (scrollTop == scrollHeight - offsetHeight) {
        page++;
        loadList(page);
      }
    };


function loadList(page) {
      page = page || 1;

      if (isLoad) {
        getJSON('/forum.php?mod=hot&page=' + page).then(function(data) {
          if (data.code == 200) {
            data = data.data;
            if (data && Object.keys(data).length > 0) {
              for (var k in data) {
                var v = data[k];
                detailTemplate = detailTemplate.cloneNode(true);
                var userInfoObj = detailTemplate.getElementsByClassName('user-info')[0];
                userInfoObj.getElementsByClassName('name')[0].innerText = v.author;
                userInfoObj.getElementsByClassName('avatar')[0].src = v.avatar;
                userInfoObj.getElementsByClassName('post-time')[0].innerHTML = v.lastpost;
                detailTemplate.getElementsByClassName('title')[0].innerText = v.subject;
                detailTemplate.getElementsByClassName('desc')[0].innerText = v.subject;
                var extInfoObj = detailTemplate.getElementsByClassName('ext-info')[0];
                extInfoObj.getElementsByClassName('from')[0].innerText = v.fname;
                extInfoObj.getElementsByClassName('view-time')[0].innerText = v.views;
                postListObj.appendChild(detailTemplate);
              }
            } else {
              isLoad = false;
            }
          } else {
            isLoad = false;
          }
        }, function(status) {
          console.log('Something went wrong, status is ' + status);
        });
      }
    }

 

你可能感兴趣的:(每天进步一点点,学习永无止境,纵观千象,纯js,乔乐共享)