iscroll
Version:
Smooth scrolling for the web
171 lines (128 loc) • 4.17 kB
JavaScript
_initZoom: function () {
this.scrollerStyle[utils.style.transformOrigin] = '0 0';
},
_zoomStart: function (e) {
var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ),
c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY );
this.touchesDistanceStart = Math.sqrt(c1 * c1 + c2 * c2);
this.startScale = this.scale;
this.originX = Math.abs(e.touches[0].pageX + e.touches[1].pageX) / 2 + this.wrapperOffset.left - this.x;
this.originY = Math.abs(e.touches[0].pageY + e.touches[1].pageY) / 2 + this.wrapperOffset.top - this.y;
this._execEvent('zoomStart');
},
_zoom: function (e) {
if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
return;
}
if ( this.options.preventDefault ) {
e.preventDefault();
}
var c1 = Math.abs( e.touches[0].pageX - e.touches[1].pageX ),
c2 = Math.abs( e.touches[0].pageY - e.touches[1].pageY ),
distance = Math.sqrt( c1 * c1 + c2 * c2 ),
scale = 1 / this.touchesDistanceStart * distance * this.startScale,
lastScale,
x, y;
this.scaled = true;
if ( scale < this.options.zoomMin ) {
scale = 0.5 * this.options.zoomMin * Math.pow(2.0, scale / this.options.zoomMin);
} else if ( scale > this.options.zoomMax ) {
scale = 2.0 * this.options.zoomMax * Math.pow(0.5, this.options.zoomMax / scale);
}
lastScale = scale / this.startScale;
x = this.originX - this.originX * lastScale + this.startX;
y = this.originY - this.originY * lastScale + this.startY;
this.scale = scale;
this.scrollTo(x, y, 0);
},
_zoomEnd: function (e) {
if ( !this.enabled || utils.eventType[e.type] !== this.initiated ) {
return;
}
if ( this.options.preventDefault ) {
e.preventDefault();
}
var newX, newY,
lastScale;
this.isInTransition = 0;
this.initiated = 0;
if ( this.scale > this.options.zoomMax ) {
this.scale = this.options.zoomMax;
} else if ( this.scale < this.options.zoomMin ) {
this.scale = this.options.zoomMin;
}
// Update boundaries
this.refresh();
lastScale = this.scale / this.startScale;
newX = this.originX - this.originX * lastScale + this.startX;
newY = this.originY - this.originY * lastScale + this.startY;
if ( newX > 0 ) {
newX = 0;
} else if ( newX < this.maxScrollX ) {
newX = this.maxScrollX;
}
if ( newY > 0 ) {
newY = 0;
} else if ( newY < this.maxScrollY ) {
newY = this.maxScrollY;
}
if ( this.x != newX || this.y != newY ) {
this.scrollTo(newX, newY, this.options.bounceTime);
}
this.scaled = false;
this._execEvent('zoomEnd');
},
zoom: function (scale, x, y, time) {
if ( scale < this.options.zoomMin ) {
scale = this.options.zoomMin;
} else if ( scale > this.options.zoomMax ) {
scale = this.options.zoomMax;
}
if ( scale == this.scale ) {
return;
}
var relScale = scale / this.scale;
x = x === undefined ? this.wrapperWidth / 2 : x;
y = y === undefined ? this.wrapperHeight / 2 : y;
time = time === undefined ? 300 : time;
x = x + this.wrapperOffset.left - this.x;
y = y + this.wrapperOffset.top - this.y;
x = x - x * relScale + this.x;
y = y - y * relScale + this.y;
this.scale = scale;
this.refresh(); // update boundaries
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;
}
this.scrollTo(x, y, time);
},
_wheelZoom: function (e) {
var wheelDeltaY,
deltaScale,
that = this;
// Execute the zoomEnd event after 400ms the wheel stopped scrolling
clearTimeout(this.wheelTimeout);
this.wheelTimeout = setTimeout(function () {
that._execEvent('zoomEnd');
}, 400);
if ( 'deltaX' in e ) {
wheelDeltaY = -e.deltaY / Math.abs(e.deltaY);
} else if ('wheelDeltaX' in e) {
wheelDeltaY = e.wheelDeltaY / Math.abs(e.wheelDeltaY);
} else if('wheelDelta' in e) {
wheelDeltaY = e.wheelDelta / Math.abs(e.wheelDelta);
} else if ('detail' in e) {
wheelDeltaY = -e.detail / Math.abs(e.wheelDelta);
} else {
return;
}
deltaScale = this.scale + wheelDeltaY / 5;
this.zoom(deltaScale, e.pageX, e.pageY, 0);
},