I have crafted a pure css toggle menu but noticed it does not work on iOS safari, so after much frustration have decided to make it toggle with Javascript instead. Only problem is I barely have any experience using Javascript, have looked for how-to's online but have had much difficulty getting any of them to work, and i think what i have here is fairly simple as there are no sub menus.
Was wondering if anybody knew an easy way to get this menu to work with Javascript so that the toggle on/off happens with the click of a mouse and also works on most mobile browsers.
The current menu on codepen is here:
http://codepen.io/lemmiwink5/pen/JoKmJb
HTML
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.1.0/css/font-awesome.min.css" rel="stylesheet">
<header class="menu">
<div tabindex="0" class="drop-menu">
<i class="fa fa-bars" ></i>
<ul class="drop-menu-content">
<i class="fa fa-sort-asc"></i>
<li><a href="#">Home</a></li>
<li><a href="#">Lorem</a></li>
<li><a href="#">Ipsum</a></li>
<li><a href="#">Check</a></li>
</ul>
</div>
</header>
CSS
.menu {
height: 40px;
width: 40px;
margin-left: 20px;
margin-top: 17px;
position: fixed;
z-index: 1;
}
.drop-menu {
position: relative;
display: inline-block;
outline: 0;
}
.drop-menu i.fa-bars {
padding-right: 30px;
padding-bottom: 14px;
transition: opacity .2s ease-in-out;
color: #000;
font-size: 27px;
}
.drop-menu .fa-bars:hover {
opacity: 0.6;
}
.drop-menu:focus {
pointer-events: none;
}
.drop-menu:focus .drop-menu-content {
opacity: 1;
visibility: visible;
pointer-events: auto;
}
.drop-menu-content {
position: absolute;
z-index: 1;
width: 200px;
padding: 0;
background-color: #eee;
height: auto;
border-radius: 5px;
opacity: 0;
visibility: hidden;
transition: visibility .5s;
margin: 0 0 0 -6px;
}
.drop-menu-content li {
margin: 5px;
display: block;
position: relative;
}
.drop-menu-content li:hover {
background-color: #222;
}
.drop-menu-content a:hover {
color: #fff;
}
.drop-menu-content i.fa-sort-asc {
color: #eee;
font-size: 30px;
position: absolute;
top: -10px;
left: 10px;
}
.drop-menu-content a {
text-decoration: none;
color: #000;
padding: 10px 15px;
display: block;
}
Thanks in advance to anyone and everyone who can help out.
EDIT: New code:
<html>
<head>
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<style>
/*Menu dropdown*/
.drop-menu i.fa-bars {
padding-right: 30px;
padding-bottom: 14px;
transition: opacity .2s ease-in-out;
color: #fff;
font-size: 27px;
}
.drop-menu .fa-bars:hover {
opacity: 0.6;
}
.drop-menu {
position: relative;
display: inline-block;
margin-left: 30px;
margin-top: 20px;
}
.drop-menu-content {
position: absolute;
z-index: 1;
width: 200px;
padding: 0;
background-color: #fff;
width: 200px;
height: auto;
border-radius: 5px;
visibility: hidden;
opacity: 0;
transition: visibility .5s;
}
.drop-menu-content li {
margin: 5px;
display: block;
position: relative;
}
ul.drop-menu-content li:hover {
background-color: #222;
}
ul.drop-menu-content a:hover {
color: #fff;
}
.drop-menu-content i {
color: #fff;
font-size: 30px;
position: absolute;
top: -10px;
left: 10px;
}
.drop-menu-content a {
text-decoration: none;
color: #000;
padding: 10px 15px;
display: block;
}
</style>
</head>
<body>
<header class="menu">
<div tabindex="0" class="drop-menu">
<i class="fa fa-bars" ></i>
<ul class="drop-menu-content">
<i class="fa fa-sort-asc"></i>
<li><a href="#">Home</a></li>
<li><a href="#">Lorem</a></li>
<li><a href="#">Ipsum</a></li>
<li><a href="#">Check</a></li>
</ul>
</div>
</nav>
</header>
<script>
var dropMenu = 0;
var toggling = false;
var opacity = 0;
var menu = document.getElementsByClassName("drop-menu-content");
window.onload = function() {
menu.style.opacity = 0;
}
function toggle() {
current = document.getElementsByClassName("drop-menu-content");
function frame() {
if(dropMenu == true) {
opacity -= 10;
menu[0].style.opacity = opacity / 100;
} else {
opacity += 10;
menu[0].style.opacity = opacity / 100;
}
if(opacity === 0) {
dropMenu = Math.Abs(dropMenu - 1);
toggling = false;
clearInterval(intervalId);
}
}
if(!toggling) {
toggling = true;
var intervalId = setInterval(frame,16);
}
}
</script>
</body>
</html>
Can't seem to figure out why it isn't working. Just incase you are wondering why everything is in-line, It's because i am creating a tumblr theme :).
<i>is not a valid child of<ul>. May be causing browser issues