File tree Expand file tree Collapse file tree 8 files changed +178
-0
lines changed Expand file tree Collapse file tree 8 files changed +178
-0
lines changed Original file line number Diff line number Diff line change
1
+ # Dropdown Menu
2
+
3
+ We've set up a dropdown menu in this exercise. Load up the page, you can see a single menu title, with a dropdown menu that will open upon clicking on the title.
4
+
5
+ Your task is to add animation to the dropdown menu so that it will have an effect of growing down. Check out the desired outcome below, and notice the _ bounce_ illusion when the dropdown grows close to its final end state.
6
+
7
+ ### Hints
8
+ - You need to specify a _ transform-origin_ property to make the dropdown menu start transforming from the top
9
+ - You need to add an intermediate step to the keyframe at rule to implement the _ bounce_ illusion.
10
+
11
+ ## Desired Outcome
12
+
13
+ ![ outcome] ( ./desired-outcome.gif )
14
+
15
+ ### Self Check
16
+
17
+ - The dropdown menu grows down after you click on the menu title
18
+ - There's a _ bounce_ illusion towards the end of the animation
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge " />
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7
+ < title > Dropdown Menu</ title >
8
+ < link rel ="stylesheet " href ="style.css " />
9
+ </ head >
10
+ < body >
11
+ < div class ="dropdown-container ">
12
+ < div class ="menu-title "> MENU</ div >
13
+ < ul class ="dropdown-menu ">
14
+ < li > ITEM 1</ li >
15
+ < li > ITEM 2</ li >
16
+ < li > ITEM 3</ li >
17
+ < li > ITEM 4</ li >
18
+ < li > ITEM 5</ li >
19
+ </ ul >
20
+ </ div >
21
+ < script src ="script.js "> </ script >
22
+ </ body >
23
+ </ html >
Original file line number Diff line number Diff line change
1
+ const dropdownContainer = document . querySelector ( ".dropdown-container" ) ;
2
+ const menuTitle = document . querySelector ( ".menu-title" ) ;
3
+ const dropdownMenu = document . querySelector ( ".dropdown-menu" ) ;
4
+
5
+ menuTitle . addEventListener ( "click" , ( e ) => {
6
+ if ( e . target === e . currentTarget ) {
7
+ dropdownMenu . classList . toggle ( "visible" ) ;
8
+ }
9
+ } )
10
+
11
+ window . addEventListener ( "click" , ( e ) => {
12
+ if ( ! dropdownContainer . contains ( e . target ) ) {
13
+ dropdownMenu . classList . remove ( "visible" )
14
+ }
15
+ } )
Original file line number Diff line number Diff line change
1
+ .dropdown-container {
2
+ max-width : 250px ;
3
+ margin : 40px auto;
4
+ text-align : center;
5
+ line-height : 50px ;
6
+ font-size : 15px ;
7
+ color : rgb (247 , 247 , 247 );
8
+ cursor : pointer;
9
+ }
10
+
11
+ .menu-title {
12
+ background-color : rgb (163 , 162 , 162 );
13
+ }
14
+
15
+ .dropdown-menu {
16
+ list-style : none;
17
+ padding : 0 ;
18
+ margin : 0 ;
19
+ background-color : rgb (99 , 97 , 97 );
20
+
21
+ display : none;
22
+ }
23
+
24
+ ul .dropdown-menu li : hover {
25
+ background : rgb (47 , 46 , 46 );
26
+ }
27
+
28
+ .visible {
29
+ display : block;
30
+ }
31
+
32
+ /* SOLUTION */
33
+
34
+ .visible {
35
+ animation : goDown 500ms ease-in-out;
36
+ transform-origin : top;
37
+ }
38
+
39
+ @keyframes goDown {
40
+ 0% {
41
+ transform : scaleY (0 );
42
+ }
43
+
44
+ 70% {
45
+ transform : scaleY (1.1 );
46
+ }
47
+
48
+ 100% {
49
+ transform : scaleY (1 );
50
+ }
51
+ }
52
+
Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html lang ="en ">
3
+ < head >
4
+ < meta charset ="UTF-8 " />
5
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge " />
6
+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 " />
7
+ < title > Dropdown Menu</ title >
8
+ < link rel ="stylesheet " href ="solution.css " />
9
+ </ head >
10
+ < body >
11
+ < div class ="dropdown-container ">
12
+ < div class ="menu-title "> MENU</ div >
13
+ < ul class ="dropdown-menu ">
14
+ < li > ITEM 1</ li >
15
+ < li > ITEM 2</ li >
16
+ < li > ITEM 3</ li >
17
+ < li > ITEM 4</ li >
18
+ < li > ITEM 5</ li >
19
+ </ ul >
20
+ </ div >
21
+ < script src ="solution.js "> </ script >
22
+ </ body >
23
+ </ html >
Original file line number Diff line number Diff line change
1
+ const dropdownContainer = document . querySelector ( ".dropdown-container" ) ;
2
+ const menuTitle = document . querySelector ( ".menu-title" ) ;
3
+ const dropdownMenu = document . querySelector ( ".dropdown-menu" ) ;
4
+
5
+ menuTitle . addEventListener ( "click" , ( e ) => {
6
+ if ( e . target === e . currentTarget ) {
7
+ dropdownMenu . classList . toggle ( "visible" ) ;
8
+ }
9
+ } )
10
+
11
+ window . addEventListener ( "click" , ( e ) => {
12
+ if ( ! dropdownContainer . contains ( e . target ) ) {
13
+ dropdownMenu . classList . remove ( "visible" )
14
+ }
15
+ } )
Original file line number Diff line number Diff line change
1
+ .dropdown-container {
2
+ max-width : 250px ;
3
+ margin : 40px auto;
4
+ text-align : center;
5
+ line-height : 50px ;
6
+ font-size : 15px ;
7
+ color : rgb (247 , 247 , 247 );
8
+ cursor : pointer;
9
+ }
10
+
11
+ .menu-title {
12
+ background-color : rgb (163 , 162 , 162 );
13
+ }
14
+
15
+ .dropdown-menu {
16
+ list-style : none;
17
+ padding : 0 ;
18
+ margin : 0 ;
19
+ background-color : rgb (99 , 97 , 97 );
20
+
21
+ display : none;
22
+ }
23
+
24
+ ul .dropdown-menu li : hover {
25
+ background : rgb (47 , 46 , 46 );
26
+ }
27
+
28
+ .visible {
29
+ display : block;
30
+ }
31
+
32
+
You can’t perform that action at this time.
0 commit comments