@@ -19,23 +19,27 @@ function updateNav() {
19
19
// The visible list is overflowing the nav
20
20
if ( $vlinks . width ( ) > availableSpace ) {
21
21
22
- // Record the width of the list
23
- breaks . push ( $vlinks . width ( ) ) ;
22
+ while ( $vlinks . width ( ) > availableSpace && $vlinks . children ( '*:not(.masthead__menu-item--lg)' ) . length > 0 ) {
24
23
25
- // Move item to the hidden list
26
- $vlinks . children ( '*:not(.masthead__menu-item--lg)' ) . last ( ) . prependTo ( $hlinks ) ;
24
+ // Record the width of the list
25
+ breaks . push ( $vlinks . width ( ) ) ;
27
26
28
- // Show the dropdown btn
29
- if ( $btn . hasClass ( 'hidden' ) ) {
30
- $btn . removeClass ( 'hidden' ) ;
27
+ // Move item to the hidden list
28
+ $vlinks . children ( '*:not(.masthead__menu-item--lg)' ) . last ( ) . prependTo ( $hlinks ) ;
29
+
30
+ availableSpace = $btn . hasClass ( 'hidden' ) ? $nav . width ( ) : $nav . width ( ) - $btn . width ( ) - 30 ;
31
+
32
+ // Show the dropdown btn
33
+ if ( $btn . hasClass ( 'hidden' ) ) {
34
+ $btn . removeClass ( 'hidden' ) ;
35
+ }
31
36
}
32
37
33
- // The visible list is not overflowing
38
+ // The visible list is not overflowing
34
39
} else {
35
40
36
41
// There is space for another item in the nav
37
- if ( availableSpace > breaks [ breaks . length - 1 ] ) {
38
-
42
+ while ( breaks . length > 0 && availableSpace > breaks [ breaks . length - 1 ] ) {
39
43
// Move the item to the visible list
40
44
$hlinks . children ( ) . first ( ) . appendTo ( $vlinks ) ;
41
45
breaks . pop ( ) ;
@@ -44,23 +48,22 @@ function updateNav() {
44
48
// Hide the dropdown btn if hidden list is empty
45
49
if ( breaks . length < 1 ) {
46
50
$btn . addClass ( 'hidden' ) ;
51
+ $btn . removeClass ( 'close' ) ;
47
52
$hlinks . addClass ( 'hidden' ) ;
48
53
}
49
54
}
50
55
51
56
// Keep counter updated
52
57
$btn . attr ( "count" , breaks . length ) ;
53
58
54
- // Recur if the visible list is still overflowing the nav
55
- if ( $vlinks . width ( ) > availableSpace ) {
56
- updateNav ( ) ;
57
- }
58
-
59
59
}
60
60
61
61
// Window listeners
62
62
63
- $ ( window ) . resize ( function ( ) {
63
+ $ ( window ) . on ( 'resize' , function ( ) {
64
+ updateNav ( ) ;
65
+ } ) ;
66
+ screen . orientation . addEventListener ( "change" , function ( ) {
64
67
updateNav ( ) ;
65
68
} ) ;
66
69
0 commit comments