jQuery SlideToggle 单独切换元素

发布于 2024-10-19 05:20:47 字数 2314 浏览 10 评论 0原文

我根据自己的喜好设计了新的 WP 管理栏,并添加了“隐藏”按钮。我将按钮设置为滑动切换“wpadminbar”div。当我单击隐藏它时,div 和搜索表单(嵌套在“wpadminbar”div 中)两者切换。这是该问题的视频...

jQuery 切换问题

代码非常简单。

$(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...

jQuery toggle problem

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-10-26 05:20:47

您的问题与此处的 position:fixed 有关,

#adminbarsearch {
width: 250px;
height: 41px;
position: fixed;
right: 10px;
top: 10px;
}

如果您将其删除,您将看到切换现在可以正常工作

示例

Your problem has to do with the position:fixed here

#adminbarsearch {
width: 250px;
height: 41px;
position: fixed;
right: 10px;
top: 10px;
}

if you remove that you will see that toggle now works fine

example http://jsfiddle.net/circadian/YvnrS/2/

九歌凝 2024-10-26 05:20:47

如果您的管理栏的 CSS 已经以静态以外的任何方式定位(看起来可能是这样),为什么不直接对顶部位置进行动画处理呢?

$('a#hide-admin').click(function () {
    $('#wpadminbar').add(this).animate({
        top: '-=' + $('#wpadminbar').height()
    }, 1000, function () {
        // Callback to manipulate the #hide-admin button if desired
    });
});

如果您想要来回切换功能,您只需多做一点思考即可。例如,您可以根据栏是否仍在屏幕上以编程方式创建动画选项对象,并正确修改 {}.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?

$('a#hide-admin').click(function () {
    $('#wpadminbar').add(this).animate({
        top: '-=' + $('#wpadminbar').height()
    }, 1000, function () {
        // Callback to manipulate the #hide-admin button if desired
    });
});

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文