1
1
/**
2
- * Detect Element Rezise
2
+ * Detect Element Resize
3
3
*
4
4
* https://github.com/sdecima/javascript-detect-element-resize
5
5
* Sebastian Decima
6
6
*
7
- * Based on the works of Back Alley Coder:
8
- * http://www.backalleycoder.com/2013/03/18/cross-browser-event-based-element-resize-detection/
7
+ * version: 0.2
9
8
**/
10
9
11
10
function addFlowListener ( element , type , fn ) {
@@ -74,17 +73,17 @@ function addResizeListener(element, fn){
74
73
matchFlow ( { } ) ;
75
74
}
76
75
var events = element . _flowEvents || ( element . _flowEvents = [ ] ) ;
77
- if ( events . indexOf ( fn ) == - 1 ) events . push ( fn ) ;
76
+ if ( indexOf . call ( events , fn ) == - 1 ) events . push ( fn ) ;
78
77
if ( ! resize ) element . addEventListener ( 'resize' , fn , false ) ;
79
78
element . onresize = function ( e ) {
80
- events . forEach ( function ( fn ) {
79
+ forEach . call ( events , function ( fn ) {
81
80
fn . call ( element , e ) ;
82
81
} ) ;
83
82
} ;
84
83
} ;
85
84
86
85
function removeResizeListener ( element , fn ) {
87
- var index = element . _flowEvents . indexOf ( fn ) ;
86
+ var index = indexOf . call ( element . _flowEvents , fn ) ;
88
87
if ( index > - 1 ) element . _flowEvents . splice ( index , 1 ) ;
89
88
if ( ! element . _flowEvents . length ) {
90
89
var sensor = element . _resizeSensor ;
@@ -97,4 +96,41 @@ function removeResizeListener(element, fn){
97
96
delete element . _flowEvents ;
98
97
}
99
98
element . removeEventListener ( 'resize' , fn ) ;
99
+ } ;
100
+
101
+ /* Array.indexOf for IE < 9 */
102
+ var indexOf = function ( needle ) {
103
+ if ( typeof Array . prototype . indexOf === 'function' ) {
104
+ indexOf = Array . prototype . indexOf ;
105
+ } else {
106
+ indexOf = function ( needle ) {
107
+ var i = - 1 , index = - 1 ;
108
+
109
+ for ( i = 0 ; i < this . length ; i ++ ) {
110
+ if ( this [ i ] === needle ) {
111
+ index = i ;
112
+ break ;
113
+ }
114
+ }
115
+
116
+ return index ;
117
+ } ;
118
+ }
119
+
120
+ return indexOf . call ( this , needle ) ;
121
+ } ;
122
+
123
+ /* Array.forEach for IE < 9 */
124
+ var forEach = function ( action , that ) {
125
+ if ( typeof Array . prototype . forEach === 'function' ) {
126
+ forEach = Array . prototype . forEach ;
127
+ } else {
128
+ forEach = function ( action , that ) {
129
+ for ( var i = 0 , n = this . length ; i < n ; i ++ )
130
+ if ( i in this )
131
+ action . call ( that , this [ i ] , i , this ) ;
132
+ } ;
133
+ }
134
+
135
+ return forEach . call ( this , action , that ) ;
100
136
} ;
0 commit comments