javascript - 使用 jQuery 切换 div 的问题

标签 javascript jquery html css

我有一个隐藏区域,它会在单击红色边栏时显示。我希望它在我再次单击红色边栏时隐藏。

我在隐藏区域内放置了一个蓝色条,单击时隐藏可折叠区域。我希望红色条同时执行这两项操作,即隐藏和显示可折叠区域。

HTML:

<div class="frame">
    <div id="menu" class="menu nav-collapse collapse width aux">
        <div class="collapse-inner">
            <div id="left-collapser"></div>
        </div>
    </div>
    <div class="view aux2">
        <div id="right-collapser" data-toggle="collapse" data-target="#menu"></div>
    </div>
</div>

CSS

#left-collapser {
    width: 20px;
    height: 100%;
    float: right;
    background-color: lightblue;
}
#right-collapser {
    width: 20px;
    height: 100%;
    float: left;
    background-color: red;
    border-top-right-radius: 20px;
    border-bottom-right-radius: 20px;

}

.aux {
    border: 1px solid red;
    background-color: yellow;
}
.aux2 {
    border: 1px solid blue;
    background-color: lightgreen;
}
 body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;

  .frame {
    position: absolute;
    width: 200%;
    height: 100%;
    top: 0;
    bottom: 0;
    left: 0;

    .menu {
      height:100%;
      /* background-color: #3D6AA2; */

      &.collapse {
        float:left;
        height: 100% !important;
        width: auto;
      }

      &.collapse.height {
        position: relative;
        height: 0;
        overflow: hidden;
        -webkit-transition: height 0.35s ease;
        -moz-transition: height 0.35s ease;
        -o-transition: height 0.35s ease;
        transition: height 0.35s ease;
      }

      &.collapse.width {
        position: relative;
        width: 0;
        overflow: hidden;
        -webkit-transition: width 0.35s ease;
        -moz-transition: width 0.35s ease;
        -o-transition: width 0.35s ease;
        transition: width 0.35s ease;
      }

      &.collapse.in.width {
        width: auto;
      }

      &.collapse.in.height {
        height: auto;
      }

      .collapse-inner {
        position: relative;        
        width: 250px;
        height: 100%;
      }

    }
    .view {
      width: 50%;
      height: 100%;
      overflow: hidden;

    }
  }
}

Javascript

$("#left-collapser").click(function (e) {
    $('#menu').collapse('hide');
});

$("#right-collapser").click(function (e) {
    $('#menu').collapse('show');
});

这是Fiddle

•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••••••• 编辑••••••••••••••••••••••••••••••••••••••••••••••••••• ••••••••••••••••••••••••••••••••••• •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• ••••••••••••••••••••••••••••••••••••••••

事实证明根本不需要 Javascript,因为动画是通过 CSS 拉取的:

FIDDLE

我以为 Javascript 会产生折叠/展开效果,但不是,只有 CSS。我得到了我想要的,但由于我重复使用了我在网上找到的代码,我仍然想知道这种效果究竟是如何实现的 - 所以我可以自己做而不是寻找已经完成的东西。

最佳答案

试试这个:

$("#left-collapser").click(function (e) {
    $('#menu').toggle('collapse');
});

FIDDLE

您可能不需要蓝色区域。

关于javascript - 使用 jQuery 切换 div 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27397848/

相关文章:

javascript - 尝试将菜单页面添加到 WordPress 管理站点

javascript - jQuery - 从任何不仅仅是单击的 <li> 中删除类

javascript - 使用 jQuery 移动 ajax 请求返回的数据

jquery - 如何将查询结果合并为新列?

javascript - 错误 “Exception in template helper: Error: Handler with name ' pathFinder' 已存在是什么意思?”意思是?

html - 多个输入标签。标签和文本字段出现在不同的行中

javascript - 如何从 href 链接替换哈希 anchor (octothorpe)

javascript - 将对象转换为二维数组但删除键

javascript - 如何使用 jsfiddle 在线服务托管图像

jquery - 将 jQuery 变量传递给 CSS