Welcome to my website U-GINE MEDIA. In this website I teach tutorials and share source code on some programming (HTML, CSS & JavaScript) tutorials.
Before we get started, do well to subscribe to my channel to never miss out on any update that I post every single day.
In this tutorial topic "Creating Online Tutorial Magic CSS Menu Indicator. Did It Work?", we will learn how to create it following these simple easy steps.
Share this to your programmer friends. If y'all learnt something today, make sure to let me in the comments.
If interested in watching the video before running the code below, you can click the play button to get started.
Code Begins
- First, create a folder with any name you like. Then, make the necessary files inside it.
- Create a file called index.html to serve as the main file.
- Create a file called style.css for the CSS code.
- Create a file called script.js for the JavaScript code.
- Finally, go to Font Awesome and embed their cdnjs library link to your main file
To start, add the following HTML codes to your index.html file. These codes include essential HTML markup with different semantic tags, such as div, form, input, button, image, etc., to build the website layout.
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" href="font-awesome.css">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Magic CSS Menu Indicator</title>
</head>
<body>
<div class="container">
<ul>
<li class="active"><a href="#">
<span class="icon"><i class="fa fa-home fa-lg"></i></span>
<span class="text">Home</span>
</a></li>
<li><a href="#">
<span class="icon"><i class="fa fa-user fa-lg"></i></span>
<span class="text">User</span>
</a></li>
<li><a href="#">
<span class="icon"><i class="fa fa-circle fa-lg"></i></span>
<span class="text">Circle</span>
</a></li>
<li><a href="#">
<span class="icon"><i class="fa fa-cog fa-lg"></i></span>
<span class="text">Settings</span>
</a></li>
<li><a href="#">
<span class="icon"><i class="fa fa-comment fa-lg"></i></span>
<span class="text">Message</span>
</a></li>
<div class="indicator"></div>
</ul>
</div>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: "Roboto", sans-serif;
}
:root {
--c: #222222;
}
body {
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
background: var(--c);
}
.container {
position: relative;
width: 400px;
height: 70px;
background: #fff;
display: flex;
justify-content: center;
align-items: center;
border-radius: 10px;
}
.container ul {
display: flex;
width: 350px;
}
.container ul li {
position: relative;
list-style: none;
width: 70px;
height: 70px;
z-index: 1;
}
.container ul li a {
position: relative;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
width: 100%;
text-align: center;
font-weight: 500;
}
.container ul li a .icon {
position: relative;
display: block;
line-height: 75px;
font-size: 1.5em;
text-align: center;
transition: 0.5s;
color: var(--c);
}
/*
.container ul li.active a .icon {
transform: translateY(-35px);
} */
.container ul li a .text {
position: absolute;
color: var(--c);
font-weight: 500;
font-size: 0.95em;
letter-spacing: 0.05em;
transition: 0.5s;
opacity: 0;
/* transform: translateY(240px); */
}
.container ul li.active a .text {
opacity: 1;
transform: translateY(15px);
color: #0f0;
border-radius: 10px;
width: auto;
padding: 2px;
}
.container ul li.active a .icon {
transform: translateY(-37px);
}
.indicator {
position: absolute;
top: -50%;
width: 70px;
height: 70px;
background: #0f0;
border-radius: 50%;
border: 6px solid var(--c);
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: 0px -10px 0 0 var(--c);
}
.indicator::after {
content: "";
position: absolute;
top: 50%;
right: -22px;
width: 20px;
height: 20px;
background: transparent;
border-top-left-radius: 20px;
box-shadow: 0px -10px 0 0 var(--c);
}
Finally, add the following JavaScript code to your script.js file to make your work functional. This code handles various functions, even listeners, input handling, etc.
<script>
const indicator = document.querySelector(".indicator");
const container = document.querySelector(".container");
const lis = container.querySelectorAll("ul li");
lis.forEach(li => li.addEventListener("click", e => {
indicator.style.left = li.offsetLeft + "px";
indicator.style.width = li.offsetWidth + "px";
document.querySelector(".active").classList.remove("active");
li.classList.add("active")
}))
</script>
0 Comments
We are happy to hear from you.