20
loading...
This website collects cookies to deliver better user experience
toggle
a menu or navigation etc. HTML
,CSS
and JavaScript
. OK let's start then.index.html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="style.css" rel="stylesheet"/>
</head>
<body>
<div class="hum_toggle" id="hum-toggle" onclick="togglehum()">
<span></span>
<span></span>
</div>
<script src="main.js" type="text/javascript"></script>
</body>
</html>
index.html
,I have created two spans tags within an outer div which class name is hum_toggle
. The onclick
event is used to trigger a function when an element is clicked on. When you click the .hum_toggle
div element, togglehum()
function will be triggered. The togglehum()
function has declared in main.js
.style.css
.hum_toggle
{
position: relative;
width: 50px;
height: 50px;
cursor: pointer;
border: solid black 2px;
border-radius: 50%;
}
.hum_toggle span
{
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%,-50%);
width: 60%;
height: 4px;
transition: all 300ms ease-in-out;
border-radius: 5px;
background: black;
}
.hum_toggle span:nth-child(1)
{
top: 40%;
}
.hum_toggle span:nth-child(2)
{
top: 60%;
}
.hum_toggle.active span:nth-child(1)
{
top: 50%;
transform: translate(-50%,-50%) rotate(45deg);
}
.hum_toggle.active span:nth-child(2)
{
top: 50%;
transform: translate(-50%,-50%) rotate(-45deg);
}
span:nth-child(1)
and span:nth-child(2)
child selectors are working.main.js
function togglehum(){
var hum = document.getElementById('hum-toggle');
hum.classList.toggle('active');
}
togglehum
function is triggered, the active
class adds to the hum_toggle
class.You can check it by using developer tools.Elements
tab