在這我不想說其他寫的如何如何,雖然本人不是很會css3,但是修改倒是沒問題網上說的iscroll5全改了從4升級5要改動內部代碼。
我了個神,修改別人的ok的源碼萬一哪天又要更誰知道你改的那個。
這個iscroll5的示例包本身就不用改,就能支持上下拉動刷新只不過導入的不是基礎的iscroll.js,導入的是iscroll-probe.js
我這里用的jquery是1.8 至於css什么的別關心了,只是把別人寫好的拿來改改增強用戶體驗而已。別說什么
topOffset 這個屬性沒有什么的,我這就ok。
網上寫的拉動刷新是由bug的,當拉動后在快速接着拉動,這時候會執行兩次刷新方法和動畫我這里解決了這問題
當然我試過修改isScroll.options.xxx屬性想禁止滾動。等待我開啟滾動。
記住導入isScroll5的js是iscroll-probe.js
- var myScroll;
- var pullDownEl, pullDownL;
- var pullUpEl, pullUpL;
- var Downcount = 0 ,Upcount = 0;
- var loadingStep = 0;//加載狀態0默認,1顯示加載狀態,2執行加載數據,只有當為0時才能再次加載,這是防止過快拉動刷新
- function pullDownAction() {//下拉事件
- setTimeout(function() {
- var el, li, i;
- el = $('#add');
- for (i = 0; i < 3; i++) {
- li = $("<li></li>");
- Downcount++;
- li.text('new Add ' + Downcount + " !");
- el.prepend(li);
- }
- pullDownEl.removeClass('loading');
- pullDownL.html('下拉顯示更多...');
- pullDownEl['class'] = pullDownEl.attr('class');
- pullDownEl.attr('class','').hide();
- myScroll.refresh();
- loadingStep = 0;
- }, 1000); //1秒
- }
- function pullUpAction() {//上拉事件
- setTimeout(function() {
- var el, li, i;
- el = $('#add');
- for (i = 0; i < 3; i++) {
- li = $("<li></li>");
- Upcount++;
- li.text('new Add ' + Upcount + " !");
- el.append(li);
- }
- pullUpEl.removeClass('loading');
- pullUpL.html('上拉顯示更多...');
- pullUpEl['class'] = pullUpEl.attr('class');
- pullUpEl.attr('class','').hide();
- myScroll.refresh();
- loadingStep = 0;
- }, 1000);
- }
- function loaded() {
- pullDownEl = $('#pullDown');
- pullDownL = pullDownEl.find('.pullDownLabel');
- pullDownEl['class'] = pullDownEl.attr('class');
- pullDownEl.attr('class','').hide();
- pullUpEl = $('#pullUp');
- pullUpL = pullUpEl.find('.pullUpLabel');
- pullUpEl['class'] = pullUpEl.attr('class');
- pullUpEl.attr('class','').hide();
- myScroll = new IScroll('#content', {
- probeType: 2,//probeType:1對性能沒有影響。在滾動事件被觸發時,滾動軸是不是忙着做它的東西。probeType:2總執行滾動,除了勢頭,反彈過程中的事件。這類似於原生的onscroll事件。probeType:3發出的滾動事件與到的像素精度。注意,滾動被迫requestAnimationFrame(即:useTransition:假)。
- scrollbars: true,//有滾動條
- mouseWheel: true,//允許滑輪滾動
- fadeScrollbars: true,//滾動時顯示滾動條,默認影藏,並且是淡出淡入效果
- bounce:true,//邊界反彈
- interactiveScrollbars:true,//滾動條可以拖動
- shrinkScrollbars:'scale',// 當滾動邊界之外的滾動條是由少量的收縮。'clip' or 'scale'.
- click: true ,// 允許點擊事件
- keyBindings:true,//允許使用按鍵控制
- momentum:true// 允許有慣性滑動
- });
- //滾動時
- myScroll.on('scroll', function(){
- if(loadingStep == 0 && !pullDownEl.attr('class').match('flip|loading') && !pullUpEl.attr('class').match('flip|loading')){
- if (this.y > 5) {
- //下拉刷新效果
- pullDownEl.attr('class',pullUpEl['class'])
- pullDownEl.show();
- myScroll.refresh();
- pullDownEl.addClass('flip');
- pullDownL.html('准備刷新...');
- loadingStep = 1;
- }else if (this.y < (this.maxScrollY - 5)) {
- //上拉刷新效果
- pullUpEl.attr('class',pullUpEl['class'])
- pullUpEl.show();
- myScroll.refresh();
- pullUpEl.addClass('flip');
- pullUpL.html('准備刷新...');
- loadingStep = 1;
- }
- }
- });
- //滾動完畢
- myScroll.on('scrollEnd',function(){
- if(loadingStep == 1){
- if (pullUpEl.attr('class').match('flip|loading')) {
- pullUpEl.removeClass('flip').addClass('loading');
- pullUpL.html('Loading...');
- loadingStep = 2;
- pullUpAction();
- }else if(pullDownEl.attr('class').match('flip|loading')){
- pullDownEl.removeClass('flip').addClass('loading');
- pullDownL.html('Loading...');
- loadingStep = 2;
- pullDownAction();
- }
- }
- });
- }
- document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);
css,還是原封不動的
- * {
- -webkit-box-sizing: border-box;
- -moz-box-sizing: border-box;
- box-sizing: border-box;
- }
- html {
- -ms-touch-action: none;
- }
- body,ul,li {
- padding: 0;
- margin: 0;
- border: 0;
- }
- body {
- font-size: 12px;
- font-family: ubuntu, helvetica, arial;
- overflow: hidden;
- /* this is important to prevent the whole page to bounce */
- }
- #header {
- position: absolute;
- z-index: 2;
- top: 0;
- left: 0;
- width: 100%;
- height: 45px;
- line-height: 45px;
- background: #CD235C;
- padding: 0;
- color: #eee;
- font-size: 20px;
- text-align: center;
- font-weight: bold;
- }
- #footer {
- position: absolute;
- z-index: 2;
- bottom: 0;
- left: 0;
- width: 100%;
- height: 48px;
- background: #444;
- padding: 0;
- border-top: 1px solid #444;
- }
- #content {
- position: absolute;
- z-index: 1;
- top: 45px;
- bottom: 48px;
- left: 0;
- width: 100%;
- background: #ccc;
- overflow: hidden;
- }
- #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;
- }
- #scroller ul {
- list-style: none;
- padding: 0;
- margin: 0;
- width: 100%;
- text-align: left;
- }
- #scroller li {
- padding: 0 10px;
- height: 40px;
- line-height: 40px;
- border-bottom: 1px solid #ccc;
- border-top: 1px solid #fff;
- background-color: #fafafa;
- font-size: 14px;
- }
- #pullDown,#pullUp {
- height: 40px;
- line-height: 40px;
- padding: 5px 10px;
- font-weight: bold;
- font-size: 14px;
- color: #888;
- }
- #pullDown .pullDownIcon,#pullUp .pullUpIcon {
- display: block;
- float: left;
- width: 40px;
- height: 40px;
- background: url(img/pull-icon@2x.png) 0 0 no-repeat;
- -webkit-background-size: 40px 80px;
- background-size: 40px 80px;
- -webkit-transition-property: -webkit-transform;
- -webkit-transition-duration: 250ms;
- }
- #pullDown .pullDownIcon {
- -webkit-transform: rotate(0deg) translateZ(0);
- }
- #pullUp .pullUpIcon {
- -webkit-transform: rotate(-180deg) translateZ(0);
- }
- #pullDown.flip .pullDownIcon {
- -webkit-transform: rotate(-180deg) translateZ(0);
- }
- #pullUp.flip .pullUpIcon {
- -webkit-transform: rotate(0deg) translateZ(0);
- }
- #pullDown.loading .pullDownIcon,#pullUp.loading .pullUpIcon {
- background-position: 0 100%;
- -webkit-transform: rotate(0deg) translateZ(0);
- -webkit-transition-duration: 0ms;
- -webkit-animation-name: loading;
- -webkit-animation-duration: 2s;
- -webkit-animation-iteration-count: infinite;
- -webkit-animation-timing-function: linear;
- }
- .-webkit-keyframes loading {
- from {
- -webkit-transform:rotate(0deg)translateZ(0);
- }
- .to {
- -webkit-transform: rotate(360deg) translateZ(0);
- }
- }
html代碼塊
- <body onload="loaded()">
- <div id="header">iScroll</div>
- <div id="content">
- <div id="scroller">
- <div id="pullDown" class="ub ub-pc c-gra">
- <div class="pullDownIcon"></div>
- <div class="pullDownLabel">下拉刷新</div>
- </div>
- <ul id="add">
- <li>Pretty row 1</li>
- <li>Pretty row 2</li>
- <li>Pretty row 3</li>
- <li>Pretty row 4</li>
- <li>Pretty row 5</li>
- <li>Pretty row 6</li>
- <li>Pretty row 7</li>
- <li>Pretty row 8</li>
- <li>Pretty row 9</li>
- <li>Pretty row 10</li>
- <li>Pretty row 11</li>
- <li>Pretty row 12</li>
- <li>Pretty row 13</li>
- <li>Pretty row 14</li>
- <li>Pretty row 15</li>
- <li>Pretty row 16</li>
- <li>Pretty row 17</li>
- <li>Pretty row 18</li>
- <li>Pretty row 19</li>
- <li>Pretty row 20</li>
- <li>Pretty row 21</li>
- <li>Pretty row 22</li>
- <li>Pretty row 23</li>
- <li>Pretty row 24</li>
- <li>Pretty row 25</li>
- <li>Pretty row 26</li>
- <li>Pretty row 27</li>
- <li>Pretty row 28</li>
- <li>Pretty row 29</li>
- <li>Pretty row 30</li>
- <li>Pretty row 31</li>
- <li>Pretty row 32</li>
- <li>Pretty row 33</li>
- <li>Pretty row 34</li>
- <li>Pretty row 35</li>
- <li>Pretty row 36</li>
- <li>Pretty row 37</li>
- <li>Pretty row 38</li>
- <li>Pretty row 39</li>
- <li>Pretty row 40</li>
- <li>Pretty row 41</li>
- <li>Pretty row 42</li>
- <li>Pretty row 43</li>
- <li>Pretty row 44</li>
- <li>Pretty row 45</li>
- <li>Pretty row 46</li>
- <li>Pretty row 47</li>
- <li>Pretty row 48</li>
- <li>Pretty row 49</li>
- <li>Pretty row 50</li>
- </ul>
- <div id="pullUp" class="ub ub-pc c-gra">
- <div class="pullUpIcon"></div>
- <div class="pullUpLabel">上拉顯示更多...</div>
- </div>
- </div>
- </div>
- <div id="footer"></div>
- </body>