Преобразование значка гамбургера / меню в значок креста / закрытия с использованием только CSS
Transition hamburger / menu icon into cross /close icon using only CSS
menu_x.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> <link rel="stylesheet" href="/menu_x.css"> </head> <body> <div id="button"> <div id="icon"> <span class="lines"></span> </div> </div> <script src="/menu_x.js"></script> </body> </html>
menu_x.css
#button { position: relative; width: 4rem; height: 4rem; background: #000; cursor: pointer; } #button:hover { background: rgb(0, 100, 100); } #icon { position: absolute; top: 0; right: 1rem; left: 1rem; width: auto; height: 100%; } /* MENU ICON */ .lines, .lines:before, .lines:after { position: absolute; display: block; width: 100%; left: 0; background: #FFFFFF; transition: 0.3s; } .lines { height: 3px; margin-top: -2px; top: 50%; } .lines:before, .lines:after { content: ''; height: 100%; /* Try different values here: .25rem, .5rem, .2rem, 5rem, 10rem... */ transform-origin: 5rem center; } .lines:before { top: 8px; } .lines:after { top: -8px; } /* CLOSE ICON */ .close { transform: scale3d(0.8, 0.8, 0.8); } .close .lines { background: transparent; } .close .lines:before, .close .lines:after { top: 0; transform-origin: 50% 50%; } .close .lines:before { transform: rotate3d(0, 0, 1, 45deg); } .close .lines:after { transform: rotate3d(0, 0, 1, -45deg); }
menu_x.js
let button = document.getElementById('button'); let icon = document.getElementById('icon'); function menu_click() { icon.classList.toggle('close'); } button.addEventListener("click", menu_click);