I'm working on a personal project to keep different snippets/examples/small projects of mine organized. I want to make the most of my page width, so I decided to write a navigation menu that slides out. It works as expected, but the code is kinda big.
I provided the HTML/CSS just in case that helps, but I'm really only looking for help with the JavaScript. I don't need anyone to rewrite the whole thing.
$(document).ready(function() {
$('#tab').click(function() {
if($('#main').attr('class') != 'out') {
$('#main').animate({
'margin-left': '+=340px',
}, 500, function() {
$('#main').addClass('out');
});
} else {
$('#main').animate({
'margin-left': '-=340px',
}, 500, function() {
$('#main').removeClass('out');
});
}
});
//does the same as above, but when I press ctrl + 1. from /http://www.scottklarr.com/topic/126/how-to-create-ctrl-key-shortcuts-in-javascript/
$(document).keyup(function (e) {
if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
if(e.which == 17) isCtrl=true;
if(e.which == 97 && isCtrl == true) {
if($('#main').attr('class') != 'out') {
$('#main').animate({
'margin-left': '+=340px',
}, 500, function() {
$('#main').addClass('out');
});
} else {
$('#main').animate({
'margin-left': '-=340px',
}, 500, function() {
$('#main').removeClass('out');
});
}
return false;
}
});
});
#sidebar {
top: 0;
left: 0;
position: relative;
float: left;
}
#main {
width: 320px;
padding: 10px;
float: left;
margin-left: -340px;
}
#tab {
width: 30px;
height: 120px;
padding: 10px;
float: left;
}
.clear { clear: both; }
<div id="sidebar">
<div id="main"></div>
<div id="tab"></div>
<br class="clear">
</div>