Skip to content

Commit

Permalink
Fixed issue#15 by removing iscroll's touchstart e.preventDefault. Upd…
Browse files Browse the repository at this point in the history
…ated to iscroll 4.0 release.
  • Loading branch information
thomasyip committed Mar 11, 2011
1 parent fc71892 commit 7a66f4a
Showing 1 changed file with 88 additions and 61 deletions.
149 changes: 88 additions & 61 deletions extensions/iscroll/iscroll.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
/**
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
* iScroll v4.0 Beta 3
* iScroll v4.0 Beta 4
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* Copyright (c) 2010 Matteo Spinelli, http://cubiq.org/
* Released under MIT license
* http://cubiq.org/dropbox/mit-license.txt
*
* Last updated: 2011.03.06
* Last updated: 2011.03.10
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
Expand Down Expand Up @@ -50,7 +50,8 @@ function iScroll (el, options) {
onScrollStart: null,
onScrollEnd: null,
onZoomStart: null,
onZoomEnd: null
onZoomEnd: null,
checkDOMChange: false // Experimental
};

// User defined options
Expand Down Expand Up @@ -113,6 +114,10 @@ function iScroll (el, options) {
if (!hasTouch) {
that._bind('mousewheel');
}

if (that.options.checkDOMChange) {
that.DOMChangeInterval = setInterval(function () { that._checkSize(); }, 250);
}
}

iScroll.prototype = {
Expand Down Expand Up @@ -210,6 +215,21 @@ iScroll.prototype = {
}, 0);
},

_checkSize: function () {
var that = this,
scrollerW,
scrollerH;

if (that.moved || that.zoomed || !that.contentReady) return;

scrollerW = m.round(that.scroller.offsetWidth * that.scale),
scrollerH = m.round((that.scroller.offsetHeight - that.offsetBottom - that.offsetTop) * that.scale);

if (scrollerW == that.scrollerW && scrollerH == that.scrollerH) return;

that.refresh();
},

_pos: function (x, y) {
var that = this;

Expand Down Expand Up @@ -240,7 +260,7 @@ iScroll.prototype = {
if (that[dir + 'ScrollbarIndicatorSize'] + that[dir + 'ScrollbarMaxScroll'] - pos < 9) pos = that[dir + 'ScrollbarIndicatorSize'] + that[dir + 'ScrollbarMaxScroll'] - 8;
}
that[dir + 'ScrollbarWrapper'].style.webkitTransitionDelay = '0';
that[dir + 'ScrollbarWrapper'].style.opacity = hidden ? '0' : '1';
that[dir + 'ScrollbarWrapper'].style.opacity = hidden && that.options.hideScrollbar ? '0' : '1';
that[dir + 'ScrollbarIndicator'].style.webkitTransform = trnOpen + (dir == 'h' ? pos + 'px,0' : '0,' + pos + 'px') + trnClose;
},

Expand All @@ -261,9 +281,9 @@ iScroll.prototype = {

that.moved = false;

e.preventDefault();
//e.preventDefault();

if (hasTouch && e.touches.length == 2 && that.options.zoom && hasGesture) {
if (hasTouch && e.touches.length == 2 && that.options.zoom && hasGesture && !that.zoomed) {
that.originX = m.abs(e.touches[0].pageX + e.touches[1].pageX - that.wrapperOffsetLeft*2) / 2 - that.x;
that.originY = m.abs(e.touches[0].pageY + e.touches[1].pageY - that.wrapperOffsetTop*2) / 2 - that.y;
}
Expand Down Expand Up @@ -313,25 +333,23 @@ iScroll.prototype = {

if (that.options.onScrollStart) that.options.onScrollStart.call(that);

// @TODO -- BeeDesk's change. To allow it to coexist with gestures.js
if (hasTouch && e.touches.length > 1) {
that.scrolling = false;
that.resetPosition();
}

// Registering/unregistering of events is done to preserve resources on Android
setTimeout(function () {
// setTimeout(function () {
// that._unbind(START_EV);
that._bind(MOVE_EV);
that._bind(END_EV);
that._bind(CANCEL_EV);
}, 0);
// }, 0);
},

_move: function (e) {
if (hasTouch && e.touches.length > 1) {
// this.zoomed = true;
return;
}
if (hasTouch && e.touches.length > 1) return;

var that = this,
point = hasTouch ? e.changedTouches[0] : e,
Expand Down Expand Up @@ -520,46 +538,6 @@ iScroll.prototype = {
that._resetPos();
},

_snap: function (x, y) {
var that = this,
i, l,
page, time,
sizeX, sizeY;

// Check page X
page = that.pagesX.length-1;
for (i=0, l=that.pagesX.length; i<l; i++) {
if (x >= that.pagesX[i]) {
page = i;
break;
}
}
if (page == that.currPageX && page > 0 && that.dirX < 0) page--;
x = that.pagesX[page];
sizeX = m.abs(x - that.pagesX[that.currPageX]);
sizeX = sizeX ? m.abs(that.x - x) / sizeX * 500 : 0;
that.currPageX = page;

// Check page Y
page = that.pagesY.length-1;
for (i=0; i<page; i++) {
if (y >= that.pagesY[i]) {
page = i;
break;
}
}
if (page == that.currPageY && page > 0 && that.dirY < 0) page--;
y = that.pagesY[page];
sizeY = m.abs(y - that.pagesY[that.currPageY]);
sizeY = sizeY ? m.abs(that.y - y) / sizeY * 500 : 0;
that.currPageY = page;

// Snap with constant speed (proportional duration)
time = m.round(m.max(sizeX, sizeY)) || 200;

return { x: x, y: y, time: time };
},

_resetPos: function (time) {
var that = this,
resetX = that.x,
Expand Down Expand Up @@ -662,8 +640,7 @@ iScroll.prototype = {

that._transitionTime(0);
that.lastScale = 1;
that.zoomed = false;


if (that.options.onZoomStart) that.options.onZoomStart.call(that);

that._unbind('gesturestart');
Expand Down Expand Up @@ -700,7 +677,7 @@ iScroll.prototype = {
lastScale = that.scale / scale;
that.x = that.originX - that.originX * lastScale + that.x;
that.y = that.originY - that.originY * lastScale + that.y;
// that._transitionTime(200);

that.scroller.style.webkitTransform = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + that.scale + ')';

setTimeout(function () {
Expand Down Expand Up @@ -775,6 +752,46 @@ iScroll.prototype = {
return { x: left, y: top };
},

_snap: function (x, y) {
var that = this,
i, l,
page, time,
sizeX, sizeY;

// Check page X
page = that.pagesX.length-1;
for (i=0, l=that.pagesX.length; i<l; i++) {
if (x >= that.pagesX[i]) {
page = i;
break;
}
}
if (page == that.currPageX && page > 0 && that.dirX < 0) page--;
x = that.pagesX[page];
sizeX = m.abs(x - that.pagesX[that.currPageX]);
sizeX = sizeX ? m.abs(that.x - x) / sizeX * 500 : 0;
that.currPageX = page;

// Check page Y
page = that.pagesY.length-1;
for (i=0; i<page; i++) {
if (y >= that.pagesY[i]) {
page = i;
break;
}
}
if (page == that.currPageY && page > 0 && that.dirY < 0) page--;
y = that.pagesY[page];
sizeY = m.abs(y - that.pagesY[that.currPageY]);
sizeY = sizeY ? m.abs(that.y - y) / sizeY * 500 : 0;
that.currPageY = page;

// Snap with constant speed (proportional duration)
time = m.round(m.max(sizeX, sizeY)) || 200;

return { x: x, y: y, time: time };
},

_bind: function (type, el) {
(el || this.scroller).addEventListener(type, this, false);
},
Expand All @@ -792,6 +809,8 @@ iScroll.prototype = {
destroy: function () {
var that = this;

if (that.options.checkDOMChange) clearTimeout(that.DOMChangeInterval);

// Remove pull to refresh
if (that.pullDownToRefresh) {
that.pullDownEl.parentNode.removeChild(that.pullDownEl);
Expand Down Expand Up @@ -824,15 +843,16 @@ iScroll.prototype = {
that._unbind('gesturecancel');
}
},

refresh: function () {
var that = this,
pos = 0, page = 0,
i, l, els,
oldHeight, offsets;
oldHeight, offsets,
loading;

if (that.pullDownToRefresh) {
var loading = that.pullDownEl.className.match('loading');
loading = that.pullDownEl.className.match('loading');
if (loading && !that.contentReady) {
oldHeight = that.scrollerH;
that.contentReady = true;
Expand All @@ -847,7 +867,7 @@ iScroll.prototype = {
}

if (that.pullUpToRefresh) {
var loading = that.pullUpEl.className.match('loading');
loading = that.pullUpEl.className.match('loading');
if (loading && !that.contentReady) {
oldHeight = that.scrollerH;
that.contentReady = true;
Expand Down Expand Up @@ -883,21 +903,26 @@ iScroll.prototype = {

// Snap
if (typeof that.options.snap == 'string') {
that.pagesX = [];
that.pagesY = [];
els = that.scroller.querySelectorAll(that.options.snap);
for (i=0, l=els.length; i<l; i++) {
pos = that._offset(els[i]);
that.pagesX[i] = pos.x < that.maxScrollX ? that.maxScrollX : pos.x * that.scale;
that.pagesY[i] = pos.y < that.maxScrollY ? that.maxScrollY : pos.y * that.scale;
}
} else if (that.options.snap) {
that.pagesX = [];
while (pos >= that.maxScrollX) {
that.pagesX[page] = pos;
pos = pos - that.wrapperW;
page++;
}
if (that.maxScrollX%that.wrapperW) that.pagesX[that.pagesX.length] = that.maxScrollX - that.pagesX[that.pagesX.length-1] + that.pagesX[that.pagesX.length-1];

pos = page = 0;
pos = 0;
page = 0;
that.pagesY = [];
while (pos >= that.maxScrollY) {
that.pagesY[page] = pos;
pos = pos - that.wrapperH;
Expand Down Expand Up @@ -1024,5 +1049,7 @@ var has3d = 'WebKitCSSMatrix' in window && 'm11' in new WebKitCSSMatrix(),
trnClose = has3d ? ',0)' : ')',
m = Math;

window.iScroll = iScroll;
if (typeof exports !== 'undefined') exports.iScroll = iScroll;
else window.iScroll = iScroll;

})();

0 comments on commit 7a66f4a

Please sign in to comment.