jQuery SlideToggle 单独切换元素
我根据自己的喜好设计了新的 WP 管理栏,并添加了“隐藏”按钮。我将按钮设置为滑动切换“wpadminbar”div。当我单击隐藏它时,div 和搜索表单(嵌套在“wpadminbar”div 中)两者切换。这是该问题的视频...
代码非常简单。
$(function() {
$("a#hide-admin").click(function(){
$(this).toggleClass('hidden', 1000);
$("#wpadminbar").slideToggle(1000);
});
});
当我将 .slideToggle
更改为 .toggle
时,也会发生同样的情况。知道是什么原因造成的和/或如何补救吗?
编辑 这是所有相关代码。
<div id="wpadminbar">
<div class="quicklinks">
<ul>
<li> Admin Bar links go here... </li>
</div>
<div id="adminbarsearch-wrap">
<form action="http://myurl.com"
method="get"
id="adminbarsearch"
name="adminbarsearch">
<input class="adminbar-input"
name="s"
id="adminbar-search"
type="text"
value=""
maxlength="150" /> <input type="submit"
class="adminbar-button"
value="Search" />
</form>
</div>
</div>
CSS:
#wpadminbar {
position: fixed;
top: 0;
height: 41px;
width: 100%;
background: url(images/adminbar-bg.png) repeat-x;
}
.quicklinks ul {
list-style:none;
margin:0;
padding:0;
text-align:center;
display: inline;
}
.quicklinks ul li {
display: inline;
}
.quicklinks ul li a {
display:inline-block;
padding:0 10px;
text-decoration: none;
font-weight: 700;
color: #fff;
line-height: 41px;
font-family: "Helvetica", Arial, sans-serif;
font-size: 12px;
text-shadow: 1px 1px 0 #111;
}
#adminbarsearch {
width: 250px;
height: 41px;
position: fixed;
right: 10px;
top: 10px;
}
#hide-admin {
display: block;
position: fixed;
right: 10px;
top: 51px;
font: bold 11px Helvetica, Arial, sans-serif;
color: #fff;
background: #C91D07;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 20px;
padding: 6px 10px;
cursor: pointer;
}
#hide-admin.hidden {
top: 10px !important;
}
I've styled the new WP Admin Bar to my liking, and added a "Hide" button. I set the button to slideToggle the "wpadminbar" div. When I click to hide it, the div and search form (which is nested in the "wpadminbar" div) both toggle. Here's a video of the issue...
The code is pretty straight forward.
$(function() {
$("a#hide-admin").click(function(){
$(this).toggleClass('hidden', 1000);
$("#wpadminbar").slideToggle(1000);
});
});
The same thing happens when I change .slideToggle
to .toggle
. Any idea what's causing this and/or how to remedy it?
EDIT
Here's all of the pertinent code.
<div id="wpadminbar">
<div class="quicklinks">
<ul>
<li> Admin Bar links go here... </li>
</div>
<div id="adminbarsearch-wrap">
<form action="http://myurl.com"
method="get"
id="adminbarsearch"
name="adminbarsearch">
<input class="adminbar-input"
name="s"
id="adminbar-search"
type="text"
value=""
maxlength="150" /> <input type="submit"
class="adminbar-button"
value="Search" />
</form>
</div>
</div>
CSS:
#wpadminbar {
position: fixed;
top: 0;
height: 41px;
width: 100%;
background: url(images/adminbar-bg.png) repeat-x;
}
.quicklinks ul {
list-style:none;
margin:0;
padding:0;
text-align:center;
display: inline;
}
.quicklinks ul li {
display: inline;
}
.quicklinks ul li a {
display:inline-block;
padding:0 10px;
text-decoration: none;
font-weight: 700;
color: #fff;
line-height: 41px;
font-family: "Helvetica", Arial, sans-serif;
font-size: 12px;
text-shadow: 1px 1px 0 #111;
}
#adminbarsearch {
width: 250px;
height: 41px;
position: fixed;
right: 10px;
top: 10px;
}
#hide-admin {
display: block;
position: fixed;
right: 10px;
top: 51px;
font: bold 11px Helvetica, Arial, sans-serif;
color: #fff;
background: #C91D07;
-moz-border-radius: 20px;
-webkit-border-radius: 20px;
border-radius: 20px;
padding: 6px 10px;
cursor: pointer;
}
#hide-admin.hidden {
top: 10px !important;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的问题与此处的
position:fixed
有关,如果您将其删除,您将看到切换现在可以正常工作
示例
Your problem has to do with the
position:fixed
hereif you remove that you will see that toggle now works fine
example http://jsfiddle.net/circadian/YvnrS/2/
如果您的管理栏的 CSS 已经以静态以外的任何方式定位(看起来可能是这样),为什么不直接对顶部位置进行动画处理呢?
如果您想要来回切换功能,您只需多做一点思考即可。例如,您可以根据栏是否仍在屏幕上以编程方式创建动画选项对象,并正确修改 {}.top 值。
If your admin bar's CSS is already positioned in any way other than static (which it appears it may be), why not just animate the top position?
You'd just have to do only a tiny bit more thinking in the event you wanted a toggle back/forth kind of functionality. For example you could programmatically create the animation options object based on whether or not the bar was still on-screen, correctly modifying the {}.top value.