前言
在移動端H5網頁中,下拉刷新和上拉加載更多數據的交互方式出現頻率很高,開源社區也有很多類似的解決方案,如iscroll,pulltorefresh.js庫等。下面是對這兩種常見交互基本實現原理的闡述。
實現原理
下拉刷新
實現下拉刷新主要分為三步:
- 監聽原生
touchstart
事件,記錄其初始位置的值,e.touches[0].pageY
; - 監聽原生
touchmove
事件,記錄並計算當前滑動的位置值與初始位置值的差值,大於0
表示向下拉動,並借助CSS3的translateY
屬性使元素跟隨手勢向下滑動對應的差值,同時也應設置一個允許滑動的最大值; - 監聽原生
touchend
事件,若此時元素滑動達到最大值,則觸發callback
,同時將translateY
重設為0
,元素回到初始位置。
示例。查看鏈接:下拉刷新demo(PC瀏覽器需調成手機模擬器模式查看)
<main> <p class="refreshText"></p> <ul id="refreshContainer"> <li>111</li> <li>222</li> <li>333</li> <li>444</li> <li>555</li> ... </ul> </main>
(function(window) { var _element = document.getElementById('refreshContainer'), _refreshText = document.querySelector('.refreshText'), _startPos = 0, _transitionHeight = 0; _element.addEventListener('touchstart', function(e) { console.log('初始位置:', e.touches[0].pageY); _startPos = e.touches[0].pageY; _element.style.position = 'relative'; _element.style.transition = 'transform 0s'; }, false); _element.addEventListener('touchmove', function(e) { console.log('當前位置:', e.touches[0].pageY); _transitionHeight = e.touches[0].pageY - _startPos; if (_transitionHeight > 0 && _transitionHeight < 60) { _refreshText.innerText = '下拉刷新'; _element.style.transform = 'translateY('+_transitionHeight+'px)'; if (_transitionHeight > 55) { _refreshText.innerText = '釋放更新'; } } }, false); _element.addEventListener('touchend', function(e) { _element.style.transition = 'transform 0.5s ease 1s'; _element.style.transform = 'translateY(0px)'; _refreshText.innerText = '更新中...'; // todo... }, false); })(window);
在下拉到松手的過程中,經歷了三個狀態:
- 當前手勢滑動位置與初始位置差值大於零時,提示正在進行下拉刷新操作;
- 下拉到一定值時,顯示松手釋放后的操作提示;
- 下拉到達設定最大值松手時,執行回調,提示正在進行更新操作。
上拉加載
上拉加載更多數據是在頁面滾動時觸發的動作,一般是頁面滾動到底部時觸發,也可以選擇在滾動到一定位置的時候觸發。
以滾動到頁面底部為例。實現原理是分別獲得當前滾動條的scrollTop
值、當前可視范圍的高度值clientHeight
以及文檔的總高度scrollHeight
。當scrollTop
和clientHeight
的值之和大於等於scrollHeight
時,觸發callback
。
示例。查看鏈接:上拉加載demo
<main> <ul id="refreshContainer"> <li>111</li> <li>222</li> <li>333</li> <li>444</li> <li>555</li> ... </ul> <p class="refreshText"></p> </main>
(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', '<li>new add...</li>'); }, 1000); } window.onscroll = function() { if (getScrollTop() + getClientHeight() == getScrollHeight()) { _text.innerText = '加載中...'; throttle(fetchData); } }; })(window);
頁面綁定onscroll
事件時加入了節流函數,其作用就是忽略滾動條300毫秒內的連續多次觸發。
小結
上拉刷新的實現主要依靠的是touch
事件的三個階段,以及借助CSS3動畫效果;下拉加載主要依靠頁面的onscroll
事件,需要注意的是頁面滾動時可能要考慮函數節流。
轉:https://www.cnblogs.com/zuobaiquan01/p/8874305.html