-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimated-toggler.html
74 lines (71 loc) · 2.31 KB
/
animated-toggler.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Moving Button</title>
<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=Rubik&display=swap" rel="stylesheet">
<style>
#toggler{
border-radius: 100px;
padding: .75rem .5rem;
background-color:#eee;
font-family: Rubik;
display:inline-block;
position: relative;
}
#toggler a {
font-size:1rem;
color:#9698A0;
border-radius: 4rem;
padding: .5rem 1rem;
line-height:1.375rem;
text-decoration:none;
cursor: pointer;
transition: 0.25s color;
position: relative;
z-index: 1;
}
#toggler a.active{
color:#111;
}
#toggler .indicator {
position: absolute;
height: 34px;
width: 128px;
background-color: #fff;
top: 6px;
left: 6px;
z-index: 0;
border-radius: 17px;
transition: 0.25s all;
}
</style>
</head>
<body>
<div id="toggler">
<a class="active" id="toggler-btn--1">Professional</a>
<a id="toggler-btn--2">Intermediate</a>
<a id="toggler-btn--3">Beginners</a>
<span class="indicator"></span>
</div>
<script>
const toggler = document.querySelector('#toggler');
const indicator = toggler.querySelector('.indicator');
const togglerButtons = toggler.querySelectorAll('a');
togglerButtons.forEach(button => {
button.addEventListener('click', function(){
const btnWidth = this.offsetWidth;
const btnOffsetLeft = this.offsetLeft;
indicator.style.width = btnWidth + 'px';
indicator.style.left = btnOffsetLeft + 'px';
toggler.querySelector('.active').classList.remove('active');
this.classList.add('active');
});
})
</script>
</body>
</html>