如何使Accordin按钮的高度均匀填充剩余空间

时间:2018-11-07 20:52:43

标签: javascript html css css3 layout

我已经开发出一种布局,其中按钮元素在关闭时应填充其下方的空白区域。我尝试了几种方法,但是没有运气。

这是我的代码:

var accordion = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < accordion.length; i++) {
			accordion[i].addEventListener("click", function() {
		
			panel = this.nextElementSibling;

            if (panel.style.maxHeight){
                panel.style.maxHeight = null;
            } else {
				for(j = 0; j < accordion.length; j++) {
					if(j != i) {
						accordion[j].nextElementSibling.style.maxHeight = null;
						accordion[j].classList.remove('active');
					}
				}
                panel.style.maxHeight = panel.scrollHeight + "px";
            }
			
			this.classList.toggle("active");
        });
    }
body {
            margin: 0;
            background-color: #e0d9d4;
        }

        .container {
            height: 100%;
        }

        .about {
            float: left;
            width: 50%;
            color : white;
            background-color : black;
        }

        .projects {
            float : right;
            width : 50%;
        }

        .about h2 {
            font-family: Clearface;
            font-size : 80px;
            padding: 30px;
        }

        .about p {
            font-family: Clearface;
            font-size : 30px;
            padding: 30px;
        }

        .accordion {
            background-color: #333648;
            color: white;
            cursor: pointer;
            padding: 20px;
            width: 100%;
            border: none;
            text-align: center;
            outline: none;
            font-size: 17px;
            transition: 0.5s;
            
        }

        .active, .accordion:hover {
            background-color: #ccc;
            color: black;
        }

        .panel {
            padding:0 18px;
            background-color: white;
            max-height: 0;
            overflow-y: hidden;
            transition: max-height 0.3s ease-out;
        }

        .img-responsive {
            display: block;
            max-width: 100%;
            height: auto;
            margin: 50px auto;
        }

        .header-img-responsive {
            display: block;
            width: 100%;
            height: auto;
        }

        .post {
            padding: 0 20%;
        }

        .title {
            text-align: center;
            color: #932D26;
        }
<div class="container">
    <div class="about">
            <h2>
                Hello!
            </h2>
            <p>
                Lorem Ipsum är en utfyllnadstext från tryck- och förlagsindustrin. Lorem ipsum har varit standard ända sedan 1500-talet, när en okänd boksättare tog att antal bokstäver och blandade dem för att göra ett provexemplar av en bok. Lorem ipsum har inte bara överlevt fem århundraden, utan även övergången till elektronisk typografi utan större förändringar. Det blev allmänt känt på 1960-talet i samband med lanseringen av Letraset-ark med avsnitt av Lorem Ipsum, och senare med mjukvaror som Aldus PageMaker.
            </p>
    </div>

    <div class="projects">
        <button class="accordion">Project 1</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 2</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 3</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 4</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 5</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>
    </div>
</div>

我怎么能达到这种效果?

1 个答案:

答案 0 :(得分:1)

display: flex类中添加.container并在height: 20%类中添加accordion,它将起作用。检查以下工作代码。

var accordion = document.getElementsByClassName("accordion");
    var i;

    for (i = 0; i < accordion.length; i++) {
			accordion[i].addEventListener("click", function() {
		
			panel = this.nextElementSibling;

            if (panel.style.maxHeight){
                panel.style.maxHeight = null;
            } else {
				for(j = 0; j < accordion.length; j++) {
					if(j != i) {
						accordion[j].nextElementSibling.style.maxHeight = null;
						accordion[j].classList.remove('active');
					}
				}
                panel.style.maxHeight = panel.scrollHeight + "px";
            }
			
			this.classList.toggle("active");
        });
    }
body {
            margin: 0;
            background-color: #e0d9d4;
        }

        .container {
            height: 100%;
            display: flex;
        }

        .about {
            float: left;
            width: 50%;
            color : white;
            background-color : black;
        }

        .projects {
            float : right;
            width : 50%;
        }

        .about h2 {
            font-family: Clearface;
            font-size : 80px;
            padding: 30px;
        }

        .about p {
            font-family: Clearface;
            font-size : 30px;
            padding: 30px;
        }

        .accordion {
            background-color: #333648;
            color: white;
            cursor: pointer;
            padding: 20px;
            width: 100%;
            height: 20%;
            border: none;
            text-align: center;
            outline: none;
            font-size: 17px;
            transition: 0.5s;
            
        }

        .active, .accordion:hover {
            background-color: #ccc;
            color: black;
        }

        .panel {
            padding:0 18px;
            background-color: white;
            max-height: 0;
            overflow-y: hidden;
            transition: max-height 0.3s ease-out;
        }

        .img-responsive {
            display: block;
            max-width: 100%;
            height: auto;
            margin: 50px auto;
        }

        .header-img-responsive {
            display: block;
            width: 100%;
            height: auto;
        }

        .post {
            padding: 0 20%;
        }

        .title {
            text-align: center;
            color: #932D26;
        }
<div class="container">
    <div class="about">
            <h2>
                Hello!
            </h2>
            <p>
                Lorem Ipsum är en utfyllnadstext från tryck- och förlagsindustrin. Lorem ipsum har varit standard ända sedan 1500-talet, när en okänd boksättare tog att antal bokstäver och blandade dem för att göra ett provexemplar av en bok. Lorem ipsum har inte bara överlevt fem århundraden, utan även övergången till elektronisk typografi utan större förändringar. Det blev allmänt känt på 1960-talet i samband med lanseringen av Letraset-ark med avsnitt av Lorem Ipsum, och senare med mjukvaror som Aldus PageMaker.
            </p>
    </div>

    <div class="projects">
        <button class="accordion">Project 1</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 2</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 3</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 4</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>

        <button class="accordion">Project 5</button>
        <div class="panel">
            <div class="post">
                <h2>TITLE HEADING</h2>
                <p>
                    Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


                </p>
                <div>
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                    <img src="images/placeholder.jpg" class="img-responsive">
                </div>
            </div>
        </div>
    </div>
</div>