javascript - 链接的 Accordion 主体

标签 javascript jquery html css

通过单击面板打开 Accordion 的内容主体。这通常是最好的。我现在想做的是更改此行为,以便仅通过单击其中一个面板(或其他任何地方)中包含的链接来显示内容主体。希望这是有道理的。

首先:这可能吗?

这是现在的代码:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>

.accordion {
  background-color: #fff;
  color: #444;
  cursor: pointer;
  width: 103%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
  margin: -5px;
}

.bg {
 width: 100%;
}

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

.panel {
  padding: 50px 0px;
  display: none;
  width: 100%;
  background-color: white;
  overflow: hidden;
  border-style: groove;
}

.accordion.active+div {
  display: block
}

.column {
    float: left;
    width: 20%;
    padding: 10px;
}


/* Clear floats after the columns */
.row:after {
    content: "";
    display: table;
    clear: both;
}


    

</style>
</head>
<body>

<button class="accordion"><div class="parent">Only the following link should open the content panel: link_1</button>

<div class="panel">
<p>Content 1</p>
</div>





<button class="accordion">Only the following link should open the content panel: link_2</button>


<div class="panel">
<p>Content 2</p>
</div>

<button class="accordion">Only the following link should open the content panel: link_3</button>

<div class="panel">
  <p>Content 3</p>
</div>

<button class="accordion">Only the following link should open the content panel: link_4</button>

<div class="panel">
  <p>Content 4</p>
</div>

<script>

function scrollElmVert(el,num) { // to scroll up use a negative number
  var re=/html$/i;
  while(!re.test(el.tagName) && (1 > el.scrollTop)) el=el.parentNode;
  if(0 < el.scrollTop) el.scrollTop += num;
}

var acc = document.getElementsByClassName("accordion");
var i;
var open = null;

for (i = 0; i < acc.length; i++) {
  acc[i].addEventListener("click", function() {
    if (open == this) {
      open.classList.toggle("active");
      open = null;
    } else {
      if (open != null) {
        open.classList.toggle("active");
      }
      this.classList.toggle("active");
      open = this;
      //Scroll to clicked element
      open.scrollIntoView();
      scrollElmVert(open,-68);
    }
  });
}
</script>

</body>
</html>

最佳答案

对 css 的改动很少,但总的来说它是一个 html 问题。您将需要有两个不同的元素或至少不同的类来区分打开 Accordion 的元素和不打开 Accordion 的元素

希望这就是您要找的。如果需要,很乐意解释或提供更好的解决方案。

<!DOCTYPE html>
<html>

<head>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <style>
    .accordion {
      background-color: #fff;
      color: #444;
      cursor: pointer;
      border: none;
      text-align: left;
      outline: none;
      font-size: 15px;
      transition: 0.4s;
      margin: -5px;
      display: inline;
    }
    
    .not-accordion {
      color: #444;
      font-size: 15px;
      display: inline-block;
    }
    
    .bg {
      width: 100%;
    }
    
    .active,
    .accordion:hover {
      background-color: #fff;
    }
    
    .panel {
      padding: 50px 0px;
      display: none;
      width: 100%;
      background-color: white;
      overflow: hidden;
      border-style: groove;
    }
    
    .accordion.active+div {
      display: block
    }
    
    .column {
      float: left;
      width: 20%;
      padding: 10px;
    }
    /* Clear floats after the columns */
    
    .row:after {
      content: "";
      display: table;
      clear: both;
    }
  </style>
</head>

<body>

  <div class="parent">

    <p class='not-accordion'>Only the following link should open the content panel:</p> <button class="accordion">link_1</button>

    <div class="panel">
      <p>Content 1</p>
    </div>




    <p class='not-accordion'>Only the following link should open the content panel:</p> <button class="accordion">link_2</button>


    <div class="panel">
      <p>Content 2</p>
    </div>
    <p class='not-accordion'>Only the following link should open the content panel:</p> <button class="accordion">link_3</button>

    <div class="panel">
      <p>Content 3</p>
    </div>

    <p class='not-accordion'>Only the following link should open the content panel:</p> <button class="accordion">link_4</button>
    <div class="panel">
      <p>Content 4</p>
    </div>

    <script>
      function scrollElmVert(el, num) { // to scroll up use a negative number
        var re = /html$/i;
        while (!re.test(el.tagName) && (1 > el.scrollTop)) el = el.parentNode;
        if (0 < el.scrollTop) el.scrollTop += num;
      }

      var acc = document.getElementsByClassName("accordion");
      var i;
      var open = null;

      for (i = 0; i < acc.length; i++) {
        acc[i].addEventListener("click", function() {
          if (open == this) {
            open.classList.toggle("active");
            open = null;
          } else {
            if (open != null) {
              open.classList.toggle("active");
            }
            this.classList.toggle("active");
            open = this;
            //Scroll to clicked element
            open.scrollIntoView();
            scrollElmVert(open, -68);
          }
        });
      }
    </script>

</body>

</html>

关于javascript - 链接的 Accordion 主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48993676/

相关文章:

javascript - Django - 使用 Javascript 将 ID 传递到 url 模板标记中

javascript - 在工作时间显示/隐藏 div,与 UTC 混合?

javascript - 判断字符串长度是否为9个字符且全是数字

javascript - 单击全部展开/折叠时指向正确方向的箭头

javascript - casper.waitForSelector,超时和错误处理

javascript - 按顺序对数组项执行函数

javascript - 通过事件监听器获取元素

javascript - 如何使用 fileupload jQuery 在文件不是图像类型时立即显示错误消息

php - 无法在脚本函数中传递 php 变量

html - 到边界半径的 SVG 路径