@@ -214,6 +214,38 @@ <h2>Cool</h2>
214
214
</ style >
215
215
216
216
< script >
217
+ const triggers = document . querySelectorAll ( '.cool > li' ) ;
218
+ const background = document . querySelector ( '.dropdownBackground' ) ;
219
+ const nav = document . querySelector ( '.top' ) ;
220
+
221
+ function handleEnter ( ) {
222
+ this . classList . add ( 'trigger-enter' ) ;
223
+ setTimeout ( ( ) => this . classList . contains ( 'trigger-enter' ) && this . classList . add ( 'trigger-enter-active' ) , 150 ) ;
224
+ background . classList . add ( 'open' ) ;
225
+
226
+ const dropdown = this . querySelector ( '.dropdown' ) ;
227
+ const dropdownCoords = dropdown . getBoundingClientRect ( ) ;
228
+ const navCoords = nav . getBoundingClientRect ( ) ;
229
+
230
+ const coords = {
231
+ height : dropdownCoords . height ,
232
+ width : dropdownCoords . width ,
233
+ top : dropdownCoords . top - navCoords . top ,
234
+ left : dropdownCoords . left - navCoords . left
235
+ } ;
236
+
237
+ background . style . setProperty ( 'width' , `${ coords . width } px` ) ;
238
+ background . style . setProperty ( 'height' , `${ coords . height } px` ) ;
239
+ background . style . setProperty ( 'transform' , `translate(${ coords . left } px, ${ coords . top } px)` ) ;
240
+ }
241
+
242
+ function handleLeave ( ) {
243
+ this . classList . remove ( 'trigger-enter' , 'trigger-enter-active' ) ;
244
+ background . classList . remove ( 'open' ) ;
245
+ }
246
+
247
+ triggers . forEach ( trigger => trigger . addEventListener ( 'mouseenter' , handleEnter ) ) ;
248
+ triggers . forEach ( trigger => trigger . addEventListener ( 'mouseleave' , handleLeave ) ) ;
217
249
</ script >
218
250
219
251
</ body >
0 commit comments