我在使用JavaScript时遇到手风琴问题

时间:2018-09-12 14:46:12

标签: javascript html css

我的手风琴有问题。

问题:一切正常,但我有一个问题。

当我发现除 1个closeIt 类以外的其他事件时,我正在使用click eventListener来清除所有手风琴触发器*(按钮)*。

问题是,如果我不关闭活动的手风琴,它将不会添加活动的类,因为它会清除所有内容。

function showAcord(n) {
  var $bro = "magic-" + n,
    $par = document.getElementById($bro);

  var $j = document.getElementsByClassName('acord-active'),
    i = $j.length;

  while (i--) {
    $j[i].classList.remove("acord-active");
  }

  $par.classList.toggle("acord-active");

  // Close It Condição
  var $myClass = document.getElementsByClassName('closeIt');

  var $selector = "btn-" + n,
    $btn = document.getElementById($selector);

  if ($btn.classList.contains("closeIt")) {
    var $j = document.getElementsByClassName('acord-active'),
      i = $j.length;

    while (i--) {
      $j[i].classList.remove("acord-active");
    }

    var $checkIcons = document.getElementsByClassName('closeIt'),
      y = $checkIcons.length;

    while (y--) {
      $checkIcons[y].classList.remove("closeIt");
    }

  } else {
    $btn.classList.toggle("closeIt");
  }
}

// EventListener
(function() {
  window.addEventListener('click', clean);

  function clean() {
    var $cleaner = document.getElementsByClassName('closeIt');
    var $loop = $cleaner.length;

    if ($loop > 1) {
      while ($loop--) {
        $cleaner[$loop].classList.remove("closeIt");
      }
    }
  }
})()
.openIt:before {
  content: "\f078";
  font-family: fontawesome;
  margin-right: 5px;
  color: #fff;
  font-size: 20px;
}

.closeIt:before {
  content: "\f077";
  font-family: fontawesome;
  margin-right: 5px;
  font-size: 20px;
  color: #f6a61d;
}

.acord-off {
  z-index: -1;
  transform: scaleY(0);
  position: absolute !important;
}

.acord-active {
  z-index: 1;
  transform: scaleY(1);
  position: inherit !important;
}
<div class="container">
  <article>
    <div class="col-sm-12 pt-3 mb-5 aligncenter">
      <h2>Ibbca Accordeon</h2>
      <div class="container">
        <div class="row">
          <div class="col-sm-6">
            <a class="btn btn-info openIt" name="button" onclick="showAcord(1)" id="btn-1">Mostrar acord 1</a>
          </div>
          <div class="col-sm-6">
            <a class="btn btn-info openIt" name="button" onclick="showAcord(2)" id="btn-2">Mostrar acord 2</a>
          </div>
        </div>
      </div>

      <div id="magic-1" class="acord-1 acord-off">
        <p>Texto II</p>
      </div>
      <div id="magic-2" class="acord-2 acord-off">
        <p>Texto III</p>
      </div>
    </div>
  </article>
</div>

2 个答案:

答案 0 :(得分:0)

我已经更新了代码段。 如果要在同一按钮的第二次单击上保持手风琴打开,请从javascript函数中删除以下代码。

if($par.classList.contains("acord-active")){
    $par.classList.remove("acord-active");
    $btn.classList.remove("closeIt");

    return;
}

function showAcord(n) {
  var $par = document.getElementById("magic-" + n);

  var $btn = document.getElementById("btn-" + n);

//REMOVE BELOW CONDITION IF YOU WANT TO KEEP THE ACCORDION OPEN ON SAME BUTTON'S SECOND CLICK.
    if($par.classList.contains("acord-active")){
      $par.classList.remove("acord-active");
      $btn.classList.remove("closeIt");
      
      return;
    }


  var $j = document.getElementsByClassName('acord-active'),
    i = $j.length;

  while (i--) {
    $j[i].classList.remove("acord-active");
  }

  $par.classList.toggle("acord-active");

  // Close It Condição
  var $myClass = document.getElementsByClassName('closeIt');
   var $myClassi = $myClass.length;

  while ($myClassi--) {
    $myClass[$myClassi].classList.remove("closeIt");
  }

  if ($btn.classList.contains("closeIt")) {     
    $btn.classList.remove("closeIt");
  } else {
    $btn.classList.add("closeIt");
  }
}
.openIt:before {
  content: "\f078";
  font-family: fontawesome;
  margin-right: 5px;
  color: #fff;
  font-size: 20px;
}

.closeIt:before {
  content: "\f077";
  font-family: fontawesome;
  margin-right: 5px;
  font-size: 20px;
  color: #f6a61d;
}

.acord-off {
  z-index: -1;
  transform: scaleY(0);
  position: absolute !important;
}

.acord-active {
  z-index: 1;
  transform: scaleY(1);
  position: inherit !important;
}
<div class="container">
  <article>
    <div class="col-sm-12 pt-3 mb-5 aligncenter">
      <h2>Ibbca Accordeon</h2>
      <div class="container">
        <div class="row">
          <div class="col-sm-6">
            <a class="btn btn-info openIt" name="button" onclick="showAcord(1)" id="btn-1">Mostrar acord 1</a>
          </div>
          <div class="col-sm-6">
            <a class="btn btn-info openIt" name="button" onclick="showAcord(2)" id="btn-2">Mostrar acord 2</a>
          </div>
        </div>
      </div>

      <div id="magic-1" class="acord-1 acord-off">
        <p>Texto II</p>
      </div>
      <div id="magic-2" class="acord-2 acord-off">
        <p>Texto III</p>
      </div>
    </div>
  </article>
</div>

答案 1 :(得分:0)

嘿,您要注意事件的传播,您每次单击时都将附加功能clean附加到窗口。如果单击btn,则此事件将冒泡至窗口,并且它将运行clean。您可以避免这种情况:

如果您添加onclick="showAcord(event, 1)

然后

function showAcord(event, n) { event.stopPropagation(); ...

这样,您可以避免在单击btn时进行清洁