var body = $('body');
var button = $('#button');
var menu = $('#menu');
var menuWidth = $('#menu').width();
var closeMenu = $('#menuClose');
menu.css({
right: -menuWidth
});
button.html('Open');
function menuOperation() {
event.preventDefault();
if (body.css('right') == '0px') {
body.animate({
right: +menuWidth
}, 600);
button.html('Close');
button.css('background', 'rgba(203,203,203,1)');
menu.css('display', 'block'); /* added this */
} else {
body.animate({
right: 0
}, 600);
button.html('Open');
button.css('background', 'rgba(192,59,84,1)');
menu.css('display', 'none'); /*added this */
}
}
button.on('click', function(event) {
menuOperation();
});
closeMenu.on('click', function(event) {
menuOperation();
});
@import url(http://fonts.googleapis.com/css?family=Open+Sans:400,300,600);
body {
width: 100%;
right: 0;
margin: 0;
background: rgba(236, 236, 236, 1);
position: relative;
overflow: auto;
/*changed this */
}
#button {
width: 125px;
height: 30px;
padding: 10px 0 10px 0;
font-size: 20px;
background: rgba(192, 59, 84, 1);
color: rgba(255, 255, 255, 1);
font-family: 'Open Sans';
font-size: 20px;
text-align: center;
text-transform: uppercase;
position: absolute;
top: 15px;
right: 15px;
transition: all 0.2s ease-in-out;
-webkit-transition: all 0.2s ease-in-out;
-moz-transition: all 0.2s ease-in-out;
cursor: pointer;
}
@media(min-width: 321px) {
#button {
width: 100px;
height: 22px;
padding: 8px 0 10px 0;
font-size: 16px;
}
}
#menu {
width: 300px;
height: 650px;
background: rgba(24, 24, 24, 0.95);
color: rgba(255, 255, 255, 1);
position: absolute;
right: 0;
display: none;
/*changed this */
}
#menu > div:first-child {
width: 100%;
padding: 22.5px 0;
color: rgba(255, 255, 255, 1);
font-family: 'Open Sans';
font-size: 14px;
text-align: right;
text-transform: uppercase;
direction: rtl;
text-indent: 70px;
opacity: 1;
transition: opacity 0.8s ease-in-out;
-webkit-transition: opacity 0.8s ease-in-out;
-moz-transition: opacity 0.8s ease-in-out;
cursor: pointer;
display: block;
}
@media(min-width: 321px) {
#menu > div: first-child {
opacity: 0;
}
}
#menuClose {
width: 35px;
height: 35px;
border: 2px solid rgba(255, 255, 255, 0.15);
border-radius: 35px;
position: absolute;
top: 15px;
right: 15px;
cursor: pointer;
display: block;
}
#menuClose > span {
position: absolute;
top: 17.5px;
right: 0;
left: 7.5px;
}
#menuClose > span:first-child {
width: 20px;
height: 1.5px;
background: rgba(255, 255, 255, 1);
transform: rotate(45deg);
-webkit-transform: rotate(45deg);
-moz-transform: rotate(45deg);
display: block;
}
#menuClose > span:last-child {
width: 20px;
height: 1.5px;
background: rgba(255, 255, 255, 1);
transform: rotate(-45deg);
-webkit-transform: rotate(-45deg);
-moz-transform: rotate(-45deg);
display: block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="button"></div>
<div id="menu">
<div>Close Menu</div>
<div id="menuClose">
<span></span>
<span></span>
</div>
</div>
您是否嘗試過使用display css爲none,然後單擊open並單擊動態更改顯示? – Sai 2014-11-06 16:18:10
你也可以嘗試 – Sai 2014-11-06 16:19:31