@@ -220,6 +220,44 @@ <h2>Cool</h2>
220220</ style >
221221
222222< script >
223+ const triggers = document . querySelectorAll ( '.cool > li' ) ;
224+ const background = document . querySelector ( '.dropdownBackground' ) ;
225+ const nav = document . querySelector ( '.top' ) ;
226+
227+ function handleEnter ( ) {
228+ this . classList . add ( 'trigger-enter' ) ;
229+ setTimeout ( ( ) => {
230+ if ( this . classList . contains ( 'trigger-enter' ) ) {
231+ this . classList . add ( 'trigger-enter-active' ) ;
232+ }
233+ } , 150 ) ;
234+ background . classList . add ( 'open' ) ;
235+
236+ const dropdown = this . querySelector ( '.dropdown' ) ;
237+ const dropdownCoords = dropdown . getBoundingClientRect ( ) ; // absolute coords, not relative to its container
238+ const navCoords = nav . getBoundingClientRect ( ) ;
239+
240+ const coords = {
241+ height : dropdownCoords . height ,
242+ width : dropdownCoords . width ,
243+ top : dropdownCoords . top - navCoords . top ,
244+ left : dropdownCoords . left - navCoords . left
245+ } ;
246+
247+ background . style . setProperty ( 'width' , `${ coords . width } px` ) ;
248+ background . style . setProperty ( 'height' , `${ coords . height } px` ) ;
249+ background . style . setProperty ( 'transform' , `translate(${ coords . left } px, ${ coords . top } px` ) ;
250+
251+ console . log ( coords )
252+ }
253+
254+ function handleLeave ( ) {
255+ this . classList . remove ( 'trigger-enter' , 'trigger-enter-active' ) ;
256+ background . classList . remove ( 'open' ) ;
257+ }
258+
259+ triggers . forEach ( trigger => trigger . addEventListener ( 'mouseenter' , handleEnter ) ) ;
260+ triggers . forEach ( trigger => trigger . addEventListener ( 'mouseleave' , handleLeave ) ) ;
223261</ script >
224262
225263</ body >
0 commit comments