Hello guys in this tutorial we will create advanced animated submenu using HTML CSS & JavaScript
First we need to create two files index.html and style.css then we need to do code for it.
Step:1
Add below code inside index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <title>Animated Submenu</title> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <meta https-equiv="X-UA-Compatible" content="ie=edge" /> <link rel="stylesheet" href="style.css" /> <link href="https://fonts.googleapis.com/css2?family=IBM+Plex+Sans&display=swap" rel="stylesheet"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <header> <div class="container"> <div class="highlighter"style=""></div> <nav class="nav"> <ul class="nav_menu"> <li class="nav_item"><a href="#" class="nav_link nav_active-link">Home</a></li> <li class="nav_item"> <a href="#" class="nav_link">About</a> <ul class="nav_submenu"> <li class="nav_submenu-item"><a href="#" class="nav_submenu-link">Who We Are</a></li> <li class="nav_submenu-item"><a href="#" class="nav_submenu-link">Training</a></li> </ul> </li> <li class="nav_item"> <a href="#" class="nav_link">Services</a> <ul class="nav_submenu"> <li class="nav_submenu-item"><a href="#" class="nav_submenu-link">Service 01</a></li> <li class="nav_submenu-item"><a href="#" class="nav_submenu-link">Service 02</a></li> <li class="nav_submenu-item"><a href="#" class="nav_submenu-link">Service 03</a></li> <li class="nav_submenu-item"><a href="#" class="nav_submenu-link">Service 04</a></li> </ul> </li> <li class="nav_item nav_item--team"> <a href="#" class="nav_link">Career</a> <div class="nav_submenu"> <p class="paragraph"> Lorem ipsum dolor sit amet consectetur, adipisicing elit. Illo, quas is iste magnam, vitae recusandae quaerat inventore repudiandae. </p> </div> </li> </ul> </nav> </div> </header> <script> const highlighter = document.querySelector('.highlighter'); const navItems = document.querySelectorAll('.nav_item'); navItems.forEach((cur, index) => { cur.addEventListener('mouseenter', handleEnter); cur.addEventListener('mouseleave', handleLeave); }); function handleEnter() { var subMenuCords = this.querySelector('.nav_submenu').getBoundingClientRect(); highlighter.classList.add('open'); var allCords = { left: subMenuCords.left, top: subMenuCords.top, height: subMenuCords.height, width: subMenuCords.width, } highlighter.style.width = `${allCords.width}px`; highlighter.style.height = `${allCords.height}px`; highlighter.style.left = `${allCords.left}px`; highlighter.style.top = `${allCords.top}px`; } function handleLeave() { highlighter.classList.remove('open'); } </script> </body> </html>
Step:2
Then we need to add code for style.css which code i provide in below screen.
* { padding: 0; margin: 0; font-family: 'IBM Plex Sans', sans-serif; } html, body { height: 100%; } body { background: #4b00ff; } .container { width: 95% max-width: 1240px; margin: 0 auto; } .nav_menu { display: flex; list-style: none; padding: 40px; justify-content: center; } .nav_item { position: relative; display: flex; align-items: center; height: 30px; } .nav_item:hover > a { color: #fff; } .nav_item:hover .nav_submenu { visibility: visible; opacity: 1; } .nav_link { font-size: 18px; text-decoration: none; padding: 10px 15px; color: #a9a9a9; transition: .3s color; } .nav_link.nav_active-link { color: #fff; } .nav_submenu { list-style: none; width: 230px; padding: 10px; position: absolute; left: 0; top: 125%; visibility: hidden; opacity: .7; transition: .35s opacity; } .nav_submenu-link { display: block; font-size: 15px; padding: 5px; color: #4b00ff; text-decoration: none; transition: .4s; font-weight: 500; } .nav_submenu-link:hover { font-weight: 800; } .highlighter { background: #fff; position: absolute; z-index: -1; opacity: 0; transition: .3s all ease-in; border-radius: 4px; width: 250px; height: 85px; left: 615px; top: 75px; } .highlighter:before { content: ""; width: 0; height: 0; border-left: 10px solid transparent; border-right: 10px solid transparent; border-bottom: 10px solid #ffffff; display: block; position: absolute; top: -10px; left: 18px; } .paragraph { padding: 5px; font-weight: 500; line-height: 1.6; color: #4b00ff; } .open { opacity: 1; } .nav_item--team .nav_submenu { width: 270px; }