[英]jQuery slide animate div with toggle button works every other time instead of every time
I'm trying to get a simple side navigation bar which you can hide and unhide with a single button. 我试图获得一个简单的侧面导航栏,您可以通过一个按钮隐藏和取消隐藏。
So far I have it worknig but only every other click, so click to hide works, then does nothing, next click unhides, nothing etc. Yet I have to button set to an image to change on every click, and that is working. 到目前为止,我有这worknig但仅隔点击,这样点击隐藏的作品,然后什么也不做,接下来点击取消隐藏,什么等等。但是我不得不按钮设置为图像,以改变每一次点击,那就是工作。
What I have so far is.... 到目前为止,我有...。
HTML: HTML:
<div id="navBar-control">
<a href="#" id="toggle-slide-button">
<img src="http://i.imgur.com/8BMxPsC.png" width="50px" height="50px" />
</a>
</div>
<div id="navBar">
<div class="navBtn navBtnText">
Calculator
</div>
<div class="navBtn navBtnText">
About
</div>
<div class="navBtn navBtnText">
Other Services
</div>
</div>
Script: 脚本:
var state = false;
$("#toggle-slide-button").click(function () {
if (!state) {
$('#navBar').animate({width: "toggle"}, 1000);
$('#toggle-slide-button img').attr('src', 'http://i.imgur.com/K3MG7gT.png');
state = true;
}
else {
$('#mnavBar').animate({width: "toggle"}, 1000);
$('#toggle-slide-button img').attr('src', 'http://i.imgur.com/8BMxPsC.png');
state = false;
}
});
and if it matters, CSS: 如果重要的话,CSS:
#navBar{
background-color: #660000;
height: 780x;
width: 80px;
}
.navBtn{
background-color: #660000;
color: white;
width: 80px;
height: 70px;
cursor: pointer;
border-bottom: 1px solid rgba(255,255,255,.2);
opacity: 1;
font-size: 12px;
text-align: center;
}
.navBtnText {
line-height: 120px;
vertical-align: bottom;
}
.navBtn:hover{
background-color:#990000;
}
#navBar-control {
width: 50px;
height: 100%;
padding: 0 15px;
background-color: #000;
}
Fiddle of this: http://jsfiddle.net/GUjPA/91/ 小提琴: http : //jsfiddle.net/GUjPA/91/
I have been basing this off this fiddle: http://jsfiddle.net/GUjPA/13/ This has the desired behavior only that it is on the right of the screen instead of the left. 我一直以这个小提琴为基础: http : //jsfiddle.net/GUjPA/13/这具有所需的行为,只是它位于屏幕的右侧而不是左侧。 The script I'm using is exactly the same, and besides different colours and size of side bar, its essentially exactly the same. 我使用的脚本完全相同,除了边栏的颜色和大小不同之外,其本质上也完全相同。
You have a mistake here: 您在这里有一个错误:
$('#mnavBar').animate({width: "toggle"}, 1000);
must be 一定是
$('#navBar').animate({width: "toggle"}, 1000);
http://jsfiddle.net/GUjPA/94/ here works http://jsfiddle.net/GUjPA/94/在这里有效
var state = false;
$("#toggle-slide-button").click(function () {
if (!state) {
$('#navBar').animate({width: "toggle"}, 1000);
$('#toggle-slide-button img').attr('src', 'http://i.imgur.com/K3MG7gT.png');
state = true;
}
else {
$('#navBar').animate({width: "toggle"}, 1000);
$('#toggle-slide-button img').attr('src', 'http://i.imgur.com/8BMxPsC.png');
state = false;
}
});
try this: you had added an m to the second call to #navbar
simplified in fiddle 试试这个:您已经在对#navbar
的第二个调用中添加了一个m,以简化提琴
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.