Skip to content

Commit a09b6c3

Browse files
committed
initial commit
0 parents commit a09b6c3

File tree

5 files changed

+231
-0
lines changed

5 files changed

+231
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Scrollmonitor Parallax
2+
3+
## Installation
4+
5+
```
6+
npm install -S scrollmonitor-parallax
7+
```
8+
9+
## Basic Usage
10+
11+
```javascript
12+
var parallax = require('scrollmonitor-parallax');
13+
var eases = require('eases'); // https://github.com/mattdesl/eases
14+
15+
16+
// Create a root element. Parallax will start when this
17+
// element enters the viewport and stop when it exits.
18+
var parallaxRoot = parallax.create(domElement);
19+
20+
// to make an element scroll at a speed relative to the
21+
// scroll parent, just add a value for speed.
22+
var parallaxChild2 = parallaxRoot.add(domElement, 0.5);
23+
24+
// for more complex animations you can start and end positions.
25+
// If it's left blank, the start position is taken from the element's CSS.
26+
var parallaxChild = parallaxRoot.add(
27+
domElement, {
28+
start: {
29+
opacity: 0
30+
}
31+
end: {
32+
x: 100,
33+
y: 50,
34+
z: 100,
35+
opacity: 0.7
36+
},
37+
easing: {
38+
x: eases.linear,
39+
y: eases.linear,
40+
z: eases.circIn,
41+
opacity: eases.bounceIn
42+
}
43+
}
44+
);
45+
46+
```

index.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
(function( factory ) {
2+
if (typeof define !== 'undefined' && define.amd) {
3+
define(['scrollmonitor'], factory);
4+
} else if (typeof module !== 'undefined' && module.exports) {
5+
module.exports = factory(require('scrollmonitor'));
6+
} else {
7+
window.parallax = factory(window.scrollMonitor);
8+
}
9+
})(function (scrollMonitor) {
10+
11+
function SpeedParallax (element, speed) {
12+
this.speed = speed;
13+
this.element = element;
14+
}
15+
16+
SpeedParallax.prototype.handleScroll = function (ratio, distance) {
17+
var pixels = distance * this.speed;
18+
this.element.style.transform = 'translateY(' + pixels + 'px)';
19+
};
20+
21+
function OptionsParallax (element, options) {
22+
this.options = options;
23+
this.element = element;
24+
}
25+
26+
OptionsParallax.prototype.handleScroll = function (ratio) {
27+
var transformString = 'translate3D(';
28+
if (this.options.end.x) {
29+
transformString += (this.options.end.x * ratio) + 'px,';
30+
} else {
31+
transformString += '0px,';
32+
}
33+
if (this.options.end.y) {
34+
transformString += (this.options.end.y * ratio) + 'px,';
35+
} else {
36+
transformString += '0px,';
37+
}
38+
if (this.options.end.z) {
39+
transformString += (this.options.end.z * ratio) + 'px)';
40+
} else {
41+
transformString += '0px)';
42+
}
43+
44+
if (this.options.end.rotate) {
45+
transformString += ' rotate(' + (this.options.end.rotate * ratio) + 'deg)';
46+
}
47+
48+
this.element.style.transform = transformString;
49+
};
50+
51+
function Root (element, offsets) {
52+
this.watcher = scrollMonitor.create(element, offsets);
53+
this.items = [];
54+
55+
this.pxThru = 0;
56+
this.ratio = 0;
57+
58+
var self = this;
59+
60+
function handleScroll () {
61+
var start = Math.max(0, self.watcher.top - scrollMonitor.viewportHeight);
62+
var end = Math.min(self.watcher.bottom, scrollMonitor.documentHeight - scrollMonitor.viewportHeight);
63+
64+
self.pxThru = Math.max(0, scrollMonitor.viewportTop - start);
65+
self.ratio = self.pxThru / (end - start);
66+
67+
for (var i=0; i < self.items.length; i++) {
68+
self.items[i].handleScroll.call(self.items[i], self.ratio, self.pxThru);
69+
}
70+
}
71+
72+
this.watcher.enterViewport(function () {
73+
window.addEventListener('scroll', handleScroll);
74+
});
75+
this.watcher.exitViewport(function () {
76+
window.removeEventListener('scroll', handleScroll);
77+
});
78+
}
79+
80+
Root.prototype.add = function add (element, optionsOrSpeed) {
81+
var newItem;
82+
if (typeof optionsOrSpeed === 'number') {
83+
newItem = new SpeedParallax(element, optionsOrSpeed);
84+
} else {
85+
newItem = new OptionsParallax(element, optionsOrSpeed);
86+
}
87+
88+
this.items.push(newItem);
89+
};
90+
91+
return {
92+
create: function (item, offsets) {
93+
return new Root(item, offsets);
94+
}
95+
};
96+
});

package.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{
2+
"name": "scrollmonitor-parallax",
3+
"version": "0.0.0",
4+
"description": "A simple parallax API",
5+
"main": "index.js",
6+
"directories": {
7+
"test": "tests"
8+
},
9+
"dependencies": {
10+
"scrollmonitor": "^1.0.13"
11+
},
12+
"devDependencies": {},
13+
"scripts": {
14+
"test": "echo \"Error: no test specified\" && exit 1"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/stutrek/scrollmonitor-parallax.git"
19+
},
20+
"keywords": [
21+
"scrolling",
22+
"parallax"
23+
],
24+
"author": "Stu Kabakoff <sakabako@gmail.com>",
25+
"license": "MIT",
26+
"bugs": {
27+
"url": "https://github.com/stutrek/scrollmonitor-parallax/issues"
28+
},
29+
"homepage": "https://github.com/stutrek/scrollmonitor-parallax#readme"
30+
}

tests/index.html

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<!doctype html>
2+
<html>
3+
<head>
4+
<title>ScrollMonitor Parallax</title>
5+
<style type="text/css">
6+
#container {
7+
background-color: silver;
8+
height: 3000px;
9+
width: 700px;
10+
margin: 0 auto;
11+
position: relative;
12+
overflow: hidden;
13+
}
14+
15+
#speedy {
16+
position: absolute;
17+
top: 10px;
18+
left: 100px;
19+
width: 200px;
20+
height: 100px;
21+
text-align: center;
22+
background-color: white;
23+
}
24+
#custom {
25+
position: absolute;
26+
top: 10px;
27+
left: 400px;
28+
width: 200px;
29+
height: 100px;
30+
text-align: center;
31+
background-color: white;
32+
}
33+
</style>
34+
</head>
35+
<body>
36+
<h1>ScrollMonitor Parallax Test Page</h1>
37+
<div id="container">
38+
<div id="speedy"><i>SPEEDY!!</i></div>
39+
<div id="custom"><i>Custom</i></div>
40+
</div>
41+
<script src="/node_modules/scrollmonitor/scrollMonitor.js"></script>
42+
<script src="/index.js"></script>
43+
<script>
44+
/*eslint-disable */
45+
var root = parallax.create(document.getElementById('container'));
46+
47+
root.add(document.getElementById('speedy'), 2);
48+
49+
root.add(document.getElementById('custom'), {
50+
end: {
51+
y: 2880,
52+
rotate: 720
53+
}
54+
});
55+
56+
</script>
57+
</body>
58+
</html>

0 commit comments

Comments
 (0)