Skip to content

Commit b97594e

Browse files
committed
addinng autoreset, options and data-kui-event attribute. v0.3.0 🚀
1 parent 3f8559d commit b97594e

File tree

4 files changed

+141
-19
lines changed

4 files changed

+141
-19
lines changed

build/scrollanim.js

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -373,32 +373,51 @@
373373
* options
374374
*/
375375
var _options = {
376-
//trigger the events on module init?
376+
// trigger the events on module init?
377377
triggerOnInit: true,
378-
attribute: 'data-kui-anim'
378+
// prefix for all `data-...` attributes
379+
attributePrefix: 'data-kui-',
380+
animAttribute: 'anim',
381+
// when to trigger the animation?
382+
eventAttribute: 'event',
383+
// default event to trigger
384+
defaultEvent: 'in',
385+
// reset the animation event after element is out of the viewport?
386+
// enabling this option triggers the event each time it appears in the viewport
387+
autoReset: true
379388
};
380389

381390
/**
382391
* To store all available elements with their options
383392
*/
384393
var _elements = [];
385394

395+
/**
396+
* Get the attribute name
397+
*
398+
*/
399+
function __(name) {
400+
return _options.attributePrefix + name;
401+
};
402+
386403
/**
387404
* Find elements
388405
*/
389406
function _populate () {
390407
//clear old elements first
391408
_elements = [];
392409

393-
var elements = document.querySelectorAll('*[' + _options.attribute + ']');
410+
var elements = document.querySelectorAll('*[' + __(_options.animAttribute) + ']');
394411

395412
for (var i = 0;i < elements.length;i++) {
413+
var param = {};
396414
var element = elements[i];
397-
var event = element.getAttribute(_options.attribute);
415+
var anim = element.getAttribute(__(_options.animAttribute));
416+
var event = element.getAttribute(__(_options.eventAttribute)) || 'in';
398417

399-
_add(element, {
400-
'in': event
401-
});
418+
param[event] = anim;
419+
420+
_add(element, param);
402421
}
403422
};
404423

@@ -426,6 +445,8 @@
426445
};
427446
}
428447

448+
kissuiPosition.add(element, 'out');
449+
429450
// add visibility: hidden to the element
430451
element.style.opacity = '0';
431452

@@ -500,6 +521,37 @@
500521
}
501522
};
502523

524+
/**
525+
* Clear animation, reset `opacity` and `active` flag on an element
526+
*
527+
*/
528+
function _resetElement (element) {
529+
var elx = _find(element)
530+
531+
for (var e in elx.event) {
532+
elx.event[e].active = false;
533+
}
534+
535+
element.style.opacity = 0;
536+
};
537+
538+
/**
539+
* Set option
540+
*
541+
*/
542+
function _setOption (name, value) {
543+
_options[name] = value;
544+
};
545+
546+
/**
547+
* Set an object of options
548+
*/
549+
function _setOptions (options) {
550+
for (var o in options) {
551+
_setOption(o, options[o]);
552+
}
553+
};
554+
503555
/**
504556
* Start the module
505557
*/
@@ -510,6 +562,13 @@
510562
_attach(_find(element), event);
511563
});
512564

565+
// to manage `autoReset`
566+
kissuiPosition.on('out', function (element) {
567+
if (_options.autoReset) {
568+
_resetElement(element);
569+
}
570+
});
571+
513572
kissuiPosition.init();
514573
};
515574

@@ -519,6 +578,8 @@
519578
_options: _options,
520579
_elements: _elements,
521580
init: _init,
522-
add: _add
581+
add: _add,
582+
setOption: _setOption,
583+
setOptions: _setOptions
523584
};
524585
}));

build/scrollanim.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "kissui.scrollanim",
3-
"version": "0.2.0",
3+
"version": "0.3.0",
44
"description": "CSS3 scroll animation library",
55
"main": "index.js",
66
"scripts": {

scrollanim.js

Lines changed: 69 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,51 @@
1919
* options
2020
*/
2121
var _options = {
22-
//trigger the events on module init?
22+
// trigger the events on module init?
2323
triggerOnInit: true,
24-
attribute: 'data-kui-anim'
24+
// prefix for all `data-...` attributes
25+
attributePrefix: 'data-kui-',
26+
animAttribute: 'anim',
27+
// when to trigger the animation?
28+
eventAttribute: 'event',
29+
// default event to trigger
30+
defaultEvent: 'in',
31+
// reset the animation event after element is out of the viewport?
32+
// enabling this option triggers the event each time it appears in the viewport
33+
autoReset: true
2534
};
2635

2736
/**
2837
* To store all available elements with their options
2938
*/
3039
var _elements = [];
3140

41+
/**
42+
* Get the attribute name
43+
*
44+
*/
45+
function __(name) {
46+
return _options.attributePrefix + name;
47+
};
48+
3249
/**
3350
* Find elements
3451
*/
3552
function _populate () {
3653
//clear old elements first
3754
_elements = [];
3855

39-
var elements = document.querySelectorAll('*[' + _options.attribute + ']');
56+
var elements = document.querySelectorAll('*[' + __(_options.animAttribute) + ']');
4057

4158
for (var i = 0;i < elements.length;i++) {
59+
var param = {};
4260
var element = elements[i];
43-
var event = element.getAttribute(_options.attribute);
61+
var anim = element.getAttribute(__(_options.animAttribute));
62+
var event = element.getAttribute(__(_options.eventAttribute)) || 'in';
63+
64+
param[event] = anim;
4465

45-
_add(element, {
46-
'in': event
47-
});
66+
_add(element, param);
4867
}
4968
};
5069

@@ -72,6 +91,8 @@
7291
};
7392
}
7493

94+
kissuiPosition.add(element, 'out');
95+
7596
// add visibility: hidden to the element
7697
element.style.opacity = '0';
7798

@@ -146,6 +167,37 @@
146167
}
147168
};
148169

170+
/**
171+
* Clear animation, reset `opacity` and `active` flag on an element
172+
*
173+
*/
174+
function _resetElement (element) {
175+
var elx = _find(element)
176+
177+
for (var e in elx.event) {
178+
elx.event[e].active = false;
179+
}
180+
181+
element.style.opacity = 0;
182+
};
183+
184+
/**
185+
* Set option
186+
*
187+
*/
188+
function _setOption (name, value) {
189+
_options[name] = value;
190+
};
191+
192+
/**
193+
* Set an object of options
194+
*/
195+
function _setOptions (options) {
196+
for (var o in options) {
197+
_setOption(o, options[o]);
198+
}
199+
};
200+
149201
/**
150202
* Start the module
151203
*/
@@ -156,6 +208,13 @@
156208
_attach(_find(element), event);
157209
});
158210

211+
// to manage `autoReset`
212+
kissuiPosition.on('out', function (element) {
213+
if (_options.autoReset) {
214+
_resetElement(element);
215+
}
216+
});
217+
159218
kissuiPosition.init();
160219
};
161220

@@ -165,6 +224,8 @@
165224
_options: _options,
166225
_elements: _elements,
167226
init: _init,
168-
add: _add
227+
add: _add,
228+
setOption: _setOption,
229+
setOptions: _setOptions
169230
};
170231
}));

0 commit comments

Comments
 (0)