0 / 0 / 0
Регистрация: 02.10.2019
Сообщений: 4
|
|
|
|
JavaScript active кнопки
13.12.2021, 17:22. Показов 550. Ответов 0
Моя задача сделать так чтобы при нажатии на одну из кнопок ей присваивался класс 'active', но у другой кнопки если этот класс убирался но, еще надо чтобы у нажатой кнопки убирался и добавлялся класс 'active' и как раз скомбинировать эти вещи у меня не получается, как всё это сделать заранее спасибо
| HTML5 | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
| <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.7.0/animate.min.css">
<link rel="stylesheet" href="css/style.min.css">
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=PT+Sans:wght@400;700&display=swap" rel="stylesheet">
</head>
<body>
<div class="wrapper">
<header class="header">
<div class="header__wrapper">
<div class="header__container _container">
<div class="header__body">
<div class="header__main">
<a href="#" class="header__logo">Funiro.</a>
<div class="header__menu menu">
<nav class="menu__body">
<ul class="menu__list" data-spollers="768, max">
<li class="menu__item">
<a href="" class="menu__link">Products</a>
<button class="menu__arrow _icon-linedown" data-spoller type="button"></button>
<ul class="menu__sub-list">
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_1</a>
</li>
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_2</a>
</li>
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_3</a>
</li>
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_4</a>
</li>
</ul>
</li>
<li class="menu__item">
<a href="" class="menu__link">Rooms</a>
<button class="menu__arrow _icon-linedown" data-spoller type="button"></button>
<ul class="menu__sub-list">
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_1</a>
</li>
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_2</a>
</li>
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_3</a>
</li>
<li class="menu__sub-item">
<a href="" class="menu__sub-linK">I PRO_4</a>
</li>
</ul>
</li>
<li class="menu__item">
<a href="" class="menu__link">Inspirations</a></li>
</ul>
</nav>
</div>
</div>
<div class="header__search">
<div class="search__form">
<button class="search__form-icon _icon-search" type="button"></button>
<form action="#" class="search__form-item">
<button class="search__form-btn _icon-search" type="submit"></button>
<input type="text" autocomplete="off" class="search__form-input" placeholder="Search for minimalist chair">
</form>
</div>
</div>
<div class="header__actions actions__header">
<a href="#" class="actions__header-item actions__header-like _icon-like"></a>
<div class="actions__header-item actions__header-basket">
<a href="#" class="actions__header-basket _icon-basket"></a>
<div class="actions__header-basket__body">
<ul class="actions__header-basket cart-list"></ul>
</div>
</div>
<a href="" class="actions__header-item actions__header-user ibg">
<img src="https://www.cyberforum.ru/images/header/avatar.jpg" alt="Avatar">
</a>
</div>
<div class="header__burger menu__burger">
<span></span>
</div>
</div>
</div>
</div>
</header>
<main class="main">
</main>
<footer class="footer">
</footer>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="js/libs.min.js"></script>
<script src="js/main.js"></script>
</body>
</html> |
|
| CSS | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
| @import "import";
.header {
position: absolute;
top: 0;
left: 0;
width: 100%;
&__wrapper {
position: fixed;
width: 100%;
left: 0;
top: 0;
z-index: 50;
background-color: $header-color;
position: relative;
@media (max-width: $md3){
&::before{
content: '';
position: absolute;
width: 100%;
height: 50px;
background-color: #FCF8F3;
z-index: 3;
}
}
}
&__body {
display: flex;
padding: 5px 0;
min-height: 125px;
align-items: center;
position: relative;
@media (max-width: $md2){
min-height: 80px;
}
@media (max-width: $md3){
min-height: 50px;
}
}
&__main {
display: flex;
flex: 0 0 494/1240 * 100%;
align-items: center;
@media (max-width: $md2){
flex: 1 0 auto;
}
}
&__logo {
@extend %Gilroy-bold;
font-size: 24px;
line-height: 29 / 24 * 100%;
color: #000;
z-index: 50;
}
&__search {
@media (min-width: $md2){
flex: 1 1 auto;
}
}
&__actions {
}
}
._icon- {
&linedown {
}
&search {
}
&like {
}
&basket {
}
}
.menu {
z-index: 4;
flex: 0 1 420px;
&__body {
@media (max-width: $md3){
position: fixed;
top: 0;
left: -100%;
width: 100%;
overflow: auto;
transition: all .4s ease 0s;
padding: 70px 15px 30px 15px;
height: 100%;
z-index: 2;
}
&._active{
left: 0;
}
}
&__list {
@media (min-width: $md3){
display: flex;
flex-wrap: wrap;
justify-content: space-evenly;
}
}
&__item {
display: flex;
align-items: center;
justify-content: space-between;
flex-wrap: wrap;
margin: 0px 3px;
padding: 5px 0;
position: relative;
transition: all .4s ease 0s;
@media (any-hover:hover ){
&:hover{
.menu__sub-list{
transform: translate(0px, 0px);
opacity: 1;
visibility: visible;
pointer-events: auto;
}
.menu__arrow{
transform: rotate(-180deg);
}
.menu__arrow,
.menu__link{
color:$orange;
}
}
}
}
&__item.active{
.menu__sub-list{
transform: translate(0px, 0px);
opacity: 1;
visibility: visible;
pointer-events: auto;
}
.menu__arrow{
transform: rotate(-180deg);
}
.menu__arrow,
.menu__link{
color:$orange;
}
.menu__sub-list{
@media (max-width: $md3){
width: 100%;
position: relative;
}
}
}
&__item._active{
.menu__sub-list{
transform: translate(0px, 0px);
opacity: 1;
visibility: visible;
pointer-events: auto;
}
.menu__arrow{
transform: rotate(-180deg);
}
.menu__arrow,
.menu__link{
color:$orange;
}
.menu__sub-list{
@media (max-width: $md3){
width: 100%;
position: relative;
}
}
}
&__link {
@extend %Gilroy-medium;
color: $mainCol;
line-height: 150%;
transition: all .3s ease 0s;
@media (max-width: $md3){
font-size: 23px;
}
}
&__arrow {
margin: 2px 0px 0px 8px;
transition: all .5s ease 0s;
font-size: 14px;
background-color: transparent;
@media (max-width: $md3){
font-size: 20px;
}
}
&__sub-list {
position: relative;
background-color: $orange;
padding: 15px;
flex: 1 1 100%;
border-radius: 7px;
@media (max-width: $md3){
position: absolute;
top: 100%;
left: 0;
opacity: 0;
visibility: hidden;
transition: all .9s ease 0s;
pointer-events: none;
}
@media (min-width: $md3){
min-width: 200px;
left: 0;
position: absolute;
top: 100%;
opacity: 0;
visibility: hidden;
transition: all .3s ease 0s;
transform: translate(0px, 10px);
pointer-events: none;
}
}
&__sub-item {
&:not(:last-child){
margin: 0px 0px 5px 0px;
}
text-align-last: left;
@media (max-width: $md3){
&:not(:last-child){
margin: 0px 0px 10px 0px;
}
}
}
&__sub-linK {
color: #fff;
line-height: 120%;
@media (max-width: $md3){
font-size: 18px;
}
}
&__burger {
}
}
.search {
&__form {
max-width: 473px;
padding: 0px 7px 0px 0px;
&._active{
.search__form-item{
opacity: 1;
top: 100%;
visibility: visible;
}
.search__form-icon{
color: $orange;
}
}
}
&__form-btn {
flex: 0 0 45px;
height: 45px;
font-size: 16px;
color: #333333;
background-color: transparent;
}
&__form-icon{
position: relative;
z-index: 5;
font-size: 22px;
color:#262F56;
background-color: transparent;
padding-left: 30px;
padding-right: 30px;
@media (max-width: $md2){
@include adaptive-paddingL (30, 10);
@include adaptive-paddingR (30, 10);
}
@media (min-width: $md2){
display: none;
}
}
&__form-item {
background-color: #fff;
display: flex;
align-items: center;
@media (max-width: $md2){
box-shadow: 0 0 5px rgba(212, 209,205, 0.3);
position: absolute;
width: 100%;
left: 0;
top: -100px;
z-index: 5;
opacity: 0;
visibility: hidden;
transition: all .4s ease 0s;
}
}
&__form-input {
font-size: 14px;
color: #616161;
width: 100%;
padding: 0px 20px 0px 0px;
outline: none;
input:focus::placeholder {
color: transparent;
}
}
}
.actions {
&__header {
position: relative;
z-index: 5;
display: grid;
grid-template-columns: auto;
grid-auto-flow: column;
align-items: center;
}
&__header-item {
margin: 0px 30px 0px 0px;
@media (max-width: $md2){
@include adaptive-marginR(30, 10);
}
}
&__header-like {
font-size: 25px;
color: #262F56;
transition: all .4s ease 0s;
&:hover{
color:$orange;
}
}
&__header-basket {
font-size: 25px;
color: #262F56;
transition: all .4s ease 0s;
display: block;
position: relative;
&:hover{
color:$orange;
}
span{
position: absolute;
width: 20px;
height: 20px;
display: flex;
justify-content: center;
align-items: center;
top: -10px;
right: -10px;
border-radius: 50%;
background-color: $orange;
color:#fff;
font-size: 12px;
}
}
&__header-user {
overflow: hidden;
border-radius: 50%;
width: 40px;
height: 40px;
@media (max-width: $md2){
@include adaptive-width(40,35);
@include adaptive-height(40,35);
@include adaptive-marginR(30, 10);
}
}
}
.cart-list {
}
@import "burger"; |
|
бургер | CSS | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
| .menu__burger{
display: none;
}
@media (max-width: $md3){
.menu{
&__burger{
display: block;
position: relative;
flex: 0 0 30px;
width: 30px;
height: 20px;
z-index: 4;
}
&__burger::before,
&__burger::after {
content: '';
position: absolute;
background-color: #000;
width: 100%;
height: 2px;
transition: transform .4s ease;
}
&__burger::before{
top: 0;
}
&__burger::after{
bottom: 0;
}
&__burger span{
background-color: #000;
position: absolute;
left: 0;
width: 100%;
height: 2px;
top: 9px;
}
&__burger._active::before{
transform: rotate(45deg);
top: 9px;
}
&__burger._active::after{
transform: rotate(-45deg);
bottom: 9px;
}
&__burger._active span{
display: none;
}
}
} |
|
| JavaScript | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
| let menuBurger = document.querySelector('.menu__burger');
let menuList = document.querySelector('.menu__body');
if (menuList) {
menuBurger.addEventListener('click', function(){
menuBurger.classList.toggle('_active');
menuList.classList.toggle('_active');
});
}
function ibg() {
let ibg = document.querySelectorAll(".ibg");
for (var i = 0; i < ibg.length; i++) {
if (ibg[i].querySelector('img')) {
ibg[i].style.backgroundImage = 'url(' + ibg[i].querySelector('img').getAttribute('src') + ')';
}
}
}
let isMobile = {
Android: function (){
return navigator.userAgent.match(/Android/i);
},
BlackBerry: function () {
return navigator.userAgent.match(/BlackBerry/i);
},
iOS: function () {
return navigator.userAgent.match(/iPhone|iPad|iPod/i);
},
Opera: function () {
return navigator.userAgent.match(/Opera Mini/i);
},
Windows: function () {
return navigator.userAgent.match(/IEMobile/i);
},
any: function (){
return(
isMobile.Android() ||
isMobile.BlackBerry() ||
isMobile.iOS() ||
isMobile.Opera() ||
isMobile.Windows());
}
}
if (isMobile.any()) {
document.body.classList.add('_touch');
}else{
document.body.classList.add('_pc');
}
window.onload = function(){
document.addEventListener('click', documentActions);
function documentActions(e){
const targetElement = e.target;
if(window.innerWidth > 768 && isMobile.any()) {
if (targetElement.classList.contains('menu__arrow')){
targetElement.closest('.menu__item').classList.toggle('_active');
}
}
if (targetElement.classList.contains('search__form-icon')){
document.querySelector('.search__form').classList.toggle('_active');
} else if (!targetElement.closest('.search__form') && document.querySelector('.search__form._active')){
document.querySelector('.search__form').classList.remove('_active');
}
if (!isMobile.any()){
let menuItem = document.querySelectorAll('.menu__item');
menuItem.forEach(function (item) {
item.classList.remove('active');
});
}
}
}
ibg();
window.addEventListener('resize', function () {
if (window.innerWidth >=768 ) {
menuBurger.classList.remove('_active');
menuList.classList.remove('_active');
}
let menuItem = document.querySelectorAll('.menu__item');
menuItem.forEach(function (item) {
item.classList.remove('active');
});
});
window.addEventListener('resize', function () {
if (window.innerWidth <= 768) {
let menuItem = document.querySelectorAll('.menu__item');
menuItem.forEach(function clickItem(item) {
item.addEventListener('click', function () {
menuItem.forEach(function (item) {
item.classList.remove('active');
});
item.classList.toggle('active');
});
});
}else{
false
}
}); |
|
0
|