如何使用jQuery切换(隐藏/显示)侧边栏div

25

我有两个id为A和B的

元素。div A具有固定的宽度,被视为侧边栏。

布局如下图所示:

Layout

样式如下:

html, body {
    margin: 0;
    padding: 0;
    border: 0;
}
#A, #B {
    position: absolute;
}
#A {
    top: 0px;
    width: 200px;
    bottom: 0px;
}
#B {
    top: 0px;
    left: 200px;
    right: 0;
    bottom: 0px;
}

我有一个作为切换按钮的<a id="toggle">toggle</a>。在点击切换按钮时,侧边栏可能会隐藏到左侧,而B div应该拉伸以填充空白区域。在第二次点击时,侧边栏可能会重新出现在之前的位置,B div应该恢复到先前的宽度。

如何使用jQuery完成这个操作?

8个回答

25

4
toggle在jQuery 1.8中已被弃用,并在jQuery 1.9中移除。 - laike9m
1
toggle仍然存在于jQuery中,但是它的签名不同,正如这个答案所示。toggle - RyBolt
你好,为什么在jQuery中使用showing/hiding按钮时会出现问题,请参见http://jsfiddle.net/7pf044cn/。 - bumbumpaw

12

请查看这个示例,并查看jquery的toggle方法animate方法的文档。

$('#toggle').toggle(function(){
    $('#A').animate({width:0});
    $('#B').animate({left:0});
},function(){
    $('#A').animate({width:200});
    $('#B').animate({left:200});
});
基本上,您可以在设置布局的属性上进行动画处理。
更高级的版本:
$('#toggle').toggle(function(){
    $('#A').stop(true).animate({width:0});
    $('#B').stop(true).animate({left:0});
},function(){
    $('#A').stop(true).animate({width:200});
    $('#B').stop(true).animate({left:200});
})

这将停止先前的动画,清除动画队列并开始新的动画。


5
您可以访问w3school获取解决方案,链接在这里:这里。另外还有另一个例子也可用,可能会很有帮助,看一下

2
以下内容适用于新版本的jQuery。
$(window).on('load', function(){

    var toggle = false;

    $('button').click(function() {
        toggle = !toggle;

        if(toggle){
            $('#B').animate({left: 0});
        }
        else{
            $('#B').animate({left: 200});
        }

    });
});

1
使用Javascript。

var side = document.querySelector("#side");
var main = document.querySelector("#main");
var togg = document.querySelector("#toogle");
var width = window.innerWidth;

window.document.addEventListener("click", function() {

  if (side.clientWidth == 0) {
//    alert(side.clientWidth);
    side.style.width      = "200px";
    main.style.marginLeft = "200px";
    main.style.width      = (width - 200) + "px";
    togg.innerHTML        = "Min";
  } else {
//    alert(side.clientWidth);
    side.style.width      = "0";
    main.style.marginLeft = "0";
    main.style.width      = width + "px";    
    togg.innerHTML        = "Max";
  }

}, false);
button {
  width: 100px;
  position: relative; 
  display: block; 
}

div {
  position: absolute;
  left: 0;
  border: 3px solid #73AD21;
  display: inline-block;
  transition: 0.5s; 
}

#side {
  left: 0;
  width: 0px;
  background-color: red;
}
 
#main {
  width: 100%;
  background-color: white;    
}
<button id="toogle">Max</button>
<div id="side">Sidebar</div>
<div id="main">Main</div>


0
$(document).ready(function () {
    $(".trigger").click(function () {
        $("#sidebar").toggle("fast");
        $("#sidebar").toggleClass("active");
        return false;
    });
});


<div>
    <a class="trigger" href="#">
        <img id="icon-menu" alt='menu' height='50' src="Images/Push Pin.png" width='50' />
    </a>
</div>
<div id="sidebar">
</div>

不要用 #sidebar,给你的 div 加上 id。


0
$('#toggle').click(function() {
  $('#B').toggleClass('extended-panel');
  $('#A').toggle(/** specify a time here for an animation */);
});

并在CSS中:

.extended-panel {
  left: 0px !important;
}

-1

这有助于隐藏和显示侧边栏,内容将占据侧边栏留下的空白处。

<div id="A">Sidebar</div>
<div id="B"><button>toggle</button>
Content here: Bla, bla, bla
</div>


//Toggle Hide/Show sidebar slowy
  $(document).ready(function(){
      $('#B').click(function(e) {
          e.preventDefault();
          $('#A').toggle('slow');
          $('#B').toggleClass('extended-panel'); 
      });
  }); 


html, body {
    margin: 0;
    padding: 0;
    border: 0;
}
#A, #B {
    position: absolute;
}
#A {
    top: 0px;
    width: 200px;
    bottom: 0px;
    background:orange;
}
#B {
    top: 0px;
    left: 200px;
    right: 0;
    bottom: 0px;
    background:green;
}

/* makes the content take place of the SIDEBAR 
   which is empty when is hided */
.extended-panel {
  left: 0px !important;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接