admin管理员组文章数量:1416631
I'm trying to create a slide out menu, that open an closes on the same a tag. I've put something together but, it runs through the whole animation instead of pausing after opening.
HTML
<header>
<nav>
<ul class="slide-menu">
<li class="menu-element"><a href="#" id="aboutopen">How tall?</a></li>
<li class="menu-element"><a href="#book">Books</a></li>
<li class="menu-element"><a href="weblink" target="_blank">Journal</a></li>
<li class="menu-element"><a href="#" id="aboutcontact">Contact</a></li>
</ul>
<a id="puller" href="#">Menu</a>
</nav>
</header>
Jquery
$(document).ready(function()
{
$("#puller").click(function(){
$(".slide-menu").animate({
marginLeft: '+=360px'
}, 500);
});
$("#puller").click(function(){
$(".slide-menu").animate({
marginLeft: '-=360px'
}, 500);
});
});
Can anyone help with this?
I'm trying to create a slide out menu, that open an closes on the same a tag. I've put something together but, it runs through the whole animation instead of pausing after opening.
HTML
<header>
<nav>
<ul class="slide-menu">
<li class="menu-element"><a href="#" id="aboutopen">How tall?</a></li>
<li class="menu-element"><a href="#book">Books</a></li>
<li class="menu-element"><a href="weblink" target="_blank">Journal</a></li>
<li class="menu-element"><a href="#" id="aboutcontact">Contact</a></li>
</ul>
<a id="puller" href="#">Menu</a>
</nav>
</header>
Jquery
$(document).ready(function()
{
$("#puller").click(function(){
$(".slide-menu").animate({
marginLeft: '+=360px'
}, 500);
});
$("#puller").click(function(){
$(".slide-menu").animate({
marginLeft: '-=360px'
}, 500);
});
});
Can anyone help with this?
Share Improve this question asked Nov 13, 2013 at 22:39 user2674488user2674488 11 silver badge1 bronze badge 1- Left an answer, if it works, please "accept" it. :) – Mike Barwick Commented Nov 13, 2013 at 22:57
2 Answers
Reset to default 3Using jQuery toggle is a good idea. You can also simply maintain the state of the menu as to slided out already or not and take action like this
$(document).ready(function(){
var slide = false;
$("#puller").click(function(){
if(slide){
$(".slide-menu").animate({marginLeft: '-=360x'}, 500);
slide = false;
}else{
$(".slide-menu").animate({marginLeft: '+=360px'}, 500);
slide = true
}
});
});
Use jQuery Toggle, like so. Simple.
$(document).ready(function() {
var menu = $('.slide-menu');
var speed = 500; // set animation speed
$('#puller').toggle(
function(){
menu.animate({
marginLeft: '+=360px'
}, speed);
},
function(){
menu.animate({
marginLeft: '-=360px'
}, speed);
}
);
)};
本文标签: javascriptMenu Open amp Close on menu buttonJqueryStack Overflow
版权声明:本文标题:javascript - Menu Open & Close on menu button - Jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745256485a2650133.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论