Hello everyone, I have a problem with my navigation. Here is a Hamburg menu which opens a navigation. This opens through pure CSS. When I click on the navigation link, the link goes to the section but the navigation itself does not close. I got a .removeClass (“change”); read but don’t know exactly how to use it. Maybe it works without Javascript. Please help me. Many Thanks. best regards
JavaScript
x
219
219
1
html {
2
font-size: 62.5%;
3
}
4
h2, .h2 {
5
text-transform: uppercase;
6
}
7
body {
8
box-sizing: border-box;
9
font-family: 'Lato', sans-serif;
10
font-weight: 400;
11
font-size: 16px;
12
line-height: 1.7;
13
color: #777;
14
padding: 3rem;
15
}
16
.heading-primary {
17
color: #fff;
18
text-transform: uppercase;
19
backface-visibility: hidden;
20
margin-bottom: 6rem;
21
}
22
.heading-primary--main {
23
display: block;
24
font-size: 6rem;
25
font-weight: 400;
26
letter-spacing: 3.5rem;
27
margin-right: -3.5rem;
28
animation-name: moveInLeft;
29
animation-duration: 1s;
30
animation-timing-function: ease-out;
31
}
32
.heading-primary--sub {
33
display: block;
34
font-size: 2rem;
35
font-weight: 700;
36
letter-spacing: 1.75rem;
37
margin-right: -1.75rem;
38
animation: moveInRight 1s ease-out;
39
}
40
.navigation__checkbox {
41
display: none;
42
}
43
.navigation__checkbox:checked ~ .navigation__background {
44
transform: scale(100);
45
}
46
.navigation__checkbox:checked ~ .navigation__nav {
47
transform: translateX(0);
48
visibility: initial;
49
opacity: 1;
50
}
51
.navigation__button {
52
position: fixed;
53
top: 6rem;
54
right: 6rem;
55
width: 7rem;
56
height: 7rem;
57
background-color: #fff;
58
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
59
cursor: pointer;
60
z-index: 12;
61
text-align: center;
62
}
63
.navigation__back {
64
position: fixed;
65
bottom: 6rem;
66
right: 6rem;
67
width: 7rem;
68
height: 7rem;
69
border-radius: 50%;
70
background-color: #fff;
71
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
72
cursor: pointer;
73
z-index: 2;
74
text-align: center;
75
}
76
.navigation__before {
77
position: fixed;
78
bottom: 6rem;
79
left: 6rem;
80
width: 7rem;
81
height: 7rem;
82
border-radius: 50%;
83
background-color: #fff;
84
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
85
cursor: pointer;
86
z-index: 2;
87
text-align: center;
88
}
89
.navigation__background {
90
position: fixed;
91
top: 6.5rem;
92
right: 6.5rem;
93
width: 6rem;
94
height: 6rem;
95
border-radius: 50%;
96
background: black;
97
z-index: 10;
98
}
99
.navigation__nav {
100
position: fixed;
101
top: 0;
102
left: 0;
103
height: 100vh;
104
width: 100%;
105
z-index: 11;
106
transform: translateX(-50%);
107
visibility: hidden;
108
opacity: 0;
109
transition: opacity 0.8s ease-in, transform 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55);
110
}
111
.navigation__list {
112
position: absolute;
113
top: 50%;
114
left: 50%;
115
transform: translate(-50%, -50%);
116
text-align: center;
117
list-style: none;
118
}
119
.navigation__link:link, .navigation__link:visited {
120
display: inline-block;
121
font-size: 3rem;
122
font-weight: 300;
123
padding: 1rem 2rem;
124
color: #fff;
125
text-decoration: none;
126
text-transform: uppercase;
127
background-size: 227%;
128
transition: all 0.4s;
129
}
130
.navigation__link:hover, .navigation__link:active {
131
color: #ddd;
132
}
133
.navigation__icon {
134
display: inline-block;
135
position: relative;
136
width: 3rem;
137
height: 0.5rem;
138
top: 28%;
139
transition: transform 0.25s ease-in-out;
140
}
141
.navigation__icon-span {
142
position: absolute;
143
height: 0.5rem;
144
width: 50%;
145
background: #777;
146
transition: all 0.5s ease-in;
147
}
148
.navigation__icon-span:nth-child(even) {
149
left: 50%;
150
border-radius: 0 0.9rem 0.9rem 0;
151
}
152
.navigation__icon-span:nth-child(odd) {
153
left: 0;
154
border-radius: 0.9rem 0 0 0.9rem;
155
}
156
.navigation__icon-span:nth-child(1), .navigation__icon-span:nth-child(2) {
157
transform: translateY(-1rem);
158
}
159
.navigation__icon-span:nth-child(5), .navigation__icon-span:nth-child(6) {
160
transform: translateY(1rem);
161
}
162
.navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(1), .navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(6) {
163
transform: rotate(45deg);
164
}
165
.navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(2), .navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(5) {
166
transform: rotate(-45deg);
167
}
168
.navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(2), .navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(6) {
169
transform-origin: left;
170
}
171
.navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(1), .navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(5) {
172
transform-origin: right;
173
}
174
.navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(3) {
175
left: -50%;
176
opacity: 0;
177
}
178
.navigation__checkbox:checked + .navigation__button .navigation__icon-span:nth-child(4) {
179
left: 100%;
180
opacity: 0;
181
}
182
.navigation__button:hover .navigation__icon-span:nth-child(3) {
183
transform: translateX(-0.3rem) scale(1.1);
184
}
185
.navigation__button:hover .navigation__icon-span:nth-child(4) {
186
transform: translateX(0.3rem) scale(1.1);
187
}
188
.navigation__checkbox:checked + .navigation__button:hover .navigation__icon {
189
transform: rotate(180deg);
190
}
191
.header {
192
background-image: black;
193
height: 95vh;
194
position: relative;
195
}
196
.header__text-box {
197
position: absolute;
198
top: 40%;
199
left: 50%;
200
transform: translate(-50%, -50%);
201
text-align: center;
202
}
203
section {
204
height: 100vh;
205
padding: 40vh;
206
color: #fff;
207
}
208
section h2 {
209
font-size: 3em;
210
}
211
section:nth-child(1) {
212
background: darkred;
213
}
214
section:nth-child(2) {
215
background: darkgrey;
216
}
217
section:nth-child(3) {
218
background: darkred;
219
}
JavaScript
1
20
20
1
<div class="navigation">
2
<input type="checkbox" id="navi-toggle" class="navigation__checkbox" />
3
<label for="navi-toggle" class="navigation__button"> <span class="navigation__icon"> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> </span> </label>
4
<div class="navigation__background"> </div>
5
<nav class="navigation__nav">
6
<ul class="navigation__list">
7
<li class="navigation__item"> <a href="#box1" class="navigation__link">Navigation</a> </li>
8
<li class="navigation__item"> <a href="#box2" class="navigation__link">Navigation</a> </li>
9
</ul>
10
</nav>
11
</div>
12
<section id="box1" class="header">
13
<div class="header__text-box">
14
<h1 class="heading-primary"> <span class="heading-primary--main">Heading</span> <span class="heading-primary--sub">Lorem ipsum</span> </h1>
15
</div>
16
</section>
17
<section id="box2" >
18
<h2 class="animation-element slide-left">Section</h2>
19
<p class="animation-element slide-left">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
20
</section>
Advertisement
Answer
You cant’t without using javascript since you don’t click on the input
JavaScript
1
6
1
let checkbox = document.getElementById("navi-toggle");
2
const navItems = document.querySelectorAll(".navigation__item");
3
4
navItems.forEach(e => e.addEventListener('click', function() {
5
checkbox.checked = false
6
}))
JavaScript
1
266
266
1
html {
2
font-size: 62.5%;
3
}
4
5
h2,
6
.h2 {
7
text-transform: uppercase;
8
}
9
10
body {
11
box-sizing: border-box;
12
font-family: 'Lato', sans-serif;
13
font-weight: 400;
14
font-size: 16px;
15
line-height: 1.7;
16
color: #777;
17
padding: 3rem;
18
}
19
20
.heading-primary {
21
color: #fff;
22
text-transform: uppercase;
23
backface-visibility: hidden;
24
margin-bottom: 6rem;
25
}
26
27
.heading-primary--main {
28
display: block;
29
font-size: 6rem;
30
font-weight: 400;
31
letter-spacing: 3.5rem;
32
margin-right: -3.5rem;
33
animation-name: moveInLeft;
34
animation-duration: 1s;
35
animation-timing-function: ease-out;
36
}
37
38
.heading-primary--sub {
39
display: block;
40
font-size: 2rem;
41
font-weight: 700;
42
letter-spacing: 1.75rem;
43
margin-right: -1.75rem;
44
animation: moveInRight 1s ease-out;
45
}
46
47
.navigation__checkbox {
48
display: none;
49
}
50
51
.navigation__checkbox:checked~.navigation__background {
52
transform: scale(100);
53
}
54
55
.navigation__checkbox:checked~.navigation__nav {
56
transform: translateX(0);
57
visibility: initial;
58
opacity: 1;
59
}
60
61
.navigation__button {
62
position: fixed;
63
top: 6rem;
64
right: 6rem;
65
width: 7rem;
66
height: 7rem;
67
background-color: #fff;
68
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
69
cursor: pointer;
70
z-index: 12;
71
text-align: center;
72
}
73
74
.navigation__back {
75
position: fixed;
76
bottom: 6rem;
77
right: 6rem;
78
width: 7rem;
79
height: 7rem;
80
border-radius: 50%;
81
background-color: #fff;
82
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
83
cursor: pointer;
84
z-index: 2;
85
text-align: center;
86
}
87
88
.navigation__before {
89
position: fixed;
90
bottom: 6rem;
91
left: 6rem;
92
width: 7rem;
93
height: 7rem;
94
border-radius: 50%;
95
background-color: #fff;
96
box-shadow: 0 1rem 3rem rgba(0, 0, 0, 0.2);
97
cursor: pointer;
98
z-index: 2;
99
text-align: center;
100
}
101
102
.navigation__background {
103
position: fixed;
104
top: 6.5rem;
105
right: 6.5rem;
106
width: 6rem;
107
height: 6rem;
108
border-radius: 50%;
109
background: black;
110
z-index: 10;
111
}
112
113
.navigation__nav {
114
position: fixed;
115
top: 0;
116
left: 0;
117
height: 100vh;
118
width: 100%;
119
z-index: 11;
120
transform: translateX(-50%);
121
visibility: hidden;
122
opacity: 0;
123
transition: opacity 0.8s ease-in, transform 0.8s cubic-bezier(0.68, -0.55, 0.265, 1.55);
124
}
125
126
.navigation__list {
127
position: absolute;
128
top: 50%;
129
left: 50%;
130
transform: translate(-50%, -50%);
131
text-align: center;
132
list-style: none;
133
}
134
135
.navigation__link:link,
136
.navigation__link:visited {
137
display: inline-block;
138
font-size: 3rem;
139
font-weight: 300;
140
padding: 1rem 2rem;
141
color: #fff;
142
text-decoration: none;
143
text-transform: uppercase;
144
background-size: 227%;
145
transition: all 0.4s;
146
}
147
148
.navigation__link:hover,
149
.navigation__link:active {
150
color: #ddd;
151
}
152
153
.navigation__icon {
154
display: inline-block;
155
position: relative;
156
width: 3rem;
157
height: 0.5rem;
158
top: 28%;
159
transition: transform 0.25s ease-in-out;
160
}
161
162
.navigation__icon-span {
163
position: absolute;
164
height: 0.5rem;
165
width: 50%;
166
background: #777;
167
transition: all 0.5s ease-in;
168
}
169
170
.navigation__icon-span:nth-child(even) {
171
left: 50%;
172
border-radius: 0 0.9rem 0.9rem 0;
173
}
174
175
.navigation__icon-span:nth-child(odd) {
176
left: 0;
177
border-radius: 0.9rem 0 0 0.9rem;
178
}
179
180
.navigation__icon-span:nth-child(1),
181
.navigation__icon-span:nth-child(2) {
182
transform: translateY(-1rem);
183
}
184
185
.navigation__icon-span:nth-child(5),
186
.navigation__icon-span:nth-child(6) {
187
transform: translateY(1rem);
188
}
189
190
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(1),
191
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(6) {
192
transform: rotate(45deg);
193
}
194
195
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(2),
196
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(5) {
197
transform: rotate(-45deg);
198
}
199
200
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(2),
201
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(6) {
202
transform-origin: left;
203
}
204
205
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(1),
206
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(5) {
207
transform-origin: right;
208
}
209
210
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(3) {
211
left: -50%;
212
opacity: 0;
213
}
214
215
.navigation__checkbox:checked+.navigation__button .navigation__icon-span:nth-child(4) {
216
left: 100%;
217
opacity: 0;
218
}
219
220
.navigation__button:hover .navigation__icon-span:nth-child(3) {
221
transform: translateX(-0.3rem) scale(1.1);
222
}
223
224
.navigation__button:hover .navigation__icon-span:nth-child(4) {
225
transform: translateX(0.3rem) scale(1.1);
226
}
227
228
.navigation__checkbox:checked+.navigation__button:hover .navigation__icon {
229
transform: rotate(180deg);
230
}
231
232
.header {
233
background-image: black;
234
height: 95vh;
235
position: relative;
236
}
237
238
.header__text-box {
239
position: absolute;
240
top: 40%;
241
left: 50%;
242
transform: translate(-50%, -50%);
243
text-align: center;
244
}
245
246
section {
247
height: 100vh;
248
padding: 40vh;
249
color: #fff;
250
}
251
252
section h2 {
253
font-size: 3em;
254
}
255
256
section:nth-child(1) {
257
background: darkred;
258
}
259
260
section:nth-child(2) {
261
background: darkgrey;
262
}
263
264
section:nth-child(3) {
265
background: darkred;
266
}
JavaScript
1
22
22
1
<div class="navigation">
2
<input type="checkbox" id="navi-toggle" class="navigation__checkbox" />
3
<label for="navi-toggle" class="navigation__button"> <span class="navigation__icon"> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> <span class="navigation__icon-span"> </span> </span> </label>
4
<div class="navigation__background"> </div>
5
<nav class="navigation__nav">
6
<ul class="navigation__list">
7
<li class="navigation__item"> <a href="#box1" class="navigation__link">Navigation</a> </li>
8
<li class="navigation__item"> <a href="#box2" class="navigation__link">Navigation</a> </li>
9
</ul>
10
</nav>
11
</div>
12
<section id="box1" class="header">
13
<div class="header__text-box">
14
<h1 class="heading-primary"> <span class="heading-primary--main">Heading</span> <span class="heading-primary--sub">Lorem ipsum</span> </h1>
15
</div>
16
</section>
17
<section id="box2">
18
<h2 class="animation-element slide-left">Section</h2>
19
<p class="animation-element slide-left">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata
20
sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum.
21
Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. </p>
22
</section>