web前端開發常用上拉刷新demo,代碼可直接運行

web前端開發常用上拉刷新demo,代碼可直接運行

前端開發過程中常用的上拉刷新demo,下拉刷新與此原理類似

界面如下(有點醜的demo哈):

web前端開發常用上拉刷新demo,代碼可直接運行

代碼如下如示:

web前端開發常用上拉刷新demo,代碼可直接運行

web前端開發常用上拉刷新demo,代碼可直接運行

<style>

*{

padding: 0;

margin: 0;

}

.box{

height: 100%;

/* overflow: hidden auto; */

}

#refreshContainer{

width: 90%;

margin: 0 auto;

}

ul li{

outline-style: none;

outline: none;

}

#refreshContainer li{

height: 60px;

line-height: 60px;

padding: 0 10px;

background: green;

margin: 10px auto;

list-style:none;

}

  • 111
  • 222
  • 333
  • 444
  • 555
  • 111
  • 222
  • 333
  • 444
  • 555

(function(window) {

// 獲取當前滾動條的位置

function getScrollTop() {

var scrollTop = 0;

if (document.documentElement && document.documentElement.scrollTop) {

scrollTop = document.documentElement.scrollTop;

} else if (document.body) {

scrollTop = document.body.scrollTop;

}

return scrollTop;

}

// 獲取當前可視範圍的高度

function getClientHeight() {

var clientHeight = 0;

if (document.body.clientHeight && document.documentElement.clientHeight) {

clientHeight = Math.min(document.body.clientHeight, document.documentElement.clientHeight);

}

else {

clientHeight = Math.max(document.body.clientHeight, document.documentElement.clientHeight);

}

return clientHeight;

}

// 獲取文檔完整的高度

function getScrollHeight() {

return Math.max(document.body.scrollHeight, document.documentElement.scrollHeight);

}

var _text = document.querySelector('.refreshText'),

_container = document.getElementById('refreshContainer');

// 節流函數

var throttle = function(method, context){

clearTimeout(method.tId);

method.tId = setTimeout(function(){

method.call(context);

}, 300);

}

function fetchData() {

setTimeout(function() {

_container.insertAdjacentHTML('beforeend', '

  • new add...
  • ');

    }, 1000);

    }

    window.onscroll = function() {

    if (getScrollTop() + getClientHeight() == getScrollHeight()) {

    _text.innerText = '加載中...';

    throttle(fetchData);

    }

    };

    })(window);

    代碼可直接複製

    每日一分享,感謝所有關注的粉絲們


    分享到:


    相關文章: