您好,登錄后才能下訂單哦!
ys_scroll.css
.ys-scroll-wrapper{ position:relative; overflow: auto; -webkit-overflow-scrolling: touch; } .ys-scroll-wrapper .ys-scroll-content{ min-height:100%; } /* loading-top */ .ys-scroll-wrapper .loading-top { display:block; width:100%; height:40px; line-height: 40px; text-align: center; } /* loading-bottom */ .ys-scroll-wrapper .loading-bottom { display:block; width:100%; height:40px; line-height: 40px; text-align: center; } /* loading-bottom */ .ys-scroll-wrapper .loading-end { display:none; width:100%; height:40px; line-height: 40px; text-align: center; }
ys_scroll.js
(function($){ var defaultSettings = { hasArrivedEnd:false,// 是否已經到底部 pulldownCallback:function(){ // 下拉回調 console.log("pull down ... "); }, pullupCallback:function(){ // 上拉回調 console.log("pull up ... "); } }; function render(target,settings){ var container = target; var childrenNode = $(container).children().remove(); $(container).addClass("ys-scroll-wrapper"); $(container).append("<div class='loading-top'>加載中...</div>"); $(container).append("<div class='ys-scroll-content'></div>"); $(container).append("<div class='loading-bottom'>加載更多...</div>"); $(container).append("<div class='loading-end'>已經到底了</div>"); $(container).find(".ys-scroll-content").append(childrenNode); $(container).scrollTop(LOADING_BAR_HEIGHT); $(container).data("settings",settings); return container; } function bindEventHandlers(target,container,settings){ var loadingTopRevertTimeout = null; var loadingTopEndTimeout = null; var loadingBottomRevertTimeout = null; var loadingBottomTimeout = null; var pulldownCallback = settings.pulldownCallback; var pullupCallback = settings.pullupCallback; function clearTimeouts(){ clearTimeout(loadingTopRevertTimeout); clearTimeout(loadingTopEndTimeout); clearTimeout(loadingBottomRevertTimeout); clearTimeout(loadingBottomTimeout); } function scrollHandler(){ clearTimeouts(); var loadingTop = false; if(arriveLoadingTopStart(container)){ loadingTopRevertTimeout = setTimeout(function(){ loadingTopRevert(container); },RESPONSE_DELAY); loadingTop = true; } if(arriveLoadingTopEnd(container)){ clearTimeouts(); loadingTopEndTimeout = setTimeout(function(){ resetLoading(container); pulldownCallback(); },RESPONSE_DELAY); loadingTop = true; } var hasArrivedEnd = $(container).data("settings").hasArrivedEnd; if(hasArrivedEnd||loadingTop){ // 如果已經到底了不需要額外操作 return; } if(arriveLoadingBottomStart(container)){ loadingBottomRevertTimeout = setTimeout(function(){ loadingBottomRevert(container); },RESPONSE_DELAY); } if(arriveLoadingBottomEnd(container)){ clearTimeouts(); loadingBottomTimeout = setTimeout(function(){ pullupCallback(); },RESPONSE_DELAY) } } var hasPressed = false; $(container).on("touchstart",function(event){ hasPressed = true; clearTimeouts(); stopAnimation(container); }); $(container).on("touchmove",function(event){ hasPressed = true; clearTimeouts(); stopAnimation(container); var clientX = event.originalEvent.changedTouches[0].clientX; var clientY = event.originalEvent.changedTouches[0].clientY; if(clientY<0||clientX<0){ scrollHandler(); } }); $(container).on("touchend touchcancel",function(event){ scrollHandler(); hasPressed = false; }); $(container).scroll(function(){ if(hasPressed||reverting){ return; } scrollHandler(); }); } var RESPONSE_DELAY = 800; // 觸發延遲 var ANIMATION_DURATION = 300; // 動畫持續時間 var LOADING_BAR_HEIGHT = 40; // loading bar 高度 var reverting = false; // 動畫正在恢復 function stopAnimation(container){ $(container).stop(true); reverting = false; } function loadingTopRevert(container){ reverting = true; $(container).animate({ "scrollTop":LOADING_BAR_HEIGHT+"px" },ANIMATION_DURATION,function(){ reverting = false; }); } /* 判斷達到 Loading-top開始處 */ function arriveLoadingTopStart(container){ var scrollTop = $(container).scrollTop(); if(scrollTop<LOADING_BAR_HEIGHT){ return true; } return false; } /* 判斷達到 Loading-top結束處 */ function arriveLoadingTopEnd(container){ var scrollTop = $(container).scrollTop(); if (scrollTop<1) { return true; } return false; } function loadingBottomRevert(container){ reverting = true; var clientHeight = $(container).height(); var scrollTop = $(container)[0].scrollHeight-LOADING_BAR_HEIGHT-clientHeight; $(container).animate({ "scrollTop":scrollTop+"px" },ANIMATION_DURATION,function(){ reverting = false; }); } /* 判斷到達 Loading Bottom 開始處 */ function arriveLoadingBottomStart(container){ var scrollTop = $(container).scrollTop(); var scrollHeight = $(container)[0].scrollHeight; var clientHeight = $(container).height(); if (scrollTop + clientHeight+LOADING_BAR_HEIGHT>= scrollHeight) { return true; } return false; } /* 判斷到達 Loading Bottom 結束處 */ function arriveLoadingBottomEnd(container){ var scrollTop = $(container).scrollTop(); var scrollHeight = $(container)[0].scrollHeight; var clientHeight = $(container).height(); if (scrollTop + clientHeight+1> scrollHeight) { return true; } return false; } /* 重設 loading */ function resetLoading(container){ $(container).data("settings").hasArrivedEnd=false; $(container).find(".loading-bottom").show(); $(container).find(".loading-end").hide(); } /* 設置loading end */ function setLoadingEnd(container){ $(container).data("settings").hasArrivedEnd=true; $(container).find(".loading-bottom").hide(); $(container).find(".loading-end").show(); } var options = { ysScroll:function(settings) { var mergedSettings = {}; $.extend(mergedSettings,defaultSettings,settings); $(this).each(function(){ var container = render(this,settings); $(container).data("settings",settings); bindEventHandlers(this,container,mergedSettings); }); }, ysScrollRefresh:function(){ if(arriveLoadingTopStart(this)){ loadingTopRevert(this); } if(arriveLoadingBottomStart(this)){ loadingBottomRevert(this); } }, ysScrollLoadEnd:function(){ setLoadingEnd(this); } }; $.fn.extend(options); })(jQuery);
測試頁面 scroll.html
<!DOCTYPE html> <!--[if lt IE 7]> <html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]--> <!--[if IE 7]> <html class="no-js lt-ie9 lt-ie8"> <![endif]--> <!--[if IE 8]> <html class="no-js lt-ie9"> <![endif]--> <!--[if gt IE 8]><!--> <html class="no-js"> <!--<![endif]--> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title></title> <meta name="description" content=""> <meta name="viewport" content="width=device-width, initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"> <meta name="format-detection" content="telephone=no"> <link href="css/common/ys_scroll.css" rel="stylesheet"> <style> html,body,.container{ height:100%; margin:0; padding:0; } </style> </head> <body> <div class="container"> <ul> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> <li>這是測試數據...</li> </ul> </div> <script src="dist/js/jquery-1.11.3.min.js"></script> <script src="js/common/ys_scroll.js"></script> <script> var dataHtml = $(".container ul").html(); $(".container ").ysScroll({ pulldownCallback:function(){ // 下拉回調 console.log("pull down ... "); $(".container ul").html(dataHtml); $(".container ").ysScrollRefresh(); }, pullupCallback:function(){ // 上拉回調 console.log("pull up ... "); $(".container ul").append(dataHtml); $(".container ").ysScrollRefresh(); } }); </script> </body> </html>
注:容器使用前要給其設置一個固定的高度。當觸發pulldownCallback,pullupCallback回調后一定要使用$(".container").ysScrollRefresh();重置容器。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。