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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 | 1× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 12× 12× 12× 12× 12× 12× 12× 12× 12× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 4× 1× 4× 4× 4× 1× 1× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 1× 4× 4× 2× 2× 2× 1× 5× 5× 5× 5× 5× 5× 5× 1× 3× 3× 3× 3× 3× 3× 3× 1× 2× 2× 2× 2× | import { getRect, prepend, removeChild } from '../util/dom' import { ease } from '../util/ease' import { extend } from '../util/lang' import { warn } from '../util/debug' export function snapMixin(BScroll) { BScroll.prototype._initSnap = function () { this.currentPage = {} const snap = this.options.snap Iif (snap.loop) { let children = this.scroller.children if (children.length > 1) { prepend(children[children.length - 1].cloneNode(true), this.scroller) this.scroller.appendChild(children[1].cloneNode(true)) } else { // Loop does not make any sense if there is only one child. snap.loop = false } } let el = snap.el Iif (typeof el === 'string') { el = this.scroller.querySelectorAll(el) } this.on('refresh', () => { this.pages = [] Iif (!this.wrapperWidth || !this.wrapperHeight || !this.scrollerWidth || !this.scrollerHeight) { return } let stepX = snap.stepX || this.wrapperWidth let stepY = snap.stepY || this.wrapperHeight let x = 0 let y let cx let cy let i = 0 let l let m = 0 let n let rect Eif (!el) { cx = Math.round(stepX / 2) cy = Math.round(stepY / 2) while (x > -this.scrollerWidth) { this.pages[i] = [] l = 0 y = 0 while (y > -this.scrollerHeight) { this.pages[i][l] = { x: Math.max(x, this.maxScrollX), y: Math.max(y, this.maxScrollY), width: stepX, height: stepY, cx: x - cx, cy: y - cy } y -= stepY l++ } x -= stepX i++ } } else { l = el.length n = -1 for (; i < l; i++) { rect = getRect(el[i]) if (i === 0 || rect.left <= getRect(el[i - 1]).left) { m = 0 n++ } if (!this.pages[m]) { this.pages[m] = [] } x = Math.max(-rect.left, this.maxScrollX) y = Math.max(-rect.top, this.maxScrollY) cx = x - Math.round(rect.width / 2) cy = y - Math.round(rect.height / 2) this.pages[m][n] = { x: x, y: y, width: rect.width, height: rect.height, cx: cx, cy: cy } if (x > this.maxScrollX) { m++ } } } this._checkSnapLoop() let initPageX = snap._loopX ? 1 : 0 let initPageY = snap._loopY ? 1 : 0 this._goToPage(this.currentPage.pageX || initPageX, this.currentPage.pageY || initPageY, 0, undefined, true) // Update snap threshold if needed. const snapThreshold = snap.threshold Iif (snapThreshold % 1 === 0) { this.snapThresholdX = snapThreshold this.snapThresholdY = snapThreshold } else { this.snapThresholdX = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].width * snapThreshold) this.snapThresholdY = Math.round(this.pages[this.currentPage.pageX][this.currentPage.pageY].height * snapThreshold) } }) this.on('scrollEnd', () => { if (snap.loop) { if (snap._loopX) { if (this.currentPage.pageX === 0) { this._goToPage(this.pages.length - 2, this.currentPage.pageY, 0, undefined, true) } if (this.currentPage.pageX === this.pages.length - 1) { this._goToPage(1, this.currentPage.pageY, 0, undefined, true) } } else { if (this.currentPage.pageY === 0) { this._goToPage(this.currentPage.pageX, this.pages[0].length - 2, 0, undefined, true) } if (this.currentPage.pageY === this.pages[0].length - 1) { this._goToPage(this.currentPage.pageX, 1, 0, undefined, true) } } } }) Eif (snap.listenFlick !== false) { this.on('flick', () => { let time = snap.speed || Math.max( Math.max( Math.min(Math.abs(this.x - this.startX), 1000), Math.min(Math.abs(this.y - this.startY), 1000) ), 300) this._goToPage( this.currentPage.pageX + this.directionX, this.currentPage.pageY + this.directionY, time ) }) } this.on('destroy', () => { Iif (snap.loop) { let children = this.scroller.children if (children.length > 2) { removeChild(this.scroller, children[children.length - 1]) removeChild(this.scroller, children[0]) } } }) } BScroll.prototype._checkSnapLoop = function () { const snap = this.options.snap Eif (!snap.loop || !this.pages || !this.pages.length) { return } if (this.pages.length > 1) { snap._loopX = true } if (this.pages[0] && this.pages[0].length > 1) { snap._loopY = true } if (snap._loopX && snap._loopY) { warn('Loop does not support two direction at the same time.') } } BScroll.prototype._nearestSnap = function (x, y) { if (!this.pages.length) { return {x: 0, y: 0, pageX: 0, pageY: 0} } let i = 0 // Check if we exceeded the snap threshold if (Math.abs(x - this.absStartX) <= this.snapThresholdX && Math.abs(y - this.absStartY) <= this.snapThresholdY) { return this.currentPage } if (x > this.minScrollX) { x = this.minScrollX } else if (x < this.maxScrollX) { x = this.maxScrollX } if (y > this.minScrollY) { y = this.minScrollY } else if (y < this.maxScrollY) { y = this.maxScrollY } let l = this.pages.length for (; i < l; i++) { if (x >= this.pages[i][0].cx) { x = this.pages[i][0].x break } } l = this.pages[i].length let m = 0 for (; m < l; m++) { if (y >= this.pages[0][m].cy) { y = this.pages[0][m].y break } } if (i === this.currentPage.pageX) { i += this.directionX if (i < 0) { i = 0 } else if (i >= this.pages.length) { i = this.pages.length - 1 } x = this.pages[i][0].x } if (m === this.currentPage.pageY) { m += this.directionY if (m < 0) { m = 0 } else if (m >= this.pages[0].length) { m = this.pages[0].length - 1 } y = this.pages[0][m].y } return { x, y, pageX: i, pageY: m } } BScroll.prototype._goToPage = function (x, y = 0, time, easing, isSilent) { const snap = this.options.snap Iif (!snap || !this.pages || !this.pages.length) { return } easing = easing || snap.easing || ease.bounce Iif (x >= this.pages.length) { x = this.pages.length - 1 } else Iif (x < 0) { x = 0 } Iif (!this.pages[x]) { return } Iif (y >= this.pages[x].length) { y = this.pages[x].length - 1 } else Iif (y < 0) { y = 0 } let posX = this.pages[x][y].x let posY = this.pages[x][y].y time = time === undefined ? snap.speed || Math.max( Math.max( Math.min(Math.abs(posX - this.x), 1000), Math.min(Math.abs(posY - this.y), 1000) ), 300) : time this.currentPage = { x: posX, y: posY, pageX: x, pageY: y } this.scrollTo(posX, posY, time, easing, isSilent) } BScroll.prototype.goToPage = function (x, y, time, easing) { const snap = this.options.snap if (!snap || !this.pages || !this.pages.length) { return } Iif (snap.loop) { let len if (snap._loopX) { len = this.pages.length - 2 if (x >= len) { x = len - 1 } else if (x < 0) { x = 0 } x += 1 } else { len = this.pages[0].length - 2 if (y >= len) { y = len - 1 } else if (y < 0) { y = 0 } y += 1 } } this._goToPage(x, y, time, easing) } BScroll.prototype.next = function (time, easing) { const snap = this.options.snap Iif (!snap) { return } let x = this.currentPage.pageX let y = this.currentPage.pageY x++ Iif (x >= this.pages.length && this.hasVerticalScroll) { x = 0 y++ } this._goToPage(x, y, time, easing) } BScroll.prototype.prev = function (time, easing) { const snap = this.options.snap Iif (!snap) { return } let x = this.currentPage.pageX let y = this.currentPage.pageY x-- Iif (x < 0 && this.hasVerticalScroll) { x = 0 y-- } this._goToPage(x, y, time, easing) } BScroll.prototype.getCurrentPage = function () { const snap = this.options.snap Iif (!snap) { return null } Iif (snap.loop) { let currentPage if (snap._loopX) { currentPage = extend({}, this.currentPage, { pageX: this.currentPage.pageX - 1 }) } else { currentPage = extend({}, this.currentPage, { pageY: this.currentPage.pageY - 1 }) } return currentPage } return this.currentPage } } |