Skip to content

Commit 998ec9d

Browse files
committed
Add call event
1 parent 8fa218a commit 998ec9d

3 files changed

Lines changed: 57 additions & 15 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "modularscroll",
3-
"version": "1.0.5",
3+
"version": "1.0.6",
44
"description": "Dead simple elements in viewport detection.",
55
"repository": "modularorg/modularscroll",
66
"author": "Antoine Boulanger <hello@antoineboulanger.com> (https://antoineboulanger.com)",

readme.md

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,31 +49,51 @@ this.scroll = new modularScroll({
4949
```js
5050
import modularScroll from 'modularscroll';
5151

52-
this.scroll = new modularScroll({});
52+
this.scroll = new modularScroll();
5353

5454
this.scroll.update();
5555
```
5656

57+
#### With events
58+
```js
59+
import modularScroll from 'modularscroll';
60+
61+
this.scroll = new modularScroll();
62+
63+
this.scroll.on('call', (func) => {
64+
this.call(...func); // Using modularJS
65+
});
66+
```
67+
```html
68+
<div data-scroll data-scroll-call="function, module">Trigger</div>
69+
```
70+
5771
## Options
5872
| Option | Type | Default | Description |
5973
| ------ | ---- | ------- | ----------- |
60-
| `el` | `object` | `document` | Scroll container element |
61-
| `name` | `string` | `'scroll'` | Data attributes name |
62-
| `class` | `string` | `'is-inview'` | Elements in-view class |
63-
| `offset` | `number` | `0` | In-view trigger offset |
64-
| `repeat` | `boolean` | `false` | Repeat in-view detection |
74+
| `el` | `object` | `document` | Scroll container element. |
75+
| `name` | `string` | `'scroll'` | Data attributes name. |
76+
| `class` | `string` | `'is-inview'` | Elements in-view class. |
77+
| `offset` | `number` | `0` | In-view trigger offset. |
78+
| `repeat` | `boolean` | `false` | Repeat in-view detection. |
6579

6680
## Attributes
6781
| Attribute | Values | Description |
6882
| --------- | ------ | ----------- |
69-
| `data-scroll` | | Detect if in-view |
70-
| `data-scroll-class` | `string` | Element in-view class |
71-
| `data-scroll-offset` | `number` | Element in-view trigger offset |
72-
| `data-scroll-repeat` | `true`, `false` | Element in-view detection repeat |
83+
| `data-scroll` | | Detect if in-view. |
84+
| `data-scroll-class` | `string` | Element in-view class. |
85+
| `data-scroll-offset` | `number` | Element in-view trigger offset. |
86+
| `data-scroll-repeat` | `true`, `false` | Element in-view detection repeat. |
87+
| `data-scroll-call` | `string` | Element in-view trigger call event. |
7388

7489
## Methods
7590
| Method | Description |
7691
| --------- | ----------- |
77-
| `init()` | Reinit the scroll |
78-
| `update()` | Update elements position |
79-
| `destroy()` | Destroy the scroll events |
92+
| `init()` | Reinit the scroll. |
93+
| `update()` | Update elements position. |
94+
| `destroy()` | Destroy the scroll events. |
95+
96+
## Events
97+
| Event | Arguments | Description |
98+
| ----- | --------- | ----------- |
99+
| `call` | `func` | Trigger if in-view. Returns your `string` or `array` if contains `,`. |

src/main.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export default class {
1010

1111
Object.assign(this, this.defaults, options);
1212

13+
this.namespace = 'modular';
1314
this.windowHeight = window.innerHeight;
1415
this.els = [];
1516
this.scrollPosition = 0;
@@ -35,6 +36,7 @@ export default class {
3536
let bottom = top + el.offsetHeight;
3637
let offset = parseInt(el.dataset[this.name + '-offset']) || parseInt(this.offset);
3738
let repeat = el.dataset[this.name + '-repeat'];
39+
let call = el.dataset[this.name + '-call'];
3840

3941
if(repeat == 'false') {
4042
repeat = false;
@@ -51,7 +53,8 @@ export default class {
5153
bottom: bottom,
5254
offset: offset,
5355
repeat: repeat,
54-
inView: false
56+
inView: false,
57+
call: call
5558
}
5659
});
5760

@@ -115,6 +118,14 @@ export default class {
115118
}
116119

117120
el.el.classList.add(el.class);
121+
122+
if (el.call) {
123+
this.callValue = el.call.split(',').map(item => item.trim());
124+
if (this.callValue.length == 1) this.callValue = this.callValue[0];
125+
126+
const callEvent = new Event(this.namespace + 'call');
127+
window.dispatchEvent(callEvent);
128+
}
118129
}
119130

120131
setOutOfView(el, i) {
@@ -134,6 +145,17 @@ export default class {
134145
this.frame = false;
135146
}
136147

148+
on(event, func) {
149+
window.addEventListener(this.namespace + event, () => {
150+
switch (event) {
151+
case 'call':
152+
return func(this.callValue);
153+
default:
154+
return func();
155+
}
156+
}, false);
157+
}
158+
137159
destroy() {
138160
window.removeEventListener('scroll', this.checkScroll, false);
139161
window.removeEventListener('resize', this.checkResize, false);

0 commit comments

Comments
 (0)