I have looked at Move HTML element upwards on hover and I know how to define the CSS animation I want, however given the other animations at play in this snippet I am running into issues animating a child element on parent hover.
Snippet:
JavaScript
x
98
98
1
html,
2
body {
3
padding: 0;
4
margin: 0;
5
font-family: "sequel-sans-roman", -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
6
}
7
8
.container {
9
padding: 0 2rem;
10
}
11
12
.main {
13
min-height: 100vh;
14
padding: 4rem 0;
15
flex: 1;
16
display: flex;
17
flex-direction: column;
18
justify-content: center;
19
align-items: center;
20
}
21
22
a {
23
color: inherit;
24
text-decoration: none;
25
}
26
27
* {
28
box-sizing: border-box;
29
}
30
31
.navIcon {
32
display: inline-block;
33
flex-grow: 1;
34
}
35
36
.nav {
37
display: flex;
38
justify-content: space-between;
39
width: 100%;
40
margin-top: 2.4em;
41
/* coordinates w height of line away from link, MUST BE = */
42
}
43
44
.snip1168 {
45
text-align: center;
46
text-transform: uppercase;
47
}
48
49
.snip1168 * {
50
box-sizing: border-box;
51
}
52
53
.snip1168 li {
54
display: inline-block;
55
list-style: outside none none;
56
margin: 0 1.5em;
57
padding: 0;
58
}
59
60
.snip1168 a {
61
padding: 2.4em 0 0.5em;
62
/* height of line away from link */
63
color: rgba(0, 0, 0, 1);
64
position: relative;
65
text-decoration: none;
66
}
67
68
.snip1168 a:before,
69
.snip1168 a:after {
70
position: absolute;
71
-webkit-transition: all 0.35s ease;
72
transition: all 0.35s ease;
73
}
74
75
.snip1168 a:before {
76
top: 0;
77
display: block;
78
height: 3px;
79
width: 0%;
80
content: "";
81
background-color: black;
82
}
83
84
.snip1168 a:hover:before,
85
.snip1168 .current a:before {
86
opacity: 1;
87
width: 100%;
88
}
89
90
.snip1168 a:hover:after,
91
.snip1168 .current a:after {
92
max-width: 100%;
93
}
94
95
.mainText {
96
text-transform: uppercase;
97
font-size: 1.1rem;
98
}
JavaScript
1
14
14
1
<div class='container'>
2
<main class='main'>
3
<div class='nav'>
4
<div class='navIcon'>
5
<img src="https://picsum.photos/40" height={40} width={40} />
6
</div>
7
<ul class='snip1168'>
8
<li class='current'><a href="#" data-hover="Work">Work</a></li>
9
<li><a href="#" data-hover="Recs">Recs</a></li>
10
<li><a href="#" data-hover="Say Hi">Say Hi</a></li>
11
</ul>
12
</div>
13
</main>
14
</div>
This is the animation I want to apply to only the text of the nav bar (not the animated lines) on hover of the whole <li>
:
JavaScript
1
3
1
transition: transform ease 400ms;
2
transform: translate(0, -10px);
3
Without breaking the existing line animations, how can I animate move only the text of these nav items on hover of each parent <li>
?
Advertisement
Answer
You could move the line drawing part to the li
rather than the a
element and on the a
element put the translate upwards. That way you don’t have to alter the HTML.
JavaScript
1
112
112
1
html,
2
body {
3
padding: 0;
4
margin: 0;
5
font-family: "sequel-sans-roman", -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Oxygen, Ubuntu, Cantarell, Fira Sans, Droid Sans, Helvetica Neue, sans-serif;
6
}
7
8
.container {
9
padding: 0 2rem;
10
}
11
12
.main {
13
min-height: 100vh;
14
padding: 4rem 0;
15
flex: 1;
16
display: flex;
17
flex-direction: column;
18
justify-content: center;
19
align-items: center;
20
}
21
22
a {
23
color: inherit;
24
text-decoration: none;
25
}
26
27
* {
28
box-sizing: border-box;
29
}
30
31
.navIcon {
32
display: inline-block;
33
flex-grow: 1;
34
}
35
36
.nav {
37
display: flex;
38
justify-content: space-between;
39
width: 100%;
40
margin-top: 2.4em;
41
/* coordinates w height of line away from link, MUST BE = */
42
}
43
44
.snip1168 {
45
text-align: center;
46
text-transform: uppercase;
47
}
48
49
.snip1168 * {
50
box-sizing: border-box;
51
}
52
53
.snip1168 li {
54
display: inline-block;
55
list-style: outside none none;
56
margin: 0 1.5em;
57
padding: 0;
58
background: ppink;
59
}
60
61
.snip1168 li {
62
padding: 2.4em 0 0.5em;
63
/* height of line away from link */
64
color: rgba(0, 0, 0, 1);
65
position: relative;
66
text-decoration: none;
67
background: pink;
68
display: inline-block;
69
}
70
71
.snip1168 li:before,
72
.snip1168 li:after {
73
position: absolute;
74
-webkit-transition: all 0.35s ease;
75
transition: all 0.35s ease;
76
}
77
78
.snip1168 li:before {
79
top: 0;
80
display: inline-block;
81
height: 3px;
82
width: 0%;
83
content: "";
84
background-color: black;
85
}
86
87
.snip1168 li:hover:before,
88
.snip1168 .current a:before {
89
opacity: 1;
90
width: 100%;
91
}
92
93
.snip1168 li:hover:after,
94
.snip1168 .current li:after {
95
max-width: 100%;
96
}
97
98
.mainText {
99
text-transform: uppercase;
100
font-size: 1.1rem;
101
}
102
103
.snip1168 li a {
104
transition: transform ease 400ms;
105
transform: translate(0, 0);
106
display: inline-block;
107
}
108
109
.snip1168 li:hover a {
110
transition: transform ease 400ms;
111
transform: translate(0, -10px);
112
}
JavaScript
1
14
14
1
<div class='container'>
2
<main class='main'>
3
<div class='nav'>
4
<div class='navIcon'>
5
<img src="https://picsum.photos/40" height={40} width={40} />
6
</div>
7
<ul class='snip1168'>
8
<li class='current'><a href="#" data-hover="Work">Work</a></li>
9
<li><a href="#" data-hover="Recs">Recs</a></li>
10
<li><a href="#" data-hover="Say Hi">Say Hi</a></li>
11
</ul>
12
</div>
13
</main>
14
</div>