Skip to content

Commit 406061b

Browse files
author
Mikel Tuesta
committed
Initial commit
0 parents  commit 406061b

15 files changed

+708
-0
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
; This file is part of the FoesScrollProxy library.
2+
;
3+
; (c) Mikel Tuesta <[email protected]>
4+
;
5+
; For the full copyright and license information, please view the LICENSE
6+
; file that was distributed with this source code.
7+
8+
root = true
9+
10+
[*]
11+
end_of_line = LF
12+
13+
[*.{js, scss, css}]
14+
indent_style = space
15+
indent_size = 2
16+
17+
[{bower.json, package.json}]
18+
indent_style = space
19+
indent_size = 2

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# This file is part of the FoesScrollProxy library.
2+
#
3+
# (c) Mikel Tuesta <[email protected]>
4+
#
5+
# For the full copyright and license information, please view the LICENSE
6+
# file that was distributed with this source code.
7+
8+
/bower_components
9+
/coverage
10+
/dist
11+
/node_modules
12+
/npm-debug.log

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2016 FriendsOfECMAScript
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
'Software'), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#FoesScrollProxy
2+
>
3+
4+
[![npm version](https://img.shields.io/npm/v/foes-scrollproxy.svg?style=flat-square)](https://www.npmjs.com/package/foes-scrollproxy)
5+
[![Build Status](http://img.shields.io/travis/FriendsOfECMAScript/ScrollProxy/master.svg?style=flat-square)](https://travis-ci.org/FriendsOfECMAScript/ScrollProxy)
6+
[![NPM Status](http://img.shields.io/npm/dm/foes-scrollproxy.svg?style=flat-square)](https://www.npmjs.org/package/foes-scrollproxy)
7+
[![devDependency Status](https://img.shields.io/david/FriendsOfECMAScript/ScrollProxy.svg?style=flat-square)](https://david-dm.org/FriendsOfECMAScript/ScrollProxy#info=dependencies)
8+
9+
##Installation
10+
The recommended and the most suitable way to install is through *NPM*.
11+
```shell
12+
$ npm install --save foes-scrollproxy
13+
```
14+
15+
Also, you can install through *Bower*.
16+
```shell
17+
$ bower install --save foes-scrollproxy
18+
```
19+
20+
21+
After installation process, you have to include the js files in your html.
22+
```html
23+
<script src="/your/path/foes-scrollproxy/dist/foes-scrollproxy.umd.min.js"></script>
24+
```
25+
26+
Also, it supports ES2015 modules so, you can easily import the library in your js instead loading the UMD version in the DOM.
27+
```js
28+
import 'foes-scrollproxy';
29+
```
30+
31+
The following code is a fully initialization example code:
32+
```js
33+
34+
```
35+
36+
37+
##Credits
38+
This library is created by:
39+
>
40+
**@mktoast** - [[email protected]](mailto:[email protected])
41+
42+
##Licensing Options
43+
[![License](https://img.shields.io/badge/License-MIT-yellowgreen.svg?style=flat-square)](https://github.com/FriendsOfECMAScript/ScrollProxy/blob/master/LICENSE)

bower.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"name": "foes-scrollproxy",
3+
"description": "",
4+
"main": "dist/foes-scrollproxy.js",
5+
"authors": [
6+
{
7+
"name": "Mikel Tuesta",
8+
"email": "[email protected]",
9+
"homepage": "http://mikeltuesta.com"
10+
}
11+
],
12+
"license": "MIT",
13+
"keywords": [
14+
"foes",
15+
"scroll",
16+
"scroll-proxy"
17+
],
18+
"homepage": "https://github.com/FriendsOfECMAScript/ScrollProxy"
19+
}

package.json

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{
2+
"name": "foes-scrollproxy",
3+
"version": "0.1.0",
4+
"license": "MIT",
5+
"description": "",
6+
"keywords": [
7+
"foes",
8+
"scroll",
9+
"scroll-proxy"
10+
],
11+
"authors": [
12+
{
13+
"name": "Mikel Tuesta Sanchez",
14+
"email": "[email protected]",
15+
"homepage": "http://mikeltuesta.com"
16+
}
17+
],
18+
"main": "dist/foes-scrollproxy.js",
19+
"repository": {
20+
"type": "git",
21+
"url": "https://github.com/FriendsOfECMAScript/ScrollProxy"
22+
},
23+
"bugs": {
24+
"url": "https://github.com/FriendsOfECMAScript/ScrollProxy/issues"
25+
},
26+
"scripts": {
27+
"test": "jest",
28+
"prebuild": "rimraf dist",
29+
"build": "npm-run-all --parallel build:*",
30+
"build:main": "webpack --output-filename foes-scrollproxy.js",
31+
"build:umd": "webpack --output-filename foes-scrollproxy.umd.js --env.libraryTarget umd",
32+
"build:umd.min": "webpack --output-filename foes-scrollproxy.umd.min.js --env.libraryTarget umd -p"
33+
},
34+
"files": [
35+
"dist",
36+
"README.md"
37+
],
38+
"babel": {
39+
"presets": [
40+
"es2015",
41+
"stage-2"
42+
],
43+
"plugins": [
44+
"babel-plugin-transform-array-from"
45+
]
46+
},
47+
"devDependencies": {
48+
"array-from": "^2.1.1",
49+
"babel-cli": "^6.14.0",
50+
"babel-loader": "^6.2.5",
51+
"babel-plugin-transform-array-from": "^1.0.0",
52+
"babel-preset-es2015": "^6.14.0",
53+
"babel-preset-stage-2": "^6.13.0",
54+
"jest": "^15.1.1",
55+
"jsdom": "^9.3.0",
56+
"npm-run-all": "^3.1.0",
57+
"rimraf": "^2.5.4",
58+
"webpack": "^2.1.0-beta.21"
59+
},
60+
"peerDependencies": {
61+
"gsap": "^1.19.0",
62+
"lodash.throttle": "^4.1.0",
63+
"jquery": "^2.2.0"
64+
}
65+
}

src/Core/ScrollProxy.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* @author Mikel Tuesta <[email protected]>
3+
*/
4+
'use strict';
5+
6+
(function (window, DomHelpers, ScrollProxyListener, _) {
7+
8+
/**
9+
* ParallaxController - Singleton
10+
*
11+
* @constructor
12+
*/
13+
var ScrollProxy = (function () {
14+
var instance;
15+
16+
function ScrollProxy() {
17+
var latestKnownScrollY = window.pageYOffset,
18+
animating = false,
19+
windowDimensions = {
20+
height: -1,
21+
width: -1
22+
},
23+
listeners = [];
24+
25+
var notifyListeners = function () {
26+
var args = Array.prototype.slice.call(arguments),
27+
methodName = args.splice(0, 1);
28+
for (var i = 0; i < listeners.length; i++) {
29+
listeners[i][methodName].apply(listeners[i], args);
30+
}
31+
};
32+
var requestFrame = function () {
33+
if (!animating) {
34+
window.requestAnimationFrame(doFrame);
35+
}
36+
animating = true;
37+
};
38+
var doFrame = function () {
39+
notifyListeners('doFrame');
40+
animating = false;
41+
};
42+
var onScroll = function () {
43+
latestKnownScrollY = window.pageYOffset;
44+
notifyListeners('onScroll', latestKnownScrollY);
45+
requestFrame();
46+
};
47+
var onResize = function () {
48+
windowDimensions = DomHelpers.getWindowDimensions();
49+
notifyListeners('onResize', windowDimensions);
50+
};
51+
var bindListeners = function () {
52+
window.addEventListener('scroll', onScroll);
53+
window.addEventListener('resize', _.debounce(onResize, 200));
54+
};
55+
var addListener = function (listener) {
56+
if (!(listener instanceof ScrollProxyListener)) {
57+
throw 'Provided listener must be an instance of ScrollProxyListener.';
58+
}
59+
listeners.push(listener);
60+
};
61+
var removeListener = function (listener) {
62+
var listenerIndex = listeners.indexOf(listener);
63+
if (listenerIndex !== -1) {
64+
listeners.splice(listenerIndex, 1);
65+
}
66+
};
67+
var init = function () {
68+
windowDimensions = DomHelpers.getWindowDimensions();
69+
bindListeners();
70+
};
71+
init();
72+
73+
return {
74+
addListener: addListener,
75+
removeListener: removeListener
76+
}
77+
}
78+
79+
return {
80+
getInstance: function () {
81+
if (!instance) {
82+
instance = new ScrollProxy();
83+
}
84+
return instance;
85+
}
86+
};
87+
})();
88+
89+
// Expose ScrollProxy
90+
window.ScrollProxy = ScrollProxy;
91+
92+
})(window, window.DomHelpers, window.ScrollProxyListener, _);

src/Core/ScrollProxyListener.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* @author Mikel Tuesta <[email protected]>
3+
*/
4+
'use strict';
5+
6+
/**
7+
* ScrollProxyListener Base Class
8+
*
9+
* usage
10+
*
11+
* function SampleParallaxListener () {
12+
* ScrollProxyListener.call(this);
13+
* // Implement base class methods (doFrame, onScroll, onResize)
14+
* }
15+
* SampleParallaxListener.prototype = Object.create( ScrollProxyListener.prototype );
16+
*
17+
* @constructor
18+
*/
19+
(function(window, DomHelpers, undefined) {
20+
21+
function ScrollProxyListener() {
22+
this.windowDimensions = DomHelpers.getWindowDimensions();
23+
this.latestKnownScrollY = undefined;
24+
this.doFrame = function () {
25+
26+
};
27+
this.onScroll = function (latestKnownScrollY) {
28+
this.latestKnownScrollY = latestKnownScrollY;
29+
};
30+
this.onResize = function (windowDimensions) {
31+
this.windowDimensions = windowDimensions;
32+
};
33+
}
34+
35+
// Expose ScrollProxyListener
36+
window.ScrollProxyListener = ScrollProxyListener;
37+
38+
})(window, window.DomHelpers);

src/Helpers/DomHelpers.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* @author Mikel Tuesta <[email protected]>
3+
*/
4+
'use strict';
5+
6+
(function () {
7+
8+
var DomHelpers = Object.create({});
9+
10+
DomHelpers.getDocumentHeight = function () {
11+
var body = document.body,
12+
html = document.documentElement;
13+
14+
return Math.max(body.scrollHeight, body.offsetHeight, html.clientHeight, html.scrollHeight, html.offsetHeight);
15+
};
16+
17+
DomHelpers.getWindowDimensions = function () {
18+
var w = window,
19+
d = document,
20+
e = d.documentElement,
21+
g = d.getElementsByTagName('body')[0];
22+
23+
return {
24+
height: w.innerHeight || e.clientHeight || g.clientHeight,
25+
width: w.innerWidth || e.clientWidth || g.clientWidth
26+
}
27+
};
28+
29+
DomHelpers.getViewportData = function (element, windowHeight) {
30+
var rect = element.getBoundingClientRect();
31+
return {
32+
isInViewport: ((rect.top >= 0 && rect.top <= windowHeight) || (rect.bottom >= 0 && rect.bottom <= windowHeight)),
33+
rect: rect
34+
};
35+
};
36+
37+
// Expose DomHelpers
38+
window.DomHelpers = DomHelpers;
39+
40+
})();

0 commit comments

Comments
 (0)