aw-front-helpers 2.5.0 → 2.5.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Readme.md +1 -0
- package/classes/Readme.md +15 -9
- package/classes/index.js +1 -1
- package/classes/sticky.js +156 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/stylesheets/_sticky.scss +15 -0
package/Readme.md
CHANGED
|
@@ -12,6 +12,7 @@ A library of functionalities for common tasks in Javascript / Sass.
|
|
|
12
12
|
- FormControl | A Javascript form validation class
|
|
13
13
|
- GParallax | A Parallax animation library
|
|
14
14
|
- Nearby | A librairy to animate element depending on the position of the mouse
|
|
15
|
+
- Sticky | A librairy to sticky an element on scroll
|
|
15
16
|
- **utils** [See the documentation](https://gitlab.com/awstudio/front-helpers/tree/master/utils/Readme.md)
|
|
16
17
|
- array
|
|
17
18
|
- dom
|
package/classes/Readme.md
CHANGED
|
@@ -4,22 +4,28 @@
|
|
|
4
4
|
## formControl
|
|
5
5
|
Based on HTML5 basic options for inputs (required, type...).
|
|
6
6
|
|
|
7
|
-
- `import FormControl from 'aw-front-helpers
|
|
8
|
-
- `
|
|
9
|
-
- You must define `this._parentElementClass`: Defines a wrapper with an input and an error message
|
|
7
|
+
- `import FormControl from 'aw-front-helpers';` to import
|
|
8
|
+
- `new FormControl(this._el, {options})`;
|
|
10
9
|
|
|
11
10
|
## globalParallax
|
|
12
|
-
- `import GParallax from 'aw-front-helpers
|
|
11
|
+
- `import GParallax from 'aw-front-helpers';` to import
|
|
13
12
|
- `GParallax.start()` to instantiate
|
|
14
13
|
- in your HTML element, you need define some atribute
|
|
15
14
|
- `data-prllx-item` with option `data-speed` and `data-delta-start` (speed between 0 and 2): HTML element, moves vertically
|
|
16
15
|
- `data-prllx-item-hz` with option `data-speed` and `data-delta-start` (speed between 0 and 2): HTML element, moves horizontally
|
|
17
16
|
- `data-prllx-bg` with options `data-delta-start` and `data-delta-end`: defines a background-image to an element
|
|
18
17
|
|
|
19
|
-
## modelsManager
|
|
20
|
-
- `import ModelsManager from 'aw-front-helpers/classes/modelsManager';` to import
|
|
21
|
-
- `class MyModule extends ModelsManager` to extend
|
|
22
|
-
|
|
23
18
|
## nearby
|
|
24
|
-
- `import
|
|
19
|
+
- `import Nearby from 'aw-front-helpers';` to import
|
|
25
20
|
- `new Nearby(this._el, {options})`;
|
|
21
|
+
|
|
22
|
+
## sticky
|
|
23
|
+
- `import Sticky from 'aw-front-helpers';` to import
|
|
24
|
+
- `new Sticky({options})`;
|
|
25
|
+
- need to define `[data-attributes]`
|
|
26
|
+
- `[data-sticker-wrapper]` sticky wrapper of the element
|
|
27
|
+
- `[data-sticker-element]` element you want stick in the wrapper
|
|
28
|
+
- Options:
|
|
29
|
+
- `el`: specific for the querysSelector
|
|
30
|
+
- `breakpointDisabled` defined a disabled breakpoint
|
|
31
|
+
- `topHeightAdjustement` like to define an sticky overlap header
|
package/classes/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
module.exports.GParallax = require('./globalParallax').default;
|
|
2
2
|
module.exports.Nearby = require('./nearby').default;
|
|
3
3
|
module.exports.FormControl = require('./formControl').default;
|
|
4
|
-
module.exports.
|
|
4
|
+
module.exports.Sticky = require('./sticky').default;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import debounce from "debounce";
|
|
2
|
+
import EventEmitter from 'events';
|
|
3
|
+
|
|
4
|
+
export default class Sticky extends EventEmitter {
|
|
5
|
+
//________________________________________________________ constructor
|
|
6
|
+
// -
|
|
7
|
+
constructor(options = {}) {
|
|
8
|
+
super();
|
|
9
|
+
this._options = {
|
|
10
|
+
el: window.document,
|
|
11
|
+
breakpointDisabled: null,
|
|
12
|
+
topHeightAdjustement: 0,
|
|
13
|
+
...options
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
this._isDisabled = false;
|
|
17
|
+
this._isSticky = false;
|
|
18
|
+
this._isEndSticky = false;
|
|
19
|
+
this._onResize = debounce(this._onResize, 200);
|
|
20
|
+
|
|
21
|
+
this._createBound();
|
|
22
|
+
this._grabDom();
|
|
23
|
+
this._addEvents();
|
|
24
|
+
this._onResize();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
//________________________________________________________ public
|
|
28
|
+
// -
|
|
29
|
+
get options() {
|
|
30
|
+
return this._options;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
set breakpointDisabled( breakpoint ) {
|
|
34
|
+
this._options.breakpointDisabled = breakpoint;
|
|
35
|
+
this.update();
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
set topHeightAdjustement( height ) {
|
|
39
|
+
this._options.topHeightAdjustement = height;
|
|
40
|
+
this.update();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
update() {
|
|
44
|
+
this._onResize();
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
destroy() {
|
|
48
|
+
window.removeEventListener("scroll", this._onScrollHandler);
|
|
49
|
+
window.removeEventListener("resize", this._onResize);
|
|
50
|
+
this._dom.el.classList.remove("is-sticky", "is-sticky-end");
|
|
51
|
+
this._dom.el.style.top = null;
|
|
52
|
+
this._dom.el.style.width = null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
//________________________________________________________ events
|
|
56
|
+
// -
|
|
57
|
+
_onScrollHandler() {
|
|
58
|
+
if (this._isDisabled) return;
|
|
59
|
+
this._vars.scrollY = window.pageYOffset;
|
|
60
|
+
requestAnimationFrame(this._onScroll)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_onScroll() {
|
|
64
|
+
if (this._isDisabled) return;
|
|
65
|
+
const positionSticky = this._cache.containerTop - this._vars.scrollY - this._options.topHeightAdjustement;
|
|
66
|
+
const stickyFilter = positionSticky < 0;
|
|
67
|
+
|
|
68
|
+
const stickyBottomWrapperPage = this._vars.scrollY + this._vars.height > this._cache.wrapperTop + this._cache.wrapperHeight;
|
|
69
|
+
const stickyBottomElement = this._vars.scrollY + this._options.topHeightAdjustement + this._cache.containerHeight > this._cache.wrapperTop + this._cache.wrapperHeight;
|
|
70
|
+
const stickyEndFilter = stickyBottomWrapperPage && stickyBottomElement;
|
|
71
|
+
if (this._cache.wrapperHeight < this._cache.containerHeight) {
|
|
72
|
+
this._dom.el.classList.remove("is-sticky", "is-sticky-end");
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (stickyFilter && !this._isSticky) {
|
|
77
|
+
this._isSticky = true;
|
|
78
|
+
this._dom.el.classList.add("is-sticky");
|
|
79
|
+
this._dom.el.style.top = `${this._options.topHeightAdjustement}px`;
|
|
80
|
+
this._dom.el.style.width = `${this._cache.containerWidth}px`;
|
|
81
|
+
} else if (!stickyFilter && this._isSticky) {
|
|
82
|
+
this._isSticky = false;
|
|
83
|
+
this._dom.el.style.top = null;
|
|
84
|
+
this._dom.el.style.width = null;
|
|
85
|
+
this._dom.el.classList.remove("is-sticky");
|
|
86
|
+
}
|
|
87
|
+
if (stickyEndFilter && !this._isEndSticky) {
|
|
88
|
+
this._isEndSticky = true;
|
|
89
|
+
this._dom.el.style.top = null;
|
|
90
|
+
this._dom.el.style.width = null;
|
|
91
|
+
this._dom.el.classList.add("is-sticky-end");
|
|
92
|
+
} else if (!stickyEndFilter && this._isEndSticky) {
|
|
93
|
+
this._isEndSticky = false;
|
|
94
|
+
this._dom.el.style.top = `${this._options.topHeightAdjustement}px`;
|
|
95
|
+
this._dom.el.style.width = `${this._cache.containerWidth}px`;
|
|
96
|
+
this._dom.el.classList.remove("is-sticky-end");
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
_onResize() {
|
|
101
|
+
if (this._options.breakpointDisabled && window.innerWidth < this._options.breakpointDisabled) {
|
|
102
|
+
if (!this._isDisabled) {
|
|
103
|
+
this._isDisabled = true;
|
|
104
|
+
this._dom.el.classList.remove("is-sticky", "is-sticky-end");
|
|
105
|
+
this._dom.el.style.top = null;
|
|
106
|
+
this._dom.el.style.width = null;
|
|
107
|
+
this.emit('disable');
|
|
108
|
+
}
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
if (this._isDisabled) {
|
|
112
|
+
this._isDisabled = false;
|
|
113
|
+
this.emit('enable');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
this._isSticky = false;
|
|
117
|
+
this._isEndSticky = false;
|
|
118
|
+
this._dom.el.classList.remove("is-sticky", "is-sticky-end");
|
|
119
|
+
this._dom.el.style.width = null;
|
|
120
|
+
setTimeout(() => {
|
|
121
|
+
const boudingWrapper = this._dom.wrapper.getBoundingClientRect();
|
|
122
|
+
const boudingContainer = this._dom.el.getBoundingClientRect();
|
|
123
|
+
this._vars = {
|
|
124
|
+
scrollY: window.pageYOffset || document.documentElement.scrollTop,
|
|
125
|
+
height: window.innerHeight || document.documentElement.clientHeight,
|
|
126
|
+
};
|
|
127
|
+
this._cache = {
|
|
128
|
+
windowHeight: window.innerHeight || document.documentElement.clientHeight || document.getElementsByTagName("body")[0],
|
|
129
|
+
containerTop: boudingContainer.top + this._vars.scrollY,
|
|
130
|
+
containerHeight: boudingContainer.height,
|
|
131
|
+
containerWidth: boudingContainer.width,
|
|
132
|
+
wrapperTop: boudingWrapper.top + this._vars.scrollY,
|
|
133
|
+
wrapperHeight: boudingWrapper.height,
|
|
134
|
+
};
|
|
135
|
+
this._onScrollHandler();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
//________________________________________________________ private
|
|
139
|
+
// -
|
|
140
|
+
_createBound() {
|
|
141
|
+
["_onScroll", "_onScrollHandler", "_onResize"].forEach((fn) => (this[fn] = this[fn].bind(this)));
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
_grabDom() {
|
|
145
|
+
this._dom = {};
|
|
146
|
+
this._dom.el = this._options.el.querySelector("[data-sticker-element]");
|
|
147
|
+
this._dom.wrapper = this._options.el.querySelector("[data-sticker-wrapper]");
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
_addEvents() {
|
|
151
|
+
setTimeout(() => {
|
|
152
|
+
window.addEventListener("scroll", this._onScrollHandler);
|
|
153
|
+
window.addEventListener("resize", this._onResize);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
}
|
package/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export const GParallax = require('./classes/globalParallax').default;
|
|
2
2
|
export const Nearby = require('./classes/nearby').default;
|
|
3
3
|
export const FormControl = require('./classes/formControl').default;
|
|
4
|
+
export const Sticky = require('./classes/sticky').default;
|
|
4
5
|
export const BootstraperMaps = require('./bootstraper/maps').default;
|
|
5
6
|
export const BootstraperYoutube = require('./bootstraper/youtube').default;
|
|
6
7
|
export const utils = require('./utils');
|
package/package.json
CHANGED