Skip to content

Commit fb31797

Browse files
committed
simplify code, add opacity
1 parent 2d84c97 commit fb31797

File tree

2 files changed

+38
-34
lines changed

2 files changed

+38
-34
lines changed

index.js

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
this.element.style.transform = 'translateY(' + pixels + 'px)';
1919
};
2020

21-
function getNumber (start, end, easing, ratio) {
21+
function getNumber (field, options, ratio) {
22+
var easing = options.easing[field];
23+
var start = options.start[field];
24+
var end = options.end[field];
25+
2226
if (easing) {
2327
ratio = easing(ratio);
2428
}
@@ -46,54 +50,33 @@
4650
OptionsParallax.prototype.handleScroll = function (ratio) {
4751
var transformString = 'translate3D(';
4852
if (this.options.end.x) {
49-
transformString += getNumber(
50-
this.options.start.x,
51-
this.options.end.x,
52-
this.options.easing.x,
53-
ratio
54-
) + 'px,';
53+
transformString += getNumber('x', this.options, ratio) + 'px,';
5554
} else {
5655
transformString += '0px,';
5756
}
5857
if (this.options.end.y) {
59-
transformString += getNumber(
60-
this.options.start.y,
61-
this.options.end.y,
62-
this.options.easing.y,
63-
ratio
64-
) + 'px,';
58+
transformString += getNumber('y', this.options, ratio) + 'px,';
6559
} else {
6660
transformString += '0px,';
6761
}
6862
if (this.options.end.z) {
69-
transformString += getNumber(
70-
this.options.start.z,
71-
this.options.end.z,
72-
this.options.easing.z,
73-
ratio
74-
) + 'px)';
63+
transformString += getNumber('z', this.options, ratio) + 'px)';
7564
} else {
7665
transformString += '0px)';
7766
}
7867

7968
if (this.options.end.rotate) {
80-
transformString += ' rotate(' + getNumber(
81-
this.options.start.rotate,
82-
this.options.end.rotate,
83-
this.options.easing.rotate,
84-
ratio
85-
) + 'deg)';
69+
transformString += ' rotate(' + getNumber('rotate', this.options, ratio) + 'deg)';
8670
}
8771

8872
if (this.options.end.scale) {
89-
transformString += ' scale(' + getNumber(
90-
this.options.start.scale,
91-
this.options.end.scale,
92-
this.options.easing.rotate,
93-
ratio
94-
) + ')';
73+
transformString += ' scale(' + getNumber('scale', this.options, ratio) + ')';
9574
}
9675
this.element.style.transform = transformString;
76+
77+
if (this.options.end.opacity) {
78+
this.element.style.opacity = getNumber('opacity', this.options, ratio);
79+
}
9780
};
9881

9982
function Root (element, offsets) {

tests/index.html

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,22 @@
1212
overflow: hidden;
1313
}
1414

15+
#outside {
16+
position: fixed;
17+
top: 10px;
18+
left: 10px;
19+
width: 200px;
20+
text-align: center;
21+
background-color: black;
22+
color: white;
23+
opacity: 0;
24+
}
25+
1526
#speedy {
1627
position: absolute;
1728
top: 10px;
1829
left: 100px;
1930
width: 200px;
20-
height: 280px;
2131
text-align: center;
2232
background-color: white;
2333
}
@@ -26,7 +36,6 @@
2636
top: 1000px;
2737
left: 250px;
2838
width: 200px;
29-
height: 280px;
3039
text-align: center;
3140
background-color: white;
3241
}
@@ -35,7 +44,6 @@
3544
top: 10px;
3645
left: 400px;
3746
width: 200px;
38-
height: 280px;
3947
text-align: center;
4048
background-color: white;
4149
}
@@ -79,6 +87,10 @@ <h1>ScrollMonitor Parallax Test Page</h1>
7987
</pre>
8088
</div>
8189
</div>
90+
<div id="outside">
91+
I live outside the scroll container,
92+
but that doesn't matter.
93+
</div>
8294
<script src="/node_modules/scrollmonitor/scrollMonitor.js"></script>
8395
<script src="/index.js"></script>
8496
<script>
@@ -103,6 +115,15 @@ <h1>ScrollMonitor Parallax Test Page</h1>
103115
}
104116
});
105117

118+
root.add(document.getElementById('outside'), {
119+
end: {
120+
opacity: 1
121+
},
122+
easing: {
123+
opacity: function (ratio) { return Math.sin(4.5 * ratio * Math.PI); }
124+
}
125+
});
126+
106127
</script>
107128
</body>
108129
</html>

0 commit comments

Comments
 (0)