Skip to content

Commit

Permalink
Updated zoom
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Spinelli committed Feb 10, 2011
1 parent b12a0e9 commit 97e0158
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 46 deletions.
1 change: 0 additions & 1 deletion examples/simple/index.html
Expand Up @@ -111,7 +111,6 @@
</head>
<body>
<div id="header"><a href="http://cubiq.org/iscroll">iScroll</a></div>

<div id="wrapper">
<div id="scroller">
<ul id="thelist">
Expand Down
114 changes: 114 additions & 0 deletions examples/zoom/index.html
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0, minimum-scale=1.0, maximum-scale=1.0">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<title>Scrolling DIV demo on iPhone / iPod Touch / Android / iPad</title>

<script type="application/javascript" src="../../src/iscroll.js?v4"></script>

<script type="text/javascript">

var myScroll;
function loaded() {
myScroll = new iScroll('wrapper', { zoom:true });
}

document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false);

document.addEventListener('DOMContentLoaded', loaded, false);

</script>

<style type="text/css" media="all">
body,ul,li,p {
padding:0;
margin:0;
border:0;
}

body {
font-size:12px;
-webkit-user-select:none;
-webkit-text-size-adjust:none;
font-family:helvetica;
}

#header {
position:absolute;
top:0; left:0;
width:100%;
height:45px;
line-height:45px;
background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #fe96c9), color-stop(0.05, #d51875), color-stop(1, #7b0a2e));
padding:0;
color:#eee;
font-size:20px;
text-align:center;
}

#header a {
color:#f3f3f3;
text-decoration:none;
font-weight:bold;
text-shadow:0 -1px 0 rgba(0,0,0,0.5);
}

#footer {
position:absolute;
bottom:0; left:0;
width:100%;
height:48px;
background-image:-webkit-gradient(linear, 0 0, 0 100%, color-stop(0, #999), color-stop(0.02, #666), color-stop(1, #222));;
padding:0;
border-top:1px solid #444;
}

#wrapper {
position:absolute; z-index:1;
top:45px; bottom:48px; left:0;
width:100%;
background:#aaa;
overflow:auto;
}

#scroller {
position:relative;
/* -webkit-touch-callout:none;*/
-webkit-tap-highlight-color:rgba(0,0,0,0);

width:640px;
padding:0;
background:#fff;
}

p {
display:block;
width:624px;
margin-bottom:1em;
padding:8px;
font-size:14px;
}

p img {
margin:4px 8px;
-webkit-transform:translate3d(0,0,0);
}
</style>
</head>
<body>
<div id="header"><a href="http://cubiq.org/iscroll">iScroll</a></div>
<div id="wrapper">
<div id="scroller">
<p><img src="iscroll.jpg" width="100" height="122" alt="iscroll" style="float:left">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
<p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?</p>
<p><img src="iscroll.jpg" width="100" height="122" alt="iscroll" style="float:right">At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.</p>
</div>
</div>

<div id="footer"></div>

</body>
</html>
Binary file added examples/zoom/iscroll.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
131 changes: 86 additions & 45 deletions src/iscroll.js
Expand Up @@ -6,7 +6,7 @@
* Released under MIT license
* http://cubiq.org/dropbox/mit-license.txt
*
* Version 4.0 dev.rel. - Last updated: 2011.02.02
* Version 4.0 dev.rel. - Last updated: 2011.02.08
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
Expand Down Expand Up @@ -87,8 +87,6 @@ function iScroll (el, options) {
that.bind('gesturestart');
that.scroller.style.webkitTransform = that.scroller.style.webkitTransform + ' scale(1)';
}

that.bind('click');
}

iScroll.prototype = {
Expand All @@ -97,14 +95,13 @@ iScroll.prototype = {
pagesX: [], pagesY: [],
offsetBottom: 0,
offsetTop: 0,
scale: 1,
scale: 1, lastScale: 1,
contentReady: true,

handleEvent: function (e) {
var that = this;

switch(e.type) {
case 'click': console.log(e.target); break;
case START_EV: that._start(e); break;
case MOVE_EV: that._move(e); break;
case END_EV:
Expand Down Expand Up @@ -460,20 +457,30 @@ iScroll.prototype = {

if (!that.moved) {
if (hasTouch) {
// Find the last touched element
target = point.target;
while (target.nodeType != 1) {
target = target.parentNode;
if (that.doubleTapTimer && that.options.zoom) {
// Double tapped
clearTimeout(that.doubleTapTimer);
that.doubleTapTimer = null;
that.zoom(that.pointX, that.pointY, that.scale == 1 ? 2 : 1);
} else {
that.doubleTapTimer = setTimeout(function () {
that.doubleTapTimer = null;

// Find the last touched element
target = point.target;
while (target.nodeType != 1) {
target = target.parentNode;
}

ev = document.createEvent('MouseEvents');
ev.initMouseEvent('click', true, true, e.view, 1,
point.screenX, point.screenY, point.clientX, point.clientY,
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
0, null);
ev._fake = true;
target.dispatchEvent(ev);
}, that.options.zoom ? 200 : 0);
}

ev = document.createEvent('MouseEvents');
ev.initMouseEvent('click', true, true, e.view, 1,
point.screenX, point.screenY, point.clientX, point.clientY,
e.ctrlKey, e.altKey, e.shiftKey, e.metaKey,
0, null);
ev._fake = true;
target.dispatchEvent(ev);
// target.focus();
}

that._resetPos();
Expand Down Expand Up @@ -525,7 +532,6 @@ iScroll.prototype = {
if (that.vScrollbar) that.vScrollbarIndicator.style.webkitTransitionTimingFunction = 'cubic-bezier(0.33,0.66,0.88,1)';
}

// that.bind('webkitTransitionEnd');
that.scrollTo(newPosX, newPosY, newDuration);
return;
}
Expand All @@ -534,7 +540,6 @@ iScroll.prototype = {
if (that.options.snap) {
snap = that._snap(that.x, that.y);
if (snap.x != that.x || snap.y != that.y) {
// that.bind('webkitTransitionEnd');
that.scrollTo(snap.x, snap.y, snap.time);
}
return;
Expand Down Expand Up @@ -608,7 +613,6 @@ iScroll.prototype = {
return;
}

// if (time) that.bind('webkitTransitionEnd');
that.scrollTo(resetX, resetY, time || 0);
},

Expand Down Expand Up @@ -652,40 +656,73 @@ iScroll.prototype = {
var that = this;

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

that.unbind('gesturestart');
// that.unbind('gesturestart');
that.bind('gesturechange');
that.bind('gestureend');
that.bind('gesturecancel');
},

_gestChange: function (e) {
var that = this,
scale = min(4, max(1, that.scale * e.scale)),
scale = that.scale * e.scale,
x, y;

if (scale > 1 && scale < 4) {
x = that.originX - that.originX * e.scale + that.x;
y = that.originY - that.originY * e.scale + that.y;
that.scroller.style.webkitTransform = trnOpen + x + 'px,' + y + 'px' + trnClose + ' scale(' + scale + ')';
}
if (scale < 1 || scale > 4) return;

x = that.originX - that.originX * e.scale + that.x;
y = that.originY - that.originY * e.scale + that.y;
that.scroller.style.webkitTransform = trnOpen + x + 'px,' + y + 'px' + trnClose + ' scale(' + scale + ')';
that.lastScale = e.scale;
},

_gestEnd: function (e) {
var that = this;

that.scale = min(4, max(1, that.scale * e.scale));
that.x = that.originX - round(that.originX * e.scale) + that.x;
that.y = that.originY - round(that.originY * e.scale) + that.y;
that.scale = that.scale * that.lastScale;
if (that.scale < 1.05) that.scale = 1;
that.x = that.originX - that.originX * that.lastScale + that.x;
that.y = that.originY - that.originY * that.lastScale + that.y;
that.scroller.style.webkitTransform = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + that.scale + ')';

that.refresh();
setTimeout(function () {
that.refresh();
}, 0);

that.bind('gesturestart')
// that.bind('gesturestart')
that.unbind('gesturechange');
that.unbind('gestureend');
that.unbind('gesturecancel');
},

zoom: function (x, y, scale) {
var that = this,
el = that.wrapper,
offsetLeft = offsetTop = 0,
relScale = scale / that.scale;

do {
offsetLeft += el.offsetLeft;
offsetTop += el.offsetTop;
} while (el = el.offsetParent);

x = x - offsetLeft - that.x;
y = y - offsetTop - that.y;
that.x = x - x * relScale + that.x;
that.y = y - y * relScale + that.y;

that.scale = scale;

that.refresh();
that.bind('webkitTransitionEnd');
that._transitionTime(200);

setTimeout(function () {
that.scroller.style.webkitTransform = trnOpen + that.x + 'px,' + that.y + 'px' + trnClose + ' scale(' + scale + ')';
}, 0);
},

_offset: function (el) {
var oLeft = oTop = 0;
do {
Expand All @@ -695,7 +732,7 @@ iScroll.prototype = {

return { x: oLeft, y: oTop };
},

scrollTo: function (x, y, time) {
var that = this;
time = time || 0;
Expand Down Expand Up @@ -727,7 +764,6 @@ iScroll.prototype = {
if (y < that.maxScrollY) y = that.maxScrollY;
}

// that.bind('webkitTransitionEnd');
that.scrollTo(x, y, time || max(abs(x)*2, abs(y)*2));
},

Expand All @@ -750,12 +786,14 @@ iScroll.prototype = {
},

destroy: function () {
/**
*
* REMOVE PULL DOWN!!!!!!!!!!!!!
*
*/

// Remove pull to refresh
if (that.pullDownToRefresh) {
that.pullDownEl.parentNode.removeChild(that.pullDownEl);
}
if (that.pullUpToRefresh) {
that.pullUpEl.parentNode.removeChild(that.pullUpEl);
}

// Remove the scrollbars
that.hScrollbar = false;
that.vScrollbar = false;
Expand All @@ -772,10 +810,13 @@ iScroll.prototype = {
that.unbind(MOVE_EV);
that.unbind(END_EV);
that.unbind(CANCEL_EV);
that.unbind('gesturestart')
that.unbind('gesturechange');
that.unbind('gestureend');
that.unbind('gesturecancel');

if (that.options.zoom) {
that.unbind('gesturestart')
that.unbind('gesturechange');
that.unbind('gestureend');
that.unbind('gesturecancel');
}
},

bind: function (type, el) {
Expand Down

0 comments on commit 97e0158

Please sign in to comment.