Skip to content

Commit 1aff5ec

Browse files
Using HTML5 data attributes on watched elements to find target handlers faster. Tweaks to example.scss (removed -moz- prefixes).
1 parent c7f9a46 commit 1aff5ec

File tree

5 files changed

+124
-201
lines changed

5 files changed

+124
-201
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# CSS animation event
2-
A very small (approx **820 bytes** minified and gzip) cross browser compatible library to handle CSS3 animation and transition DOM events with a fall back pattern for unsupported browsers. Tested successfully with Google Chrome, Firefox, Opera (Presto) and IE10.
2+
A very small (approx **840 bytes** minified and gzipped) cross browser compatible library to handle CSS3 animation and transition DOM events with a fall back pattern for unsupported browsers. Tested successfully with Google Chrome, Firefox, Opera (Presto) and IE10.
33

44
- [Usage](#usage)
55
- [Example](#example)
@@ -139,7 +139,7 @@ As a small bonus, `CSSAnimEvent` also adds a handy CSS styling hook; `cssanimact
139139
<div id="movethis" class="movethis-basestyle">
140140

141141
<!-- our element after onAnimationEnd()/onTransitionEnd() called -->
142-
<div id="movethis" class="movethis-basestyle cssanimactive">
142+
<div id="movethis" class="movethis-basestyle cssanimactive" data-cssanimid="ID">
143143

144144
<!-- and finally, once the animation/transition ends -->
145145
<div id="movethis" class="movethis-basestyle">

cssanimevent.js

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
'use strict';
44

55
var TRIM_REGEXP = /^\s+|\s+$/g,
6+
DATA_ATTRIBUTE_HANDLER_ID = 'cssanimid',
7+
DATA_ATTRIBUTE_HANDLER_ID_PREFIXED = 'data-' + DATA_ATTRIBUTE_HANDLER_ID,
68
CLASS_NAME_ANIM_ACTIVE = ' cssanimactive',
79
IS_OPERA_EVENT_TYPE_REGEXP = /^o[AT]/,
810
HANDLER_LIST_INDEX_ANIMATION = 0,
@@ -12,15 +14,16 @@
1214
animationEventTypeIteration,
1315
animationEventTypeEnd,
1416
transitionEventTypeEnd,
15-
handlerList = [undefined,undefined];
17+
nextHandlerID = 0,
18+
handlerCollection = [undefined,undefined];
1619

1720
function detect() {
1821

1922
// if already detected support then exit
2023
if (isDetected) return;
2124
isDetected = true;
2225

23-
// list of animation/transition style properties per browser engine and matching DOM event names
26+
// list of animation/transition properties per browser engine and matching DOM event names
2427
// the non-prefixed properties are intentionally checked first
2528
var ANIMATION_DETECT_LIST = [
2629
['animation','animationstart','animationiteration','animationend'],
@@ -65,7 +68,7 @@
6568

6669
obj.addEventListener(type,handler,false);
6770
if (IS_OPERA_EVENT_TYPE_REGEXP.test(type)) {
68-
// some earlier versions of Opera (Presto) need lowercased event names
71+
// some earlier versions of Opera (Presto) raise lowercased event names
6972
obj.addEventListener(type.toLowerCase(),handler,false);
7073
}
7174

@@ -76,39 +79,40 @@
7679

7780
obj.removeEventListener(type,handler,false);
7881
if (IS_OPERA_EVENT_TYPE_REGEXP.test(type)) {
79-
// some earlier versions of Opera (Presto) need lowercased event names
82+
// some earlier versions of Opera (Presto) raise lowercased event names
8083
obj.removeEventListener(type.toLowerCase(),handler,false);
8184
}
8285

8386
return true;
8487
}
8588

86-
function getElHandlerListIndex(handlerIndex,el) {
89+
function getElHandlerCollectionID(handlerIndex,el) {
8790

88-
var seekList = handlerList[handlerIndex];
89-
if (seekList !== undefined) {
90-
for (var index = seekList.length - 1;index >= 0;index--) {
91-
if (seekList[index][0] == el) {
92-
// found element, return numeric index
93-
return index;
94-
}
95-
}
96-
}
91+
var handlerID = (el.hasAttribute(DATA_ATTRIBUTE_HANDLER_ID_PREFIXED))
92+
? el.getAttribute(DATA_ATTRIBUTE_HANDLER_ID_PREFIXED)
93+
: null;
9794

98-
// element not found in handler list
99-
return false;
95+
return (
96+
(handlerID !== null) &&
97+
(handlerCollection[handlerIndex][handlerID] !== undefined)
98+
)
99+
// found element in collection, return handlerID
100+
? handlerID
101+
// not found
102+
: false;
100103
}
101104

102-
function removeElHandlerItem(handlerIndex,el,index) {
105+
function removeElHandlerItem(handlerIndex,el,handlerID) {
103106

104-
// if index to remove has been given, don't call getElHandlerListIndex()
105-
if (index === undefined) index = getElHandlerListIndex(handlerIndex,el);
107+
// if handlerID to remove has been given, don't call getElHandlerCollectionID()
108+
if (handlerID === undefined) handlerID = getElHandlerCollectionID(handlerIndex,el);
106109

107-
if (index !== false) {
108-
// found element in list, remove from handler list array
109-
handlerList[handlerIndex].splice(index,1);
110+
if (handlerID !== false) {
111+
// found element in collection, now remove
112+
delete handlerCollection[handlerIndex][handlerID];
110113

111-
// drop the 'animation active' class from element
114+
// drop data-* attribute and 'animation active' class from element
115+
el.removeAttribute(DATA_ATTRIBUTE_HANDLER_ID_PREFIXED);
112116
el.className =
113117
(' ' + el.className + ' ').
114118
replace(CLASS_NAME_ANIM_ACTIVE + ' ',' ').
@@ -123,22 +127,22 @@
123127
setTimeout(function() { handler(el,data); });
124128

125129
} else {
126-
if (!handlerList[handlerIndex]) {
130+
if (!handlerCollection[handlerIndex]) {
127131
// setup end handler
128-
handlerList[handlerIndex] = [];
132+
handlerCollection[handlerIndex] = {};
129133
addEvent(docEl,eventTypeEnd,function(event) {
130134

131135
// ensure event returned the target element
132136
if (event.target) {
133-
// get the element handler list index - skip over event if not found
137+
// get the element handler list ID - skip over if not found
134138
var targetEl = event.target,
135-
index = getElHandlerListIndex(handlerIndex,targetEl);
139+
handlerID = getElHandlerCollectionID(handlerIndex,targetEl);
136140

137-
if (index !== false) {
141+
if (handlerID !== false) {
138142
// execute handler then remove from handler list
139-
var handlerItem = handlerList[handlerIndex][index];
140-
removeElHandlerItem(handlerIndex,targetEl,index);
141-
handlerItem[1](targetEl,handlerItem[2]);
143+
var handlerItem = handlerCollection[handlerIndex][handlerID];
144+
removeElHandlerItem(handlerIndex,targetEl,handlerID);
145+
handlerItem[0](targetEl,handlerItem[1]);
142146
}
143147
}
144148
});
@@ -148,7 +152,9 @@
148152
removeElHandlerItem(handlerIndex,el);
149153

150154
// add element to handler list and a 'animation active' class identifier to the target element
151-
handlerList[handlerIndex].push([el,handler,data]);
155+
nextHandlerID++;
156+
el.setAttribute(DATA_ATTRIBUTE_HANDLER_ID_PREFIXED,nextHandlerID);
157+
handlerCollection[handlerIndex][nextHandlerID] = [handler,data];
152158
el.className = el.className.replace(TRIM_REGEXP,'') + CLASS_NAME_ANIM_ACTIVE;
153159
}
154160
}

cssanimevent.min.js

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

0 commit comments

Comments
 (0)