如何使用 html, css 和 javascript 创建手风琴.因此,默认情况下

我使用以下代码创建了一个手风琴 -


网页:


<button class="accordion">C/C++</button>

<div class="panel">

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt 

       ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco 

       laboris nisi ut aliquip ex ea commodo consequat.

     </p>

</div>


<button class="accordion">JAVA</button>

<div class="panel">

    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor 

       incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud 

       exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

     </p>

</div>

CSS:


.accordion {

    background-color: #eee;

    color: #444;

    cursor: pointer;

    padding: 18px;

    width: 100%;

    border: none;

    text-align: left;

    outline: none;

    font-size: 15px;

    transition: 0.4s;

}


.active, .accordion:hover {

    background-color: #ccc;

}


.panel {

    padding: 0 18px;

    background-color: white;

    max-height: 0;

    overflow: hidden;

    transition: max-height 0.2s ease-out;

}

JavaScript:


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

var i;


for (i = 0; i < acc.length; i++) {

    acc[i].addEventListener("click", function() {

        this.classList.toggle("active");

        var panel = this.nextElementSibling;

        if (panel.style.maxHeight) {

            panel.style.maxHeight = null;

        } else {

            panel.style.maxHeight = panel.scrollHeight + "px";

        } 

    });

}

它给出这样的输出 -

http://img.mukewang.com/62fdf564000137b401640089.jpg

但是我想首先在默认情况下打开页面时应该打开一个,并且一次只能打开一个。像这样——

http://img1.mukewang.com/62fdf56f0001034908220168.jpg

http://img4.mukewang.com/62fdf57700017cf408130160.jpg

并且应该有+符号最后打开它关闭和 - 当它打开时。


DIEA
浏览 113回答 2
2回答

临摹微笑

尝试运行代码段。您可能需要更改 CSS 样式。[name="accordion-group"]{&nbsp; &nbsp; display: none;}.element-list{&nbsp; margin-left: 0;&nbsp; padding-left: 0;&nbsp; list-style-type: none;}.accordion-group label{&nbsp; &nbsp; &nbsp; color: #666;&nbsp; &nbsp; &nbsp; cursor: pointer;}.accordion-group label:hover{&nbsp; &nbsp; color: #C85A17;}.accordion-toggle:before{&nbsp; &nbsp; content: "▶ ";}.accordion-toggle::before{&nbsp; &nbsp; &nbsp; content: "▶ ";}[name="accordion-group"] ~ .accordion-h .accordion-toggle{&nbsp; &nbsp; display: block;}[name="accordion-group"]:checked ~ .accordion-h .accordion-toggle{&nbsp; &nbsp; &nbsp; display: none;}.accordion-dismiss:before{&nbsp; &nbsp; &nbsp; content: "▼ " !important;}.accordion-dismiss::before{&nbsp; &nbsp; &nbsp; content: "▼ " !important;}[name="accordion-group"] ~ h4 .accordion-dismiss{&nbsp; &nbsp; &nbsp; display: none;}[name="accordion-group"]:checked ~ h4 .accordion-dismiss{&nbsp; &nbsp; &nbsp; display: block;}.accordion-body{&nbsp; &nbsp; &nbsp; border-bottom: 1px solid #CC6600;&nbsp; &nbsp; &nbsp; height: auto;&nbsp; &nbsp; &nbsp; max-height: 0;&nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; &nbsp; background-color: hsla(0, 0%, 100%, .25);&nbsp; &nbsp; &nbsp; box-shadow: 0 2px 2px #CC6600 inset;&nbsp; &nbsp; &nbsp; -webkit-transition: max-height .2s ease 0s;&nbsp; &nbsp; &nbsp; transition: max-height .25s ease 0s;}.accordion-group li:last-child .accordion-body{&nbsp; &nbsp; border-bottom-width: 1;}[name="accordion-group"]:checked ~ .accordion-body{&nbsp; &nbsp; &nbsp; max-height: 660px;&nbsp; &nbsp; &nbsp; transition-duration: .75s;}.accordion-inner{&nbsp; padding: 0.631100639515315em 2%;&nbsp;}<div>&nbsp; <input checked="checked" id="collapse-all" name="accordion-group" type="radio" />&nbsp; <ul class="accordion-group element-list">&nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; <input id="collapse-1" name="accordion-group" type="radio" checked/>&nbsp; &nbsp; &nbsp; <h4 class="accordion-h">&nbsp; &nbsp; &nbsp; &nbsp; <label class="accordion-dismiss" for="collapse-all">C++</label>&nbsp; &nbsp; &nbsp; &nbsp; <label class="accordion-toggle" for="collapse-1">C++</label>&nbsp; &nbsp; &nbsp; </h4>&nbsp; &nbsp; &nbsp; <div class="accordion-body">&nbsp; &nbsp; &nbsp; &nbsp; <div class="accordion-inner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span>Content</span></p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </li>&nbsp; &nbsp; <li>&nbsp; &nbsp; &nbsp; <input id="collapse-2" name="accordion-group" type="radio" />&nbsp; &nbsp; &nbsp; <h4 class="accordion-h">&nbsp; &nbsp; &nbsp; &nbsp; <label class="accordion-dismiss" for="collapse-all">JAVA</label>&nbsp; &nbsp; &nbsp; &nbsp; <label class="accordion-toggle" for="collapse-2">JAVA</label>&nbsp; &nbsp; &nbsp; </h4>&nbsp; &nbsp; &nbsp; <div class="accordion-body">&nbsp; &nbsp; &nbsp; &nbsp; <div class="accordion-inner">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Content&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; </li>&nbsp; </ul></div>

MMTTMM

可折叠面板标题中的&nbsp;+&nbsp;和&nbsp;-&nbsp;图标可以使用&nbsp;CSS 内容属性添加。手风琴面板的扩展和收缩可以通过Javascript更改“高度”属性来控制。下面是一个工作代码示例,用于显示图标并控制 acordion 的扩展:<!DOCTYPE html><html>&nbsp; &nbsp; <head>&nbsp; &nbsp; &nbsp; &nbsp; <meta name="viewport" content="width=device-width, initial-scale=1">&nbsp; &nbsp; &nbsp; &nbsp; <title>Accordion - demo</title>&nbsp; &nbsp; &nbsp; &nbsp; <style>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .accordion {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color: #eee;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: #444;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cursor: pointer;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 18px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; width: 100%;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; border: none;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; text-align: left;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; outline: none;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-size: 15px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition: 0.4s;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .active, .accordion:hover {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color: #ccc;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .accordion:after {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content: '\002B';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; color: #777;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; font-weight: bold;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; float: right;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; margin-left: 5px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .active:after {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; content: "\2212";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .panel {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; padding: 0 18px;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; background-color: white;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max-height: 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; overflow: hidden;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; transition: max-height 0.2s ease-out;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </style>&nbsp; &nbsp; </head>&nbsp; &nbsp; <body>&nbsp; &nbsp; &nbsp; &nbsp; <button class="accordion">C / C++</button>&nbsp; &nbsp; &nbsp; &nbsp; <div class="panel">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>C programming</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>C++ programming</p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <button class="accordion">Java</button>&nbsp; &nbsp; &nbsp; &nbsp; <div class="panel">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Java programming</p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <button class="accordion">Javascript</button>&nbsp; &nbsp; &nbsp; &nbsp; <div class="panel">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <p>Javascript programming</p>&nbsp; &nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; &nbsp; <script>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var accordions = document.getElementsByClassName("accordion");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < accordions.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; accordions[i].addEventListener(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "click",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; function() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.classList.toggle("active")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var panel = this.nextElementSibling&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( panel.style.maxHeight ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel.style.maxHeight = null&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; panel.style.maxHeight = (panel.scrollHeight + "px")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; </script>&nbsp; &nbsp; </body></html>输出:更多信息:https://www.w3schools.com/howto/howto_js_accordion.asp
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript