IScroll5 参数说明和调用方法 https://my.oschina.net/huqiji/blog/895532
官方一个例子: http://pnc.co.il/dev/iscroll-5-pull-to-refresh-and-infinite-demo.html
iscroll5 上下拉动刷新 http://www.cnblogs.com/mrxia/p/3868126.html
iscroll5的上拉加载和下拉刷新
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 |
<!-- 这个是空白模板.ftl --> <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="description" content=""> <meta name="keywords" content=""> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"> <meta name="renderer" content="webkit"> <meta http-equiv="Cache-Control" content="no-siteapp"/> <title>${title}</title> <style type="text/css"></style> <link rel="stylesheet" type="text/css" href="${contextPath}/pages/views/cms/templates/ganlee/css/template.css?pv=${buildTime}"> <script type="text/javascript" src="${contextPath}/script/plugins/iscroll/build/iscroll-probe.js?pv=${buildTime}"></script> <style type="text/css"> .iscroll-page-wrapper { position: absolute; z-index: 1; top: 45px; bottom: 48px; left: 9999px; width: 100%; background: #ccc; overflow: hidden; } .iscroll-page-scroller { position: absolute; z-index: 1; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); width: 100%; -webkit-transform: translateZ(0); -moz-transform: translateZ(0); -ms-transform: translateZ(0); -o-transform: translateZ(0); transform: translateZ(0); -webkit-touch-callout: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; -webkit-text-size-adjust: none; -moz-text-size-adjust: none; -ms-text-size-adjust: none; -o-text-size-adjust: none; text-size-adjust: none; } .iscroll-page-scroller ul { list-style: none; padding: 0; margin: 0; width: 100%; text-align: left; } .iscroll-page-scroller li { padding: 0 10px; height: 80px; line-height: 80px; border-bottom: 1px solid #ccc; border-top: 1px solid #fff; background-color: #fafafa; font-size: 14px; } .pullDownLabel, .pullUpLabel { color: #999 } .pullDown, .pullUp { background: #fff; height: 40px; line-height: 40px; font-weight: bold; font-size: 0.8em; color: #888 } .pullDown .pullDownIcon, .pullUp .pullUpIcon { display: block; float: left; opacity: 0.4; width: 40px; height: 40px; background: url(/pages/views/cms/templates/ganlee/images/pull_to_refresh.png) 0 0 no-repeat; -webkit-background-size: 40px 80px; -ms-background-size: 40px 80px; background-size: 40px 80px; -webkit-transition-property: -webkit-transform; -ms-transition-property: -webkit-transform; -webkit-transition-duration: 250ms; -ms-transition-duration: 250ms } .pullDown .pullDownIcon { -webkit-transform: rotate(0deg) translateZ(0); -ms-transform: rotate(0deg) translateZ(0) } .pullUp .pullUpIcon { -webkit-transform: rotate(-180deg) translateZ(0); -ms-transform: rotate(-180deg) translateZ(0) } .pullDown.flip .pullDownIcon { -webkit-transform: rotate(-180deg) translateZ(0); -ms-transform: rotate(-180deg) translateZ(0) } .pullUp.flip .pullUpIcon { -webkit-transform: rotate(0deg) translateZ(0); -ms-transform: rotate(0deg) translateZ(0) } .pullDown.loading .pullDownIcon, .pullUp.loading .pullUpIcon { background-position: 0 100%; -webkit-transform: rotate(0deg) translateZ(0); -ms-transform: rotate(0deg) translateZ(0); -webkit-transition-duration: 0ms; -ms-transition-duration: 0ms; -webkit-animation-name: loading; -ms-animation-name: loading; -webkit-animation-duration: 1s; -ms-animation-duration: 1s; -webkit-animation-iteration-count: infinite; -ms-animation-iteration-count: infinite; -webkit-animation-timing-function: linear; -ms-animation-timing-function: linear } @-webkit-keyframes loading { from { -webkit-transform: rotate(0deg) translateZ(0) } to { -webkit-transform: rotate(360deg) translateZ(0) } } @-ms-keyframes loading { from { -ms-transform: rotate(0deg) translateZ(0) } to { -ms-transform: rotate(360deg) translateZ(0) } } </style> <script type="text/javascript"> var items_per_page = 10; var scroll_in_progress = false; var myScroll; var scrollerUl = null; var pullActionDetect = { count: 0, limit: 10, check: function (count) { if (count) { pullActionDetect.count = 0; } // Detects whether the momentum has stopped, and if it has reached the end - 200px of the scroller - it trigger the pullUpAction setTimeout(function () { if (myScroll.y <= (myScroll.maxScrollY + 200) && pullUpEl && !pullUpEl.className.match('loading')) { //提示载入分页数据 $('.pullUp').addClass('loading').html( '<span class="pullUpIcon"> ' + '</span><span class="pullUpLabel">正在载入...</span>' ); pullUpAction(); } else if (pullActionDetect.count < pullActionDetect.limit) { pullActionDetect.check(); pullActionDetect.count++; } }, 200); } } function load_content(refresh, next_page) { var date = new Date(); var dataStr = date.Format("yyyy-mm-dd hh:MM:ss"); console.log(refresh, next_page); setTimeout(function () { if (!refresh) { console.log('载入初始化, 没传入任何信息'); for (var i = 0; i < items_per_page; i++) { scrollerUl.append('<li>[' + dataStr + '] O 载入初始化[' + i + ']</li>'); } } else if (refresh && !next_page) { console.log('下拉刷新的数据,传入刷新命令,没传入下一页信息'); scrollerUl.html(''); for (var i = 0; i < items_per_page; i++) { scrollerUl.append('<li>[' + dataStr + '] V 下拉刷新的数据[' + i + ']</li>'); } } else if (refresh && next_page) { console.log('向上滑动的翻页,传入刷新命令,传入下一页信息'); for (var i = 0; i < items_per_page; i++) { scrollerUl.append('<li>[' + dataStr + '] A 向上滑动的翻页[' + i + ']</li>'); } } if (refresh) { myScroll.refresh(); pullActionCallback(); } else { if (myScroll) { myScroll.destroy(); $(myScroll.scroller).attr('style', ''); // Required since the styles applied by IScroll might conflict with transitions of parent layers. myScroll = null; } trigger_myScroll(); } }, 1000); } function pullDownAction() { console.log("下拉刷新"); load_content('refresh'); scrollerUl.data('page', 1); // Since "topOffset" is not supported with iscroll-5 $('#wrapper > .scroller').css({top: 0}); } function pullUpAction(callback) { console.log("上拉翻页加载"); if (scrollerUl.data('page')) { var next_page = parseInt(scrollerUl.data('page'), 10) + 1; } else { var next_page = 2; } load_content('refresh', next_page); scrollerUl.data('page', next_page); if (callback) { callback(); } } function pullActionCallback() { if (pullDownEl && pullDownEl.className.match('loading')) { pullDownEl.className = 'pullDown'; pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新'; myScroll.scrollTo(0, parseInt(pullUpOffset) * (-1), 200); } else if (pullUpEl && pullUpEl.className.match('loading')) { $('.pullUp').removeClass('loading').html(''); } } function trigger_myScroll(offset) { pullDownEl = document.querySelector('#wrapper .pullDown'); if (pullDownEl) { pullDownOffset = pullDownEl.offsetHeight; } else { pullDownOffset = 0; } pullUpEl = document.querySelector('#wrapper .pullUp'); if (pullUpEl) { pullUpOffset = pullUpEl.offsetHeight; } else { pullUpOffset = 0; } if ($('#wrapper ul > li').length < items_per_page) { // If we have only 1 page of result - we hide the pullup and pulldown indicators. $('#wrapper .pullDown').hide(); $('#wrapper .pullUp span').hide(); offset = 0; } else if (!offset) { // If we have more than 1 page of results and offset is not manually defined - we set it to be the pullUpOffset. offset = pullUpOffset; } myScroll = new IScroll('#wrapper', { probeType: 1, tap: true, click: false, preventDefaultException: {tagName: /.*/}, mouseWheel: true, scrollbars: true, fadeScrollbars: true, interactiveScrollbars: false, keyBindings: false, deceleration: 0.0002, startY: (parseInt(offset) * (-1)) }); myScroll.on('scrollStart', function () { console.log('滚动开始'); scroll_in_progress = true; }); myScroll.on('scroll', function () { scroll_in_progress = true; if ($('#wrapper ul > li').length >= items_per_page) { if (this.y >= 5 && pullDownEl && !pullDownEl.className.match('flip')) { pullDownEl.className = 'pullDown flip'; pullDownEl.querySelector('.pullDownLabel').innerHTML = '准备刷新'; this.minScrollY = 0; } else if (this.y <= 5 && pullDownEl && pullDownEl.className.match('flip')) { pullDownEl.className = 'pullDown'; pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新'; this.minScrollY = -pullDownOffset; } console.log(this.y); pullActionDetect.check(0); } }); myScroll.on('scrollEnd', function () { console.log('滚动完成'); setTimeout(function () { scroll_in_progress = false; }, 100); if ($('#wrapper ul > li').length >= items_per_page) { if (pullDownEl && pullDownEl.className.match('flip')) { pullDownEl.className = 'pullDown loading'; pullDownEl.querySelector('.pullDownLabel').innerHTML = '正在载入...'; pullDownAction(); } //如果到达结束-加载下一页 pullActionDetect.check(0); } }); //为了触发之前,为了避免看到“下拉刷新” setTimeout(function () { $('#wrapper').css({left: 0}); }, 100); } $(function () { scrollerUl = $('#wrapper > #scroller > ul'); //这里不知道有什么用 document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); load_content(); }); </script> </head> <body> <div id="wrapper" class="iscroll-page-wrapper"> <div id="scroller" class="iscroll-page-scroller"> <div class="pullDown"> <span class="pullDownIcon"> </span> <span class="pullDownLabel">往下拉就刷新</span> </div> <ul></ul> <div class="pullUp"></div> </div> </div> </body> </html> |
通过Scroller.js制作上拉加载和下拉刷新
http://my.oschina.net/wolfx/blog/632549
之前做移动端webAPP开发,一直是用的IScroll来做滚动列表,但是IScroll没有直接提供上下拉刷新的功能,虽然我们基于IScroll自己实现了一套,但IScroll依然有不少bug.就拿点击来说吧,在某些型号的手机上会连续触发2次click.原因是配置项的{click:true}对某些手机有效,某些手机无效.
最近在网上发现了Scroller.js这个库,看了一下API,我们需要的功能都有,以下是一个可上下拉刷新的滚动列表实现:
注:上下拉刷新是Scroller.js的插件功能,所以官方文档没有详细的API