@@ -70,4 +70,121 @@ $(document).ready(function(){
70
70
}
71
71
}
72
72
} ) ;
73
- } ) ;
73
+
74
+ const timeline = document . querySelector ( ".timeline ol" ) ,
75
+ elH = document . querySelectorAll ( ".timeline li > div" ) ,
76
+ arrows = document . querySelectorAll ( ".timeline .arrows .arrow" ) ,
77
+ arrowPrev = document . querySelector ( ".timeline .arrows .arrow__prev" ) ,
78
+ arrowNext = document . querySelector ( ".timeline .arrows .arrow__next" ) ,
79
+ firstItem = document . querySelector ( ".timeline li:first-child" ) ,
80
+ lastItem = document . querySelector ( ".timeline li:last-child" ) ,
81
+ xScrolling = 280 ,
82
+ disabledClass = "disabled" ;
83
+
84
+ // START
85
+ window . addEventListener ( "load" , init ) ;
86
+
87
+ function init ( ) {
88
+ setEqualHeights ( elH ) ;
89
+ animateTl ( xScrolling , arrows , timeline ) ;
90
+ setSwipeFn ( timeline , arrowPrev , arrowNext ) ;
91
+ setKeyboardFn ( arrowPrev , arrowNext ) ;
92
+ }
93
+
94
+ // SET EQUAL HEIGHTS
95
+ function setEqualHeights ( el ) {
96
+ let counter = 0 ;
97
+ for ( let i = 0 ; i < el . length ; i ++ ) {
98
+ const singleHeight = el [ i ] . offsetHeight ;
99
+
100
+ if ( counter < singleHeight ) {
101
+ counter = singleHeight ;
102
+ }
103
+ }
104
+
105
+ for ( let i = 0 ; i < el . length ; i ++ ) {
106
+ el [ i ] . style . height = `${ counter } px` ;
107
+ }
108
+ }
109
+
110
+ // CHECK IF AN ELEMENT IS IN VIEWPORT
111
+ // http://stackoverflow.com/questions/123999/how-to-tell-if-a-dom-element-is-visible-in-the-current-viewport
112
+ function isElementInViewport ( el ) {
113
+ const rect = el . getBoundingClientRect ( ) ;
114
+ return (
115
+ rect . top >= 0 &&
116
+ rect . left >= 0 &&
117
+ rect . bottom <= ( window . innerHeight || document . documentElement . clientHeight ) &&
118
+ rect . right <= ( window . innerWidth || document . documentElement . clientWidth )
119
+ ) ;
120
+ }
121
+
122
+ // SET STATE OF PREV/NEXT ARROWS
123
+ function setBtnState ( el , flag = true ) {
124
+ if ( flag ) {
125
+ el . classList . add ( disabledClass ) ;
126
+ } else {
127
+ if ( el . classList . contains ( disabledClass ) ) {
128
+ el . classList . remove ( disabledClass ) ;
129
+ }
130
+ el . disabled = false ;
131
+ }
132
+ }
133
+
134
+ // ANIMATE TIMELINE
135
+ function animateTl ( scrolling , el , tl ) {
136
+ let counter = 0 ;
137
+ for ( let i = 0 ; i < el . length ; i ++ ) {
138
+ el [ i ] . addEventListener ( "click" , function ( ) {
139
+ if ( ! arrowPrev . disabled ) {
140
+ arrowPrev . disabled = true ;
141
+ }
142
+ if ( ! arrowNext . disabled ) {
143
+ arrowNext . disabled = true ;
144
+ }
145
+ const sign = ( this . classList . contains ( "arrow__prev" ) ) ? "" : "-" ;
146
+ if ( counter === 0 ) {
147
+ tl . style . transform = `translateX(-${ scrolling } px)` ;
148
+ } else {
149
+ const tlStyle = getComputedStyle ( tl ) ;
150
+ // add more browser prefixes if needed here
151
+ const tlTransform = tlStyle . getPropertyValue ( "-webkit-transform" ) || tlStyle . getPropertyValue ( "transform" ) ;
152
+ const values = parseInt ( tlTransform . split ( "," ) [ 4 ] ) + parseInt ( `${ sign } ${ scrolling } ` ) ;
153
+ tl . style . transform = `translateX(${ values } px)` ;
154
+ }
155
+
156
+ setTimeout ( ( ) => {
157
+ isElementInViewport ( firstItem ) ? setBtnState ( arrowPrev ) : setBtnState ( arrowPrev , false ) ;
158
+ isElementInViewport ( lastItem ) ? setBtnState ( arrowNext ) : setBtnState ( arrowNext , false ) ;
159
+ } , 1100 ) ;
160
+
161
+ counter ++ ;
162
+ } ) ;
163
+ }
164
+ }
165
+
166
+ // ADD SWIPE SUPPORT FOR TOUCH DEVICES
167
+ function setSwipeFn ( tl , prev , next ) {
168
+ // const hammer = new Hammer(tl);
169
+ // hammer.on("swipeleft", () => next.click());
170
+ // hammer.on("swiperight", () => prev.click());
171
+ }
172
+
173
+ // ADD BASIC KEYBOARD FUNCTIONALITY
174
+ function setKeyboardFn ( prev , next ) {
175
+ document . addEventListener ( "keydown" , ( e ) => {
176
+ if ( ( e . which === 37 ) || ( e . which === 39 ) ) {
177
+ const timelineOfTop = timeline . offsetTop ;
178
+ const y = window . pageYOffset ;
179
+ if ( timelineOfTop !== y ) {
180
+ window . scrollTo ( 0 , timelineOfTop ) ;
181
+ }
182
+ if ( e . which === 37 ) {
183
+ prev . click ( ) ;
184
+ } else if ( e . which === 39 ) {
185
+ next . click ( ) ;
186
+ }
187
+ }
188
+ } ) ;
189
+ }
190
+ } ) ;
0 commit comments