简体   繁体   English

使用JavaScript展开和折叠Div

[英]Expand and Collapse Div using Javascript

This is my code and it works fine. 这是我的代码,可以正常工作。 But, I don't want to keep the style in div tag so I created a class "sections" in style.css and placed it in the class tag. 但是,我不想将样式保留在div标签中,所以我在style.css中创建了一个类“ sections”并将其放置在class标签中。 But, now the expand and collapse doesn't work. 但是,现在展开和折叠不起作用。 Any help would be appreciated. 任何帮助,将不胜感激。

<div class="col-sm-10" >
            <div id="profile_section" class="center-block sections"style="border:2px solid black; 
            height:40px;
            width:60%;">
                <h4>Profile<h4>
                <button id="1" onclick="func1(this)" class="icon_button glyphicon glyphicon-chevron-down">
            </div>
            <br>

<script>

function func1(item){
    item = item.closest("div");
    if(item.style.height=="300px"){
        console.log("height=300px");
        item.className = 'center-block smooth_contract';
        item.addEventListener("animationend", function(){
            item.className = "center-block";
            item.style.height="40px";
            item.style.width="60%";
            //scrollTo(document.body,item.offsetTop,100);
        });
    }
    else if(item.style.height=="40px"){
        item.className = 'center-block smooth_expand';
        item.addEventListener("animationend", function(){
            item.className = "center-block";
            item.style.height="300px";
            item.style.width="100%";    

        });

    }
}

After adding class "sections". 添加类“节”之后。

<div class="col-sm-10" >
            <div id="profile_section" class="center-block sections">
                <h4>Profile<h4>
                <button id="1" onclick="func1(this)" class="icon_button glyphicon glyphicon-chevron-down">
            </div>
            <br>
</div>

我必须在javascript中使用getComputedStyle才能从外部文件获取CSS

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

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