Skip to content

Commit 2b6960a

Browse files
author
Matteo Spinelli
committed
fix various snap errors, fix cubiq#377
1 parent 398b625 commit 2b6960a

File tree

16 files changed

+326
-129
lines changed

16 files changed

+326
-129
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ iScroll comes with a custom build system. The required nodejs packages are inclu
99
./build.js dist
1010

1111
The compiled scripts will be saved in the /build and /dist directories.
12+
13+
Script released under the MIT license.

build/iscroll-lite.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! iScroll v5.0.0-beta.1 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
1+
/*! iScroll v5.0.0-beta.2 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
22
var IScroll = (function (window, document, Math) {
33

44
var rAF = window.requestAnimationFrame ||
@@ -235,7 +235,6 @@ function IScroll (el, options) {
235235
startX: 0,
236236
startY: 0,
237237
scrollY: true,
238-
lockDirection: true,
239238
directionLockThreshold: 5,
240239
momentum: true,
241240

@@ -268,7 +267,7 @@ function IScroll (el, options) {
268267
this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
269268

270269
// With eventPassthrough we also need lockDirection mechanism
271-
this.options.lockDirection = this.options.lockDirection || this.options.eventPassthrough;
270+
this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
272271
this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
273272

274273
this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
@@ -294,7 +293,7 @@ function IScroll (el, options) {
294293
}
295294

296295
IScroll.prototype = {
297-
version: '5.0.0-beta.1',
296+
version: '5.0.0-beta.2',
298297

299298
_init: function () {
300299
this._initEvents();
@@ -392,13 +391,13 @@ IScroll.prototype = {
392391
}
393392

394393
// If you are scrolling in one direction lock the other
395-
if ( !this.directionLocked && this.options.lockDirection ) {
394+
if ( !this.directionLocked && !this.options.freeScroll ) {
396395
if ( absDistX > absDistY + this.options.directionLockThreshold ) {
397396
this.directionLocked = 'h'; // lock horizontally
398397
} else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
399398
this.directionLocked = 'v'; // lock vertically
400399
} else {
401-
this.directionLocked = 0; // no lock
400+
this.directionLocked = 'n'; // no lock
402401
}
403402
}
404403

@@ -470,6 +469,8 @@ IScroll.prototype = {
470469
time = 0,
471470
easing = '';
472471

472+
this.scrollTo(newX, newY); // ensures that the last position is rounded
473+
473474
this.isInTransition = 0;
474475
this.initiated = 0;
475476
this.endTime = utils.getTime();
@@ -563,6 +564,13 @@ IScroll.prototype = {
563564
y = this.maxScrollY;
564565
}
565566

567+
if ( this.options.snap ) {
568+
var snap = this._nearestSnap(x, y);
569+
this.currentPage = snap;
570+
x = snap.x;
571+
y = snap.y;
572+
}
573+
566574
this.scrollTo(x, y, time, this.options.bounceEasing);
567575

568576
return true;

build/iscroll-probe.js

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*! iScroll v5.0.0-beta.1 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
1+
/*! iScroll v5.0.0-beta.2 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
22
var IScroll = (function (window, document, Math) {
33

44
var rAF = window.requestAnimationFrame ||
@@ -239,7 +239,6 @@ function IScroll (el, options) {
239239
startX: 0,
240240
startY: 0,
241241
scrollY: true,
242-
lockDirection: true,
243242
directionLockThreshold: 5,
244243
momentum: true,
245244

@@ -272,7 +271,7 @@ function IScroll (el, options) {
272271
this.options.scrollX = this.options.eventPassthrough == 'horizontal' ? false : this.options.scrollX;
273272

274273
// With eventPassthrough we also need lockDirection mechanism
275-
this.options.lockDirection = this.options.lockDirection || this.options.eventPassthrough;
274+
this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
276275
this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;
277276

278277
this.options.bounceEasing = typeof this.options.bounceEasing == 'string' ? utils.ease[this.options.bounceEasing] || utils.ease.circular : this.options.bounceEasing;
@@ -303,7 +302,7 @@ function IScroll (el, options) {
303302
}
304303

305304
IScroll.prototype = {
306-
version: '5.0.0-beta.1',
305+
version: '5.0.0-beta.2',
307306

308307
_init: function () {
309308
this._initEvents();
@@ -417,13 +416,13 @@ IScroll.prototype = {
417416
}
418417

419418
// If you are scrolling in one direction lock the other
420-
if ( !this.directionLocked && this.options.lockDirection ) {
419+
if ( !this.directionLocked && !this.options.freeScroll ) {
421420
if ( absDistX > absDistY + this.options.directionLockThreshold ) {
422421
this.directionLocked = 'h'; // lock horizontally
423422
} else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
424423
this.directionLocked = 'v'; // lock vertically
425424
} else {
426-
this.directionLocked = 0; // no lock
425+
this.directionLocked = 'n'; // no lock
427426
}
428427
}
429428

@@ -501,6 +500,8 @@ IScroll.prototype = {
501500
time = 0,
502501
easing = '';
503502

503+
this.scrollTo(newX, newY); // ensures that the last position is rounded
504+
504505
this.isInTransition = 0;
505506
this.initiated = 0;
506507
this.endTime = utils.getTime();
@@ -594,6 +595,13 @@ IScroll.prototype = {
594595
y = this.maxScrollY;
595596
}
596597

598+
if ( this.options.snap ) {
599+
var snap = this._nearestSnap(x, y);
600+
this.currentPage = snap;
601+
x = snap.x;
602+
y = snap.y;
603+
}
604+
597605
this.scrollTo(x, y, time, this.options.bounceEasing);
598606

599607
return true;
@@ -966,6 +974,10 @@ IScroll.prototype = {
966974
this.pages = [];
967975
this.currentPage = {};
968976

977+
if ( typeof this.options.snap == 'string' ) {
978+
this.options.snap = this.scroller.querySelectorAll(this.options.snap);
979+
}
980+
969981
this.on('refresh', function () {
970982
var i = 0, l,
971983
m = 0, n,
@@ -979,12 +991,12 @@ IScroll.prototype = {
979991
cx = Math.round( stepX / 2 );
980992
cy = Math.round( stepY / 2 );
981993

982-
while ( x >= -this.scrollerWidth ) {
994+
while ( x > -this.scrollerWidth ) {
983995
this.pages[i] = [];
984996
l = 0;
985997
y = 0;
986998

987-
while ( y >= -this.scrollerHeight ) {
999+
while ( y > -this.scrollerHeight ) {
9881000
this.pages[i][l] = {
9891001
x: Math.max(x, this.maxScrollX),
9901002
y: Math.max(y, this.maxScrollY),
@@ -1005,7 +1017,7 @@ IScroll.prototype = {
10051017
n = -1;
10061018

10071019
for ( ; i < l; i++ ) {
1008-
if ( el[i].offsetLeft === 0 ) {
1020+
if ( i === 0 || el[i].offsetLeft < el[i-1].offsetLeft ) {
10091021
m = 0;
10101022
n++;
10111023
}
@@ -1050,6 +1062,18 @@ IScroll.prototype = {
10501062
return this.currentPage;
10511063
}
10521064

1065+
if ( x > 0 ) {
1066+
x = 0;
1067+
} else if ( x < this.maxScrollX ) {
1068+
x = this.maxScrollX;
1069+
}
1070+
1071+
if ( y > 0 ) {
1072+
y = 0;
1073+
} else if ( y < this.maxScrollY ) {
1074+
y = this.maxScrollY;
1075+
}
1076+
10531077
for ( ; i < l; i++ ) {
10541078
if ( x >= this.pages[i][0].cx ) {
10551079
x = this.pages[i][0].x;
@@ -1101,7 +1125,7 @@ IScroll.prototype = {
11011125
goToPage: function (x, y, time, easing) {
11021126
if ( x >= this.pages.length ) {
11031127
x = this.pages.length - 1;
1104-
} else if ( x < 0) {
1128+
} else if ( x < 0 ) {
11051129
x = 0;
11061130
}
11071131

@@ -1135,8 +1159,12 @@ IScroll.prototype = {
11351159
var x = this.currentPage.pageX,
11361160
y = this.currentPage.pageY;
11371161

1138-
x += this.hasHorizontalScroll ? 1 : 0;
1139-
y += this.hasVericalScroll ? 1 : 0;
1162+
x++;
1163+
1164+
if ( x >= this.pages.length && this.hasVericalScroll ) {
1165+
x = 0;
1166+
y++;
1167+
}
11401168

11411169
this.goToPage(x, y, time, easing);
11421170
},
@@ -1145,8 +1173,12 @@ IScroll.prototype = {
11451173
var x = this.currentPage.pageX,
11461174
y = this.currentPage.pageY;
11471175

1148-
x -= this.hasHorizontalScroll ? 1 : 0;
1149-
y -= this.hasVericalScroll ? 1 : 0;
1176+
x--;
1177+
1178+
if ( x < 0 && this.hasVericalScroll ) {
1179+
x = 0;
1180+
y--;
1181+
}
11501182

11511183
this.goToPage(x, y, time, easing);
11521184
},

0 commit comments

Comments
 (0)