简体   繁体   English

默认情况下,如何使所有手风琴在浏览器中打开并在移动视图中关闭?

[英]How to make all accordion open in browser and close in mobile view by default?

I'm editing accordions according to my requirements, as the accordions are closed by default.我正在根据我的要求编辑手风琴,因为默认情况下手风琴是关闭的。 I want it to be opened by default in desktop browser and keep closed on mobile browser.我希望它在桌面浏览器中默认打开并在移动浏览器上保持关闭。 HTML, CSS and JS code are bellow. HTML、CSS 和 JS 代码如下。 I'm sure you are clear about my requirements.我相信你很清楚我的要求。

HTML code: HTML代码:

<div class="rightSideAccor">
  <button class="loginAccordion">Section 1</button>
  <div class="panel">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  </div>
</div>

Style sheet CSS:样式表 CSS:

.rightSideAccor {
    float: left;
    width: 356px;
    height: auto;
    margin-top: 120px;
    padding: 5px;
}
.loginAccordion {
    opacity: .8;
  background-color: #1e1b1a;
  color: #fff;
  cursor: pointer;
  padding: 18px;
  width: 100%;
  border: none;
  text-align: left;
  outline: none;
  font-size: 15px;
  transition: 0.4s;
}
.active, .accordion:hover {
  background-color: #1e1b1a;
}
.loginAccordion:after {
  content: '\02C5';
  color: #777;
  font-weight: bold;
  float: right;
  margin-left: 5px;
}
.active:after {
  content: "\02C4";
}
.panel {
  opacity: .8;
  padding: 0 18px;
  background-color: #1e1b1a;
  color: #fff;
  max-height: 0;
  overflow: hidden;
  transition: max-height 0.2s ease-out;
}

JS Code: JS代码:

<script>
  var acc = document.getElementsByClassName("loginAccordion");
  var i;

  for (i = 0; i < acc.length; i++) {
    acc[i].addEventListener("click", function() {
      this.classList.toggle("active");
      var panel = this.nextElementSibling;
      if (panel.style.maxHeight){
        panel.style.maxHeight = null;
      } else {
        panel.style.maxHeight = panel.scrollHeight + "px";
      } 
    });
  }
</script>

Here I write a solution.我在这里写一个解决方案。 You need to add a class name to your accordion.您需要为您的手风琴添加一个类名。 I named the class accordion and trigger the click event by this class.我将类命名为手风琴并由此类触发单击事件。 Here I assume max mobile width is 500px .这里我假设最大移动宽度为500px You can change it.你可以改变它。

 var acc = document.getElementsByClassName("loginAccordion"); var i; for (i = 0; i < acc.length; i++) { acc[i].addEventListener("click", function() { this.classList.toggle("active"); var panel = this.nextElementSibling; if (panel.style.maxHeight) { panel.style.maxHeight = null; } else { panel.style.maxHeight = panel.scrollHeight + "px"; } }); } //suppose the max width of mobile is 500px if (screen.width > 500) { var accordion = document.getElementsByClassName('accordion'); Object.keys(accordion).forEach(el => { accordion[el].click(); }); }
 .rightSideAccor { float: left; width: 356px; height: auto; margin-top: 20px; padding: 5px; } .loginAccordion { opacity: .8; background-color: #1e1b1a; color: #fff; cursor: pointer; padding: 18px; width: 100%; border: none; text-align: left; outline: none; font-size: 15px; transition: 0.4s; } .active, .accordion:hover { background-color: #1e1b1a; } .loginAccordion:after { content: '\\02C5'; color: #777; font-weight: bold; float: right; margin-left: 5px; } .active:after { content: "\\02C4"; } .panel { opacity: .8; padding: 0 18px; background-color: #1e1b1a; color: #fff; max-height: 0; overflow: hidden; transition: max-height 0.2s ease-out; }
 <div class="rightSideAccor"> <button class="accordion loginAccordion">Section 1</button> <div class="panel"> <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM