javascript - 当元素的前一个兄弟元素具有特定类时,如何设置元素的样式?

标签 javascript css

我想在打开 Accordion button.accordion.is-open 时在 Accordion accordion-content 类的内容中添加边框。可以通过纯 CSS 实现吗?

setTimeout(function(){
    
const accordionBtns = document.querySelectorAll(".accordion");

accordionBtns.forEach((accordion) => {
  accordion.onclick = function () {
    this.classList.toggle("is-open");

    let content = this.nextElementSibling;

    if (content.style.maxHeight) {
      //this is if the accordion is open
      content.style.maxHeight = null;
    } else {
      //if the accordion is currently closed
      content.style.maxHeight = content.scrollHeight + "px";
    }
  };
});

}, 200); 
button.accordion {
  width: 100%;
  background-color: whitesmoke;
  border: 1px solid #333;
  outline: none;
  text-align: left;
  padding: 15px 20px;
  font-size: 18px;
  font-weight: 500;
  color: #333;
  cursor: pointer;
  transition: background-color 0.2s linear;
}

button.accordion:after {
  font-family: "Font Awesome 5 Free";
  content: "\f067";
  font-size: 18px;
  float: right;
  font-weight: 900;
}

button.accordion.is-open:after {
  content: "\f068";
  font-weight: 900;
}

button.accordion:hover,
button.accordion.is-open {
  background-color: #ddd;
}

.accordion-content {
  background-color: white;
  padding: 0 20px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-in-out;
}
<button class="accordion">Show Solution</button>
<div class="accordion-content">
<p>
kmax, kmin = k.max(), k.min()<br />
k_new = (k - kmin)/(kmax - kmin)
</p>
</div>

最佳答案

您只需使用基于开放 Accordion 类的同级选择器。您可能需要添加透明边框和过渡以防止跳跃。

setTimeout(function() {

  const accordionBtns = document.querySelectorAll(".accordion");

  accordionBtns.forEach((accordion) => {
    accordion.onclick = function() {
      this.classList.toggle("is-open");

      let content = this.nextElementSibling;

      if (content.style.maxHeight) {
        //this is if the accordion is open
        content.style.maxHeight = null;
      } else {
        //if the accordion is currently closed
        content.style.maxHeight = content.scrollHeight + "px";
      }
    };
  });

}, 200);
button.accordion {
  width: 100%;
  background-color: whitesmoke;
  border: 1px solid #333;
  outline: none;
  text-align: left;
  padding: 15px 20px;
  font-size: 18px;
  font-weight: 500;
  color: #333;
  cursor: pointer;
  transition: background-color 0.2s linear;
}

button.accordion:after {
  font-family: "Font Awesome 5 Free";
  content: "\f067";
  font-size: 18px;
  float: right;
  font-weight: 900;
}

button.accordion.is-open:after {
  content: "\f068";
  font-weight: 900;
}

button.accordion:hover,
button.accordion.is-open {
  background-color: #ddd;
}

.accordion-content {
  background-color: white;
  padding: 0 20px;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-in-out;
  border: 3px solid transparent; /* prevents position shift */
  transition: border 0.3s; /* softens border change */
}

.accordion.is-open + .accordion-content { /* selects following sibling element */
  border: 3px solid pink;
}
<button class="accordion">Show Solution</button>
<div class="accordion-content">
  <p>
    kmax, kmin = k.max(), k.min()<br /> k_new = (k - kmin)/(kmax - kmin)
  </p>
</div>

关于javascript - 当元素的前一个兄弟元素具有特定类时,如何设置元素的样式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70567346/

相关文章:

javascript - 如何在React Native Expo中使用Realm?

javascript - 通过 JS 函数发布输入字段值

html - CSS 文件不会在 React JS 中加载

html - Thymeleaf - 如何根据互斥条件应用两种(或更多)样式

css - IE 7 中的溢出问题

html - 在 bootstrap css 中删除 div 后的额外空间

javascript - 在函数中使用 Angular 表达式

javascript - 有没有办法获取表单元素及其相关值

javascript - 在 D3 中过渡文本

css - 如何在 CSS 中制作带有固定页眉和页脚的 2 列布局?