可折叠HTML(CSS + JQ) - 图标错误和"关闭其他"

时间:2018-04-25 08:08:23

标签: jquery html css

我在我的网站上使用此可折叠代码:https://www.w3schools.com/howto/howto_js_collapsible.asp (下面是动画和图标代码)。

我注意到当可折叠按钮有一个长文本时,图标会转到第二行。 知道如何解决这个问题(所以它会显示在第一行)?

还有一件事......你知道我怎么能添加"关闭他人"到那个可折叠的(所以当我点击按钮时,前一个按钮关闭)。

非常感谢!

1 个答案:

答案 0 :(得分:0)

根据您的要求,这是我创建的小提琴

  1. 修复“图标转到第二行”
  2. 添加折叠功能(当可折叠打开时,同一组中的其他人被隐藏)。
  3. 请检查代码段中的评论以获得进一步的示例:

    initCollapseGroup(".cgroup");
    
    //use this to init an accordion
    function initCollapseGroup(groupselector){
      var coll = document.querySelectorAll(groupselector + " .collapsible");
      //console.log(coll);
      var i;
    
      for (i = 0; i < coll.length; i++) {
        coll[i].addEventListener("click", function() {
          this.classList.toggle("active");
          var content = this.nextElementSibling;
          if (content.style.maxHeight) {
             content.style.maxHeight = null;
          } else {
          	closeCollapseGroup(groupselector);
            this.classList.toggle("active");
            content.style.maxHeight = content.scrollHeight + "px";
          }
        });
      }
    }
    
    // hides all .collapsible content inside group matched by groupselector variable
    function closeCollapseGroup(groupselector){
      var coll = document.querySelectorAll(groupselector + " .collapsible");
      //console.log(coll);
      var i;
      for (i = 0; i < coll.length; i++) {
      		coll[i].classList.remove("active");
          var content = coll[i].nextElementSibling;
          if (content.style.maxHeight) {
             content.style.maxHeight = null;
          }
      }
    }
    /* Style the button that is used to open and close the collapsible content */
    .collapsible {
      background-color: #eee;
      color: #444;
      cursor: pointer;
      padding: 18px;
      width: 100%;
      border: none;
      text-align: left;
      outline: none;
      font-size: 15px;
    }
    
    /* Add a background color to the button if it is clicked on (add the .active class with JS), and when you move the mouse over it (hover) */
    .active, .collapsible:hover {
      background-color: #ccc;
    }
    
    /* Style the collapsible content. Note: hidden by default */
    .content {
      padding: 0 18px;
      overflow: hidden;
      background-color: #f1f1f1;
      max-height: 0;
      overflow: hidden;
      transition: max-height 0.2s ease-out;
    }
    
    .collapsible:after {
        content: '\02795'; /* Unicode character for "plus" sign (+) */
        font-size: 13px;
        color: white;
        float: right;
        margin-left: 5px;
    }
    
    .active:after {
        content: "\2796"; /* Unicode character for "minus" sign (-) */
    }
    
    /*style for label element, width gets dinamically calculated to dont 
    overflow icon area, so that icon can't go in newline */
    .collapsible .label {
        width: calc(100% - 25px);
        display: inline-block;
    }
    <div class="cgroup">
    <button class="collapsible"><span class="label">Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Text Open Collapsible</span></button>
    <div class="content">
      <p>Lorem ipsum...</p>
    </div>
    <button class="collapsible"><span class="label">Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Text Open Collapsible</span></button>
    <div class="content">
      <p>Lorem ipsum...</p>
    </div>
    <button class="collapsible"><span class="label">Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Long Text Open Collapsible</span></button>
    <div class="content">
      <p>Lorem ipsum...</p>
    </div>
    </div>

    <强>加成

    你也可以使用bootstrap库来实现这个目标(如果没有这么好的话,我不会重新发明这种情况,在这种情况下,可能是性能和避免使用库的小东西)。 这是文档: https://getbootstrap.com/docs/4.0/components/collapse/#accordion-example