| 1 | |
| 2 | _initSnap: function () { |
| 3 | this.currentPage = {}; |
| 4 | |
| 5 | if ( typeof this.options.snap == 'string' ) { |
| 6 | this.options.snap = this.scroller.querySelectorAll(this.options.snap); |
| 7 | } |
| 8 | |
| 9 | this.on('refresh', function () { |
| 10 | var i = 0, l, |
| 11 | m = 0, n, |
| 12 | cx, cy, |
| 13 | x = 0, y, |
| 14 | stepX = this.options.snapStepX || this.wrapperWidth, |
| 15 | stepY = this.options.snapStepY || this.wrapperHeight, |
| 16 | el; |
| 17 | |
| 18 | this.pages = []; |
| 19 | |
| 20 | if ( !this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight ) { |
| 21 | return; |
| 22 | } |
| 23 | |
| 24 | if ( this.options.snap === true ) { |
| 25 | cx = Math.round( stepX / 2 ); |
| 26 | cy = Math.round( stepY / 2 ); |
| 27 | |
| 28 | while ( x > -this.scrollerWidth ) { |
| 29 | this.pages[i] = []; |
| 30 | l = 0; |
| 31 | y = 0; |
| 32 | |
| 33 | while ( y > -this.scrollerHeight ) { |
| 34 | this.pages[i][l] = { |
| 35 | x: Math.max(x, this.maxScrollX), |
| 36 | y: Math.max(y, this.maxScrollY), |
| 37 | width: stepX, |
| 38 | height: stepY, |
| 39 | cx: x - cx, |
| 40 | cy: y - cy |
| 41 | }; |
| 42 | |
| 43 | y -= stepY; |
| 44 | l++; |
| 45 | } |
| 46 | |
| 47 | x -= stepX; |
| 48 | i++; |
| 49 | } |
| 50 | } else { |
| 51 | el = this.options.snap; |
| 52 | l = el.length; |
| 53 | n = -1; |
| 54 | |
| 55 | for ( ; i < l; i++ ) { |
| 56 | if ( i === 0 || el[i].offsetLeft <= el[i-1].offsetLeft ) { |
| 57 | m = 0; |
| 58 | n++; |
| 59 | } |
| 60 | |
| 61 | if ( !this.pages[m] ) { |
| 62 | this.pages[m] = []; |
| 63 | } |
| 64 | |
| 65 | x = Math.max(-el[i].offsetLeft, this.maxScrollX); |
| 66 | y = Math.max(-el[i].offsetTop, this.maxScrollY); |
| 67 | cx = x - Math.round(el[i].offsetWidth / 2); |
| 68 | cy = y - Math.round(el[i].offsetHeight / 2); |
| 69 | |
| 70 | this.pages[m][n] = { |
| 71 | x: x, |
| 72 | y: y, |
| 73 | width: el[i].offsetWidth, |
| 74 | height: el[i].offsetHeight, |
| 75 | cx: cx, |
| 76 | cy: cy |
| 77 | }; |
| 78 | |
| 79 | if ( x > this.maxScrollX ) { |
| 80 | m++; |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | this.goToPage(this.currentPage.pageX || 0, this.currentPage.pageY || 0, 0); |
| 86 | |
| 87 | // Update snap threshold if needed |
| 88 | if ( this.options.snapThreshold % 1 === 0 ) { |
| 89 | this.snapThresholdX = this.options.snapThreshold; |
| 90 | this.snapThresholdY = this.options.snapThreshold; |
| 91 | } else { |
| 92 | this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * this.options.snapThreshold); |
| 93 | this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * this.options.snapThreshold); |
| 94 | } |
| 95 | }); |
| 96 | |
| 97 | this.on('flick', function () { |
| 98 | var time = this.options.snapSpeed || Math.max( |
| 99 | Math.max( |
| 100 | Math.min(Math.abs(this.x - this.startX), 1000), |
| 101 | Math.min(Math.abs(this.y - this.startY), 1000) |
| 102 | ), 300); |
| 103 | |
| 104 | this.goToPage( |
| 105 | this.currentPage.pageX + this.directionX, |
| 106 | this.currentPage.pageY + this.directionY, |
| 107 | time |
| 108 | ); |
| 109 | }); |
| 110 | }, |
| 111 | |
| 112 | _nearestSnap: function (x, y) { |
| 113 | if ( !this.pages.length ) { |
| 114 | return { x: 0, y: 0, pageX: 0, pageY: 0 }; |
| 115 | } |
| 116 | |
| 117 | var i = 0, |
| 118 | l = this.pages.length, |
| 119 | m = 0; |
| 120 | |
| 121 | // Check if we exceeded the snap threshold |
| 122 | if ( Math.abs(x - this.absStartX) < this.snapThresholdX && |
| 123 | Math.abs(y - this.absStartY) < this.snapThresholdY ) { |
| 124 | return this.currentPage; |
| 125 | } |
| 126 | |
| 127 | if ( x > 0 ) { |
| 128 | x = 0; |
| 129 | } else if ( x < this.maxScrollX ) { |
| 130 | x = this.maxScrollX; |
| 131 | } |
| 132 | |
| 133 | if ( y > 0 ) { |
| 134 | y = 0; |
| 135 | } else if ( y < this.maxScrollY ) { |
| 136 | y = this.maxScrollY; |
| 137 | } |
| 138 | |
| 139 | for ( ; i < l; i++ ) { |
| 140 | if ( x >= this.pages[i][0].cx ) { |
| 141 | x = this.pages[i][0].x; |
| 142 | break; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | l = this.pages[i].length; |
| 147 | |
| 148 | for ( ; m < l; m++ ) { |
| 149 | if ( y >= this.pages[0][m].cy ) { |
| 150 | y = this.pages[0][m].y; |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if ( i == this.currentPage.pageX ) { |
| 156 | i += this.directionX; |
| 157 | |
| 158 | if ( i < 0 ) { |
| 159 | i = 0; |
| 160 | } else if ( i >= this.pages.length ) { |
| 161 | i = this.pages.length - 1; |
| 162 | } |
| 163 | |
| 164 | x = this.pages[i][0].x; |
| 165 | } |
| 166 | |
| 167 | if ( m == this.currentPage.pageY ) { |
| 168 | m += this.directionY; |
| 169 | |
| 170 | if ( m < 0 ) { |
| 171 | m = 0; |
| 172 | } else if ( m >= this.pages[0].length ) { |
| 173 | m = this.pages[0].length - 1; |
| 174 | } |
| 175 | |
| 176 | y = this.pages[0][m].y; |
| 177 | } |
| 178 | |
| 179 | return { |
| 180 | x: x, |
| 181 | y: y, |
| 182 | pageX: i, |
| 183 | pageY: m |
| 184 | }; |
| 185 | }, |
| 186 | |
| 187 | goToPage: function (x, y, time, easing) { |
| 188 | easing = easing || this.options.bounceEasing; |
| 189 | |
| 190 | if ( x >= this.pages.length ) { |
| 191 | x = this.pages.length - 1; |
| 192 | } else if ( x < 0 ) { |
| 193 | x = 0; |
| 194 | } |
| 195 | |
| 196 | if ( y >= this.pages[x].length ) { |
| 197 | y = this.pages[x].length - 1; |
| 198 | } else if ( y < 0 ) { |
| 199 | y = 0; |
| 200 | } |
| 201 | |
| 202 | var posX = this.pages[x][y].x, |
| 203 | posY = this.pages[x][y].y; |
| 204 | |
| 205 | time = time === undefined ? this.options.snapSpeed || Math.max( |
| 206 | Math.max( |
| 207 | Math.min(Math.abs(posX - this.x), 1000), |
| 208 | Math.min(Math.abs(posY - this.y), 1000) |
| 209 | ), 300) : time; |
| 210 | |
| 211 | this.currentPage = { |
| 212 | x: posX, |
| 213 | y: posY, |
| 214 | pageX: x, |
| 215 | pageY: y |
| 216 | }; |
| 217 | |
| 218 | this.scrollTo(posX, posY, time, easing); |
| 219 | }, |
| 220 | |
| 221 | next: function (time, easing) { |
| 222 | var x = this.currentPage.pageX, |
| 223 | y = this.currentPage.pageY; |
| 224 | |
| 225 | x++; |
| 226 | |
| 227 | if ( x >= this.pages.length && this.hasVerticalScroll ) { |
| 228 | x = 0; |
| 229 | y++; |
| 230 | } |
| 231 | |
| 232 | this.goToPage(x, y, time, easing); |
| 233 | }, |
| 234 | |
| 235 | prev: function (time, easing) { |
| 236 | var x = this.currentPage.pageX, |
| 237 | y = this.currentPage.pageY; |
| 238 | |
| 239 | x--; |
| 240 | |
| 241 | if ( x < 0 && this.hasVerticalScroll ) { |
| 242 | x = 0; |
| 243 | y--; |
| 244 | } |
| 245 | |
| 246 | this.goToPage(x, y, time, easing); |
| 247 | }, |