Skip to content
This repository has been archived by the owner on Dec 12, 2018. It is now read-only.

Commit

Permalink
fix various snap errors, fix cubiq#377
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Spinelli committed Jun 10, 2013
1 parent 398b625 commit 2b6960a
Show file tree
Hide file tree
Showing 16 changed files with 326 additions and 129 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ iScroll comes with a custom build system. The required nodejs packages are inclu
./build.js dist

The compiled scripts will be saved in the /build and /dist directories.

Script released under the MIT license.
20 changes: 14 additions & 6 deletions build/iscroll-lite.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! iScroll v5.0.0-beta.1 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
/*! iScroll v5.0.0-beta.2 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
var IScroll = (function (window, document, Math) {

var rAF = window.requestAnimationFrame ||
Expand Down Expand Up @@ -235,7 +235,6 @@ function IScroll (el, options) {
startX: 0,
startY: 0,
scrollY: true,
lockDirection: true,
directionLockThreshold: 5,
momentum: true,

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

// With eventPassthrough we also need lockDirection mechanism
this.options.lockDirection = this.options.lockDirection || this.options.eventPassthrough;
this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;

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

IScroll.prototype = {
version: '5.0.0-beta.1',
version: '5.0.0-beta.2',

_init: function () {
this._initEvents();
Expand Down Expand Up @@ -392,13 +391,13 @@ IScroll.prototype = {
}

// If you are scrolling in one direction lock the other
if ( !this.directionLocked && this.options.lockDirection ) {
if ( !this.directionLocked && !this.options.freeScroll ) {
if ( absDistX > absDistY + this.options.directionLockThreshold ) {
this.directionLocked = 'h'; // lock horizontally
} else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
this.directionLocked = 'v'; // lock vertically
} else {
this.directionLocked = 0; // no lock
this.directionLocked = 'n'; // no lock
}
}

Expand Down Expand Up @@ -470,6 +469,8 @@ IScroll.prototype = {
time = 0,
easing = '';

this.scrollTo(newX, newY); // ensures that the last position is rounded

this.isInTransition = 0;
this.initiated = 0;
this.endTime = utils.getTime();
Expand Down Expand Up @@ -563,6 +564,13 @@ IScroll.prototype = {
y = this.maxScrollY;
}

if ( this.options.snap ) {
var snap = this._nearestSnap(x, y);
this.currentPage = snap;
x = snap.x;
y = snap.y;
}

this.scrollTo(x, y, time, this.options.bounceEasing);

return true;
Expand Down
60 changes: 46 additions & 14 deletions build/iscroll-probe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! iScroll v5.0.0-beta.1 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
/*! iScroll v5.0.0-beta.2 ~ (c) 2008-2013 Matteo Spinelli ~ http://cubiq.org/license */
var IScroll = (function (window, document, Math) {

var rAF = window.requestAnimationFrame ||
Expand Down Expand Up @@ -239,7 +239,6 @@ function IScroll (el, options) {
startX: 0,
startY: 0,
scrollY: true,
lockDirection: true,
directionLockThreshold: 5,
momentum: true,

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

// With eventPassthrough we also need lockDirection mechanism
this.options.lockDirection = this.options.lockDirection || this.options.eventPassthrough;
this.options.freeScroll = this.options.freeScroll && !this.options.eventPassthrough;
this.options.directionLockThreshold = this.options.eventPassthrough ? 0 : this.options.directionLockThreshold;

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

IScroll.prototype = {
version: '5.0.0-beta.1',
version: '5.0.0-beta.2',

_init: function () {
this._initEvents();
Expand Down Expand Up @@ -417,13 +416,13 @@ IScroll.prototype = {
}

// If you are scrolling in one direction lock the other
if ( !this.directionLocked && this.options.lockDirection ) {
if ( !this.directionLocked && !this.options.freeScroll ) {
if ( absDistX > absDistY + this.options.directionLockThreshold ) {
this.directionLocked = 'h'; // lock horizontally
} else if ( absDistY >= absDistX + this.options.directionLockThreshold ) {
this.directionLocked = 'v'; // lock vertically
} else {
this.directionLocked = 0; // no lock
this.directionLocked = 'n'; // no lock
}
}

Expand Down Expand Up @@ -501,6 +500,8 @@ IScroll.prototype = {
time = 0,
easing = '';

this.scrollTo(newX, newY); // ensures that the last position is rounded

this.isInTransition = 0;
this.initiated = 0;
this.endTime = utils.getTime();
Expand Down Expand Up @@ -594,6 +595,13 @@ IScroll.prototype = {
y = this.maxScrollY;
}

if ( this.options.snap ) {
var snap = this._nearestSnap(x, y);
this.currentPage = snap;
x = snap.x;
y = snap.y;
}

this.scrollTo(x, y, time, this.options.bounceEasing);

return true;
Expand Down Expand Up @@ -966,6 +974,10 @@ IScroll.prototype = {
this.pages = [];
this.currentPage = {};

if ( typeof this.options.snap == 'string' ) {
this.options.snap = this.scroller.querySelectorAll(this.options.snap);
}

this.on('refresh', function () {
var i = 0, l,
m = 0, n,
Expand All @@ -979,12 +991,12 @@ IScroll.prototype = {
cx = Math.round( stepX / 2 );
cy = Math.round( stepY / 2 );

while ( x >= -this.scrollerWidth ) {
while ( x > -this.scrollerWidth ) {
this.pages[i] = [];
l = 0;
y = 0;

while ( y >= -this.scrollerHeight ) {
while ( y > -this.scrollerHeight ) {
this.pages[i][l] = {
x: Math.max(x, this.maxScrollX),
y: Math.max(y, this.maxScrollY),
Expand All @@ -1005,7 +1017,7 @@ IScroll.prototype = {
n = -1;

for ( ; i < l; i++ ) {
if ( el[i].offsetLeft === 0 ) {
if ( i === 0 || el[i].offsetLeft < el[i-1].offsetLeft ) {
m = 0;
n++;
}
Expand Down Expand Up @@ -1050,6 +1062,18 @@ IScroll.prototype = {
return this.currentPage;
}

if ( x > 0 ) {
x = 0;
} else if ( x < this.maxScrollX ) {
x = this.maxScrollX;
}

if ( y > 0 ) {
y = 0;
} else if ( y < this.maxScrollY ) {
y = this.maxScrollY;
}

for ( ; i < l; i++ ) {
if ( x >= this.pages[i][0].cx ) {
x = this.pages[i][0].x;
Expand Down Expand Up @@ -1101,7 +1125,7 @@ IScroll.prototype = {
goToPage: function (x, y, time, easing) {
if ( x >= this.pages.length ) {
x = this.pages.length - 1;
} else if ( x < 0) {
} else if ( x < 0 ) {
x = 0;
}

Expand Down Expand Up @@ -1135,8 +1159,12 @@ IScroll.prototype = {
var x = this.currentPage.pageX,
y = this.currentPage.pageY;

x += this.hasHorizontalScroll ? 1 : 0;
y += this.hasVericalScroll ? 1 : 0;
x++;

if ( x >= this.pages.length && this.hasVericalScroll ) {
x = 0;
y++;
}

this.goToPage(x, y, time, easing);
},
Expand All @@ -1145,8 +1173,12 @@ IScroll.prototype = {
var x = this.currentPage.pageX,
y = this.currentPage.pageY;

x -= this.hasHorizontalScroll ? 1 : 0;
y -= this.hasVericalScroll ? 1 : 0;
x--;

if ( x < 0 && this.hasVericalScroll ) {
x = 0;
y--;
}

this.goToPage(x, y, time, easing);
},
Expand Down
Loading

0 comments on commit 2b6960a

Please sign in to comment.