I was trying to create an interactive Navigation bar following step by step a tutorial video, I did everything (letter by letter) as it was done in the video, but the part when the green indicator should hover on clicking a button (link) is not working. In the video he did insert JS directly into HTML File, didnĀ“t work so I created a JS file and insert the code there, linked it via script under the body, still not working. Any suggestions what may be the problem?
Link to the video: https://www.youtube.com/watch?v=ArTVfdHOB-M
Thank you for all answers!
const list = document.querySelectorAll(".list");
function activeLink() {
list.forEach((item) => item.classList.remove("active"));
this.classList.add("active");
}
list.forEach((item) => item.addEventListener("click", activeLink));
@import url("https://fonts.googleapis.com/css2?family=DM+Sans&display=swap");
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "DM Sans", sans-serif;
}
:root {
--clr: #222327;
}
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: var(--clr);
}
.navigation {
width: 400px;
height: 70px;
background: #fff;
position: relative;
display: flex;
justify-content: center;
align-items: center;
border-radius: 10px;
}
.navigation ul {
display: flex;
width: 350px;
}
.navigation ul li {
position: relative;
list-style: none;
width: 70px;
height: 70px;
z-index: 1;
}
.navigation ul li a {
position: relative;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 100%;
text-align: center;
font-weight: 600;
}
.navigation ul li a .icon {
position: relative;
display: block;
line-height: 75px;
font-size: 1.5em;
text-align: center;
transition: 0.5s;
color: var(--clr);
}
.navigation ul li.active a .icon {
transform: translateY(-32px);
}
.navigation ul li a .text {
position: absolute;
color: var(--clr);
font-weight: 600;
font-size: 0.75em;
letter-spacing: 0.05em;
transition: 0.5s;
opacity: 0;
transform: translateY(20px);
}
.navigation ul li.active a .text {
opacity: 1;
transform: translateY(10px);
}
.indicator {
position: absolute;
top: -50%;
width: 70px;
height: 70px;
background: #29fd53;
border-radius: 50%;
border: 6px solid var(--clr);
transition: 0.5s;
}
.indicator::before {
content: "";
position: absolute;
top: 50%;
left: -22px;
width: 20px;
height: 20px;
background: transparent;
border-top-right-radius: 20px;
box-shadow: 1px -10px 0 0 var(--clr);
}
.indicator::after {
content: "";
position: absolute;
top: 50%;
right: -22px;
width: 20px;
height: 20px;
background: transparent;
border-top-left-radius: 20px;
box-shadow: -1px -10px 0 0 var(--clr);
}
.navigation ul li:nth-child(1).active .indicator {
transform: translateX(calc(70px * 0));
}
.navigation ul li:nth-child(2).active .indicator {
transform: translateX(calc(70px * 1));
}
.navigation ul li:nth-child(3).active .indicator {
transform: translateX(calc(70px * 2));
}
.navigation ul li:nth-child(4).active .indicator {
transform: translateX(calc(70px * 3));
}
.navigation ul li:nth-child(5).active .indicator {
transform: translateX(calc(70px * 4));
}
<!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" />
<link rel="stylesheet" href="styles.css" />
<title>Interactive Navbar</title>
</head>
<body>
<div class="navigation">
<ul>
<li class="list active">
<a href="#">
<span class="icon"><ion-icon name="home-outline"></ion-icon></span>
<span class="text">Home</span>
</a>
</li>
<li class="list">
<a href="#">
<span class="icon"
><ion-icon name="person-outline"></ion-icon
></span>
<span class="text">Profile</span>
</a>
</li>
<li class="list">
<a href="#">
<span class="icon"
><ion-icon name="chatbubble-outline"></ion-icon
></span>
<span class="text">Message</span>
</a>
</li>
<li class="list">
<a href="#">
<span class="icon"
><ion-icon name="camera-outline"></ion-icon
></span>
<span class="text">Photos</span>
</a>
</li>
<li class="list">
<a href="#">
<span class="icon"
><ion-icon name="settings-outline"></ion-icon
></span>
<span class="text">Setting</span>
</a>
</li>
<div class="indicator"></div>
</ul>
</div>
<script type="module" src="indicator.js"></script>
<script
type="module"
src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.esm.js"
></script>
<script
nomodule
src="https://unpkg.com/[email protected]/dist/ionicons/ionicons.js"
></script>
</body>
</html>
change from
.navigation ul li:nth-child(1).active .indicator
to.navigation ul li:nth-child(1).active ~ .indicator
for all children.Notice
~
symbol. To understand>
,+
&~
read this article.