-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparallax.es5.html
More file actions
130 lines (124 loc) · 4.52 KB
/
parallax.es5.html
File metadata and controls
130 lines (124 loc) · 4.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<title>parallax</title>
<style type="text/css">
html, body { height:100%; margin:0; }
body { background:#000; font:14px/1 helvetica,arial,freesans,sans-serif; }
.parallaxer { height:100%; width:100%; overflow:hidden; position:relative; left:0; top:0; user-select:none; }
.parallaxer img { max-height:100%; max-width:100%; margin:auto; position:absolute; left:0; top:0; bottom:0; right:0; }
</style>
</head>
<body>
<div class="parallaxer js-parallaxer">
<img alt="" src="img/parallax/layer2.1.jpg">
<img alt="" src="img/parallax/layer2.2.png">
<img alt="" src="img/parallax/layer2.3.png">
<img alt="" src="img/parallax/layer2.2.png">
<img alt="" src="img/parallax/layer2.3.png">
</div>
<script type="text/javascript">
'use strict';
function getComputed(el, style) {
if (window.getComputedStyle) {
var y = document.defaultView.getComputedStyle(el, null).getPropertyValue(style);
} else if (el.currentStyle) {
if (style == 'width') {
y = el.getBoundingClientRect().right - el.getBoundingClientRect().left;
} else if (style == 'height') {
y = el.getBoundingClientRect().bottom - el.getBoundingClientRect().top;
} else {
var y = el.currentStyle[style];
}
}return parseFloat(y);
}
function getEventXY(e) {
var xy = [];
if (e.touches && e.touches.length) {
if (e.touches.length > 1) {
xy[0] = (e.touches[0].pageX + e.touches[0].pageX) / 2;
xy[1] = (e.touches[0].pageY + e.touches[0].pageY) / 2;
} else {
xy[0] = e.touches[0].pageX;
xy[1] = e.touches[0].pageY;
}
} else {
xy[0] = e.pageX;
xy[1] = e.pageY;
}
return xy;
}
var parallaxers = document.querySelectorAll(".js-parallaxer");
if (parallaxers.length) {
var painter = {
id: 1,
size: function size(el) {
el.wh = [parseInt(getComputed(el, 'width')), Math.ceil(getComputed(el, 'height'))];
el.xy = [parseInt(getComputed(el, 'left')), Math.ceil(getComputed(el, 'top'))];
},
init: function init(el) {
el.imgs = el.getElementsByTagName('img');
el.scale = 3.4;
el.xlate = [0, 0];
el.pinchDelta = 0;
if (!el.paint) {
el.id = el.id || 'parallaxer' + painter.id++;
el.css = el.appendChild(document.createElement('style'));
el.paint = function () {
var css = '';
for (var i = 0; i < el.imgs.length; i++) {
css += '#' + el.id + ' :nth-child(' + (i + 1) + '){ -webkit-transform: -webkit-scale(' + (.5 + el.scale * i * i / 100) + ') -webkit-translate(' + -el.xlate[0] * i * i * 5 / el.scale + '% , ' + -el.xlate[1] * i * i * 5 / el.scale + '%) }';
css += '#' + el.id + ' :nth-child(' + (i + 1) + '){ transform: scale(' + (.5 + el.scale * i * i / 100) + ') translate(' + -el.xlate[0] * i * i * 5 / el.scale + '% , ' + -el.xlate[1] * i * i * 5 / el.scale + '%) }';
}
window.requestAnimationFrame(function () {
el.css.innerHTML = css;
});
};
el.move = function (xy) {
el.xlate = xy;
};
el.zoom = function (z) {
el.scale += z;
if (el.scale < 1) {
el.scale = 1;
}
};
el.paint();
el.onmousemove = painter.touch.bind(null, el);
el.onwheel = painter.touch.bind(null, el);
el.ontouchmove = painter.touch.bind(null, el);
}
},
touch: function touch(el, e) {
var xyE = getEventXY(e);
if (!el.w) {
painter.size(el);
}
if (e.type === 'wheel') {
el.zoom(e.deltaY > 0 ? -1 : 1);
} else {
if (e.touches && e.touches.length === 2) {
var pinchDeltaPrevious = el.pinchDelta;
el.pinchDelta = Math.sqrt(Math.pow(e.touches[0].pageX - e.touches[1].pageX, 2) + Math.pow(e.touches[0].pageY - e.touches[1].pageY, 2));
if (pinchDeltaPrevious) {
el.zoom((el.pinchDelta - pinchDeltaPrevious) / 20);
}
} else {
var mvGain = (!e.touches) ? 1 : 5;
el.move([(xyE[0] - (el.xy[0] + el.wh[0] / 2)) * mvGain / el.wh[0], (xyE[1] - (el.xy[1] + el.wh[1] / 2)) * mvGain / el.wh[1]]);
}
}
el.paint();
}
};
for (var i = 0; i < parallaxers.length; i++) {
painter.init(parallaxers[i]);
}
}
</script>
</body>
</html>