qiaowei | f044a74 | 2019-07-10 16:04:20 +0800 | [diff] [blame^] | 1 | /** |
| 2 | * aui-pull-refresh.js |
| 3 | * @author 流浪男 |
| 4 | * @todo more things to abstract, e.g. Loading css etc. |
| 5 | * Licensed under the MIT license. |
| 6 | * http://www.opensource.org/licenses/mit-license.php |
| 7 | */ |
| 8 | (function(window) { |
| 9 | 'use strict'; |
| 10 | /** |
| 11 | * Extend obj function |
| 12 | * |
| 13 | * This is an object extender function. It allows us to extend an object |
| 14 | * by passing in additional variables and overwriting the defaults. |
| 15 | */ |
| 16 | var auiPullToRefresh = function (params,callback) { |
| 17 | this.extend(this.params, params); |
| 18 | this._init(callback); |
| 19 | } |
| 20 | var touchYDelta; |
| 21 | var isLoading = false; |
| 22 | var docElem = window.document.documentElement, |
| 23 | loadWrapH, |
| 24 | win = {width: window.innerWidth, height: window.innerHeight}, |
| 25 | winfactor= 0.2, |
| 26 | translateVal, |
| 27 | isMoved = false, |
| 28 | firstTouchY, initialScroll; |
| 29 | auiPullToRefresh.prototype = { |
| 30 | params: { |
| 31 | container: document.querySelector('.aui-refresh-content'), |
| 32 | friction: 2.5, |
| 33 | triggerDistance: 100, |
| 34 | callback:false |
| 35 | }, |
| 36 | _init : function(callback) { |
| 37 | var self = this; |
| 38 | var loadingHtml = '<div class="aui-refresh-load"><div class="aui-refresh-pull-arrow"></div></div>'; |
| 39 | self.params.container.insertAdjacentHTML('afterbegin', loadingHtml); |
| 40 | self.params.container.addEventListener('touchstart', function(ev){ |
| 41 | self.touchStart(ev) |
| 42 | }); |
| 43 | self.params.container.addEventListener('touchmove', function(ev){ |
| 44 | self.touchMove(ev) |
| 45 | }); |
| 46 | self.params.container.addEventListener('touchend', function(ev){ |
| 47 | self.touchEnd(ev,callback); |
| 48 | }); |
| 49 | }, |
| 50 | touchStart : function(ev) { |
| 51 | // this.params.container.classList.remove("refreshing"); |
| 52 | if (isLoading) { |
| 53 | return; |
| 54 | } |
| 55 | isMoved = false; |
| 56 | this.params.container.style.webkitTransitionDuration = |
| 57 | this.params.container.style.transitionDuration = '0ms'; |
| 58 | touchYDelta = ''; |
| 59 | var touchobj = ev.changedTouches[0]; |
| 60 | // register first touch "y" |
| 61 | firstTouchY = parseInt(touchobj.clientY); |
| 62 | initialScroll = this.scrollY(); |
| 63 | }, |
| 64 | touchMove : function (ev) { |
| 65 | if (isLoading) { |
| 66 | ev.preventDefault(); |
| 67 | return; |
| 68 | } |
| 69 | var self = this; |
| 70 | var moving = function() { |
| 71 | var touchobj = ev.changedTouches[0], // reference first touch point for this event |
| 72 | touchY = parseInt(touchobj.clientY); |
| 73 | touchYDelta = touchY - firstTouchY; |
| 74 | if ( self.scrollY() === 0 && touchYDelta > 0 ) { |
| 75 | ev.preventDefault(); |
| 76 | } |
| 77 | if ( initialScroll > 0 || self.scrollY() > 0 || self.scrollY() === 0 && touchYDelta < 0 ) { |
| 78 | firstTouchY = touchY; |
| 79 | return; |
| 80 | } |
| 81 | translateVal = Math.pow(touchYDelta, 0.85); |
| 82 | self.params.container.style.webkitTransform = self.params.container.style.transform = 'translate3d(0, ' + translateVal + 'px, 0)'; |
| 83 | isMoved = true; |
| 84 | if(touchYDelta > self.params.triggerDistance){ |
| 85 | self.params.container.classList.add("aui-refresh-pull-up"); |
| 86 | self.params.container.classList.remove("aui-refresh-pull-down"); |
| 87 | }else{ |
| 88 | self.params.container.classList.add("aui-refresh-pull-down"); |
| 89 | self.params.container.classList.remove("aui-refresh-pull-up"); |
| 90 | } |
| 91 | }; |
| 92 | this.throttle(moving(), 20); |
| 93 | }, |
| 94 | touchEnd : function (ev,callback) { |
| 95 | var self =this; |
| 96 | if (isLoading|| !isMoved) { |
| 97 | isMoved = false; |
| 98 | return; |
| 99 | } |
| 100 | // 根据下拉高度判断是否加载 |
| 101 | if( touchYDelta >= this.params.triggerDistance) { |
| 102 | isLoading = true; //正在加载中 |
| 103 | ev.preventDefault(); |
| 104 | this.params.container.style.webkitTransitionDuration = |
| 105 | this.params.container.style.transitionDuration = '300ms'; |
| 106 | this.params.container.style.webkitTransform = |
| 107 | this.params.container.style.transform = 'translate3d(0,60px,0)'; |
| 108 | document.querySelector(".aui-refresh-pull-arrow").style.webkitTransitionDuration = |
| 109 | document.querySelector(".aui-refresh-pull-arrow").style.transitionDuration = '0ms'; |
| 110 | self.params.container.classList.add("aui-refreshing"); |
| 111 | if(callback){ |
| 112 | callback({ |
| 113 | status:"success" |
| 114 | }); |
| 115 | } |
| 116 | }else{ |
| 117 | this.params.container.style.webkitTransitionDuration = |
| 118 | this.params.container.style.transitionDuration = '300ms'; |
| 119 | this.params.container.style.webkitTransform = |
| 120 | this.params.container.style.transform = 'translate3d(0,0,0)'; |
| 121 | if(callback){ |
| 122 | callback({ |
| 123 | status:"fail" |
| 124 | }); |
| 125 | } |
| 126 | } |
| 127 | isMoved = false; |
| 128 | return; |
| 129 | }, |
| 130 | cancelLoading : function () { |
| 131 | var self =this; |
| 132 | isLoading = false; |
| 133 | self.params.container.classList.remove("aui-refreshing"); |
| 134 | document.querySelector(".aui-refresh-pull-arrow").style.webkitTransitionDuration = |
| 135 | document.querySelector(".aui-refresh-pull-arrow").style.transitionDuration = '300ms'; |
| 136 | this.params.container.style.webkitTransitionDuration = |
| 137 | this.params.container.style.transitionDuration = '0ms'; |
| 138 | self.params.container.style.webkitTransform = |
| 139 | self.params.container.style.transform = 'translate3d(0,0,0)'; |
| 140 | self.params.container.classList.remove("aui-refresh-pull-up"); |
| 141 | self.params.container.classList.add("aui-refresh-pull-down"); |
| 142 | return; |
| 143 | }, |
| 144 | scrollY : function() { |
| 145 | return window.pageYOffset || docElem.scrollTop; |
| 146 | }, |
| 147 | throttle : function(fn, delay) { |
| 148 | var allowSample = true; |
| 149 | return function(e) { |
| 150 | if (allowSample) { |
| 151 | allowSample = false; |
| 152 | setTimeout(function() { allowSample = true; }, delay); |
| 153 | fn(e); |
| 154 | } |
| 155 | }; |
| 156 | }, |
| 157 | winresize : function () { |
| 158 | var resize = function() { |
| 159 | win = {width: window.innerWidth, height: window.innerHeight}; |
| 160 | }; |
| 161 | throttle(resize(), 10); |
| 162 | }, |
| 163 | extend: function(a, b) { |
| 164 | for (var key in b) { |
| 165 | if (b.hasOwnProperty(key)) { |
| 166 | a[key] = b[key]; |
| 167 | } |
| 168 | } |
| 169 | return a; |
| 170 | } |
| 171 | } |
| 172 | window.auiPullToRefresh = auiPullToRefresh; |
| 173 | |
| 174 | })(window); |