<!-- Load an icon library for example-->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> in the head section
CODE SUMMARY FOR USING AN ICON LIBRARY
<head>
<!-- Loading an icon library starts here -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/>
<!-- Loading an icon library ends here -->
</head>
METHOD 2 - BY USING CSS TO CREATE A BASIC MENU ICON
Step 1 - Add HTML:
<div></div>
<div></div>
<div></div>
Step 2 - Ad CSS
div {
width: 35px; /* Specifies the width of each bar */
height: 5px; /* Specifies the height of each bar */
background-color: black; /* Background colour for each bar */
margin: 6px 0;} /* Create some distance between each bar with a top and bottom margin */
CODE SUMMARY FOR BASIC MENU ICON:
<head>
<!-- Creating a menu icon for head section starts here -->
<style>
div {
width: 35px;
height: 5px;
background-color: black;
margin: 6px 0;
}
</style>
<!-- Creating a menu icon for head section ends here -->
</head>
<body>
<!-- Creating a menu icon for body section starts here -->
<div></div>
<div></div>
<div></div>
<!-- Creating a menu icon for body section ends here -->
</body>
METHOD 2 - BY USING CSS AND JAVASCRIPT TO CREATE AN ANIMATED MENU ICON BY CHANGING THE MENU ICON TO A "CANCEL/REMOVE" ICON WHEN IT IS CLICKED ON
<div class="container" onclick="myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
Step 2 - Add CSS
.container {display: inline-block;
cursor: pointer;}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;}
/* Rotate first bar */
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px) ;
transform: rotate(-45deg) translate(-9px, 6px) ;}
/* Fade out the second bar */
.change .bar2 {
opacity: 0;}
/* Rotate last bar */
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px) ;
transform: rotate(45deg) translate(-8px, -8px) ;}
Step 3 - Add Java Script
function myFunction(x) {
x.classList.toggle("change");
}
We use the same HTML and CSS styles as before, only this time, we wrap a container element around each <div> element and we specify a class name for each.
The container element is used to show a pointer symbol when the user moves the mouse over the divs (bars). When it is clicked on, it will execute a JavaScript function that adds a new class name to it, which will change the styles of each horizontal bar: the first and the last bar is transformed and rotated to the letter "x". The bar in the middle fades out and becomes invisible.
CODE SUMMARY FOR ANIMATED MENU ICON:
<head>
<!-- Creating an animated menu icon for head section starts here -->
<style>
.container {
display: inline-block;
cursor: pointer;
}
.bar1, .bar2, .bar3 {
width: 35px;
height: 5px;
background-color: #333;
margin: 6px 0;
transition: 0.4s;
}
.change .bar1 {
-webkit-transform: rotate(-45deg) translate(-9px, 6px);
transform: rotate(-45deg) translate(-9px, 6px);
}
.change .bar2 {opacity: 0;}
.change .bar3 {
-webkit-transform: rotate(45deg) translate(-8px, -8px);
transform: rotate(45deg) translate(-8px, -8px);
}
</style>
<!-- Creating an animated menu icon for head section ends here -->
</head>
<body>
<!-- Creating an animated menu icon for body section starts here -->
<div class="container" onclick="myFunction(this)">
<div class="bar1"></div>
<div class="bar2"></div>
<div class="bar3"></div>
</div>
<script>
function myFunction(x) {
x.classList.toggle("change");
}
</script>
<!-- Creating an animated menu icon for body section ends here -->
</body>
No comments:
Post a Comment