Простое адаптивное горизонтальное меню на CSS3 и jQuery
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
#main-menu ul { list-style: none; margin: 0 auto; padding: 0; display: flex; } @media only screen and (max-width: 40em) { #main-menu ul { flex-direction: column; } } @media only screen and (max-width: 40em) { #main-menu .menu-container { display: none; } } #main-menu li { flex-grow: 1; display: inline-block; position: relative; } #main-menu li > ul { flex-direction: column; visibility: hidden; position: absolute; left: 0; transition: .5s all ease; opacity: 0; transform: translateY(25%); width: 200px; } #main-menu li > ul a { text-align: left; padding: .5rem 1rem; } @media only screen and (max-width: 40em) { #main-menu li > ul a { text-align: right; padding-right: 3rem; background: #e9e9e9; color: #a9a9a9; } } #main-menu a { background: #a9a9a9; display: block; text-align: center; color: #fff; text-decoration: none; transition: .2s all ease; padding: 1rem .5rem; } @media only screen and (max-width: 40em) { #main-menu a { border-bottom: 1px solid #fff; text-align: right; padding: 1rem; } } #main-menu a:hover { background: #696969; color: #fff; } #main-menu #menuTrigger { border: 0; background: #a9a9a9; padding: .5rem 1rem; color: #fff; font-size: 1.5rem; cursor: pointer; background: #696969; display: none; } @media only screen and (max-width: 40em) { #main-menu #menuTrigger { display: block; } } #main-menu .submenu span { content: '\f0d7'; font-family: FontAwesome; padding-left: .5rem; } @media only screen and (max-width: 40em) { #main-menu .submenu span { margin-left: .5rem; border: 1px solid #fff; padding: .25rem .5rem; } } @media only screen and (min-width: 40em) { #main-menu .submenu:hover > a { background: #696969; } } #main-menu li > ul.openDesktopSubMenu { visibility: visible; position: absolute; left: 0; top: 100%; opacity: 1; transform: translateY(0%); } @media only screen and (max-width: 40em) { #main-menu li > ul.openDesktopSubMenu { width: 100%; visibility: visible; position: relative; left: 0; top: 100%; opacity: 1; transform: translateY(0%); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
<nav id="main-menu"> <a href="#" id="menuTrigger"><i class="fa fa-bars" aria-hidden="true"></i></a> <div class="menu-container"> <ul> <li><a href="#">item</a></li> <li><a href="#">item</a></li> <li><a href="#">item</a> <ul> <li><a href="#">item</a></li> <li><a href="#">item</a></li> <li><a href="#">item</a></li> </ul> </li> <li><a href="#">item</a></li> <li><a href="#">item</a> <ul> <li><a href="#">item</a></li> <li><a href="#">item</a></li> <li><a href="#">item</a></li> </ul> </li> <li><a href="#">item</a></li> </ul> </div> </nav> <script> $("li").has("ul").addClass('submenu'); $('.submenu > a').append('<span><i class="fa fa-caret-down" aria-hidden="true"></i></span>'); $('.submenu span').click(function() { $(this).next('#main-menu li > ul').toggleClass('openDesktopSubMenu'); }); $('.submenu a').hover(function() { $(this).next('#main-menu li > ul').addClass('openDesktopSubMenu'); }, function() { $(this).next('#main-menu li > ul').removeClass('openDesktopSubMenu'); }); $('#main-menu li > ul').hover(function() { $(this).addClass('openDesktopSubMenu'); }, function() { $(this).removeClass('openDesktopSubMenu'); }); var x = true; $('#menuTrigger').on("click", function(e) { e.preventDefault(); if (x) { $('.menu-container').stop().slideToggle('fast'); x = false; } else { $('.menu-container').stop().slideToggle('fast', function() { $('.menu-container').css('display', ''); x = true; }) } }); </script> |
Мобильная версия криво работает
Проверил со своего телефона. Яндекс браузер на Android - проблем не увидел. Всё работает