让 Bootsrrap 手风琴等待工作完成后再展开

问题描述 投票:0回答:1

按下手风琴按钮,我想等到计算手风琴主体,然后展开其内容。 否则,随着将新元素添加到手风琴主体,扩展会分几个阶段发生。不光滑。

const ab = document.querySelector('.accordion-button');
ab.addEventListener('click', abListener);

function abListener(ev) {
    // Heavy DOM work to build accordion body;
}

我想我可以用

preventDefault
方法来做到。但手风琴仍然扩大。

ab.addEventListener('click', abListener);
// mouseup, mousedown and so on...

function abListener(ev) {
    ev.preventDefault();
    // Heavy DOM work to build accordion body;
    setTimeout(() => { /* Manually expand the accordion; */ }, 0);
}

谢谢你。

javascript bootstrap-5
1个回答
0
投票

在这种情况下需要使用这个 JavaScript 函数:

button.addEventListener('click', () => {
setTimeout(() => {
      const bootstrapCollapse = new bootstrap.Collapse(collapseOne, {
        toggle: true // open the accordion
      });
    }, 5000); // delays for 5 seconds, you can edit this
});

由于它会延迟 5 秒打开手风琴,因此有必要消除打开按钮的类,使其成为一个简单的按钮,而不是具有 bootstrapp 功能的按钮。

这是例子:

  const button = document.getElementById('openButton1');
  const collapseOne = document.getElementById('collapseOne');
button.addEventListener('click', () => {
    setTimeout(() => {
      const bootstrapCollapse = new bootstrap.Collapse(collapseOne, {
        toggle: true
      });
    }, 5000);
  });
  const buttonTwo = document.getElementById('openButton2');
  const collapseTwo = document.getElementById('collapseTwo');
buttonTwo.addEventListener('click', () => {
    setTimeout(() => {
      const bootstrapCollapse = new bootstrap.Collapse(collapseTwo, {
        toggle: true
      });
    }, 5000);
  });
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css" rel="stylesheet"/>
<div class="accordion" id="accordionExample">

  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed accordion-button1" type="button" id="openButton1">
        Accordion Item #1
      </button>
    </h2>
    <div id="collapseOne" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the second item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
  <div class="accordion-item">
    <h2 class="accordion-header">
      <button class="accordion-button collapsed" type="button" id="openButton2">
        Accordion Item #2
      </button>
    </h2>
    <div id="collapseTwo" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
      <div class="accordion-body">
        <strong>This is the third item's accordion body.</strong> It is hidden by default, until the collapse plugin adds the appropriate classes that we use to style each element. These classes control the overall appearance, as well as the showing and hiding via CSS transitions. You can modify any of this with custom CSS or overriding our default variables. It's also worth noting that just about any HTML can go within the <code>.accordion-body</code>, though the transition does limit overflow.
      </div>
    </div>
  </div>
</div>

© www.soinside.com 2019 - 2024. All rights reserved.