简体   繁体   English

切换Div容器的高度

[英]Toggle a Div container height

I am working on a button on which the toggle applies and the div height increases and decreases. 我正在研究一个按钮,将在该按钮上应用切换开关,并且div高度会增加和减少。 So what i am trying to do is that on a button lets say on #about click the #aboutsubmenu shows and the height of #maincontainer increases and on same click if if it's open it closes. 所以,我所要做的是,在一个按钮上可以说#about点击#aboutsubmenu节目和高度#maincontainer增加,在相同的点击,如果,如果它是打开它关闭。 Here is what i am trying to do 这是我想要做的

$(document).ready(function() {
    $('#aboutsubmenu').hide();
    var opened;
    var opened2;
    $('#about').click(function() {
        opened = "no";
        if (opened == "no") {
            $('#aboutsubmenu').show();
            $('#maincontainer').css('height', '387px');
            opened = "yes";
        }
        else if (opened == "yes") {

        }
    });
});

I am totally confused here. 我在这里完全感到困惑。 Can you help me out. 你能帮我吗。

Try this code: 试试这个代码:

$(document).ready(function () {
    $('#aboutsubmenu').hide();
    var opened = false;
    $('#about').click(function () {
        $('#aboutsubmenu').toggle(); // Toggle visibility
        $('#maincontainer').height(opened ? 387 : 100);
        opened = !opened;
    });
});

What you did wrong: 您做错了什么:

opened = "no";

that code at the start of the function makes it alwasy open. 该函数开始处的代码使其完全打开。

Try this, Initialize your variable outside event handler 试试这个,在事件处理程序外部初始化变量

var opened = "no"; //
var opened2;
$('#about').click(function () {
    if (opened == "no") {
        $('#aboutsubmenu').show();
        $('#maincontainer').css('height', '387px');
        opened = "yes";
    } else if (opened == "yes") {
        $('#aboutsubmenu').hide();
        $('#maincontainer').css('height', '100px');
        opened = "no";
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM