简体   繁体   English

单击关闭jQuery手风琴活动面板

[英]Make jQuery accordion active panel close on click

I'm using the Simple jQuery Accordion from CSS-Tricks, and I want to know how to make the active panel close on click of the dt link. 我正在使用CSS-Tricks的Simple jQuery手风琴,我想知道如何在单击dt链接时关闭活动面板。 The application I'm using it for is as a menu in a sidebar that cannot scroll vertically, so if the content of the panel is too large, it can push the other panels out of reach of the user. 我正在使用它的应用程序是作为无法垂直滚动的侧边栏中的菜单,因此,如果面板的内容太大,它将其他面板推到用户无法触及的位置。

Secondarily, I would like to be able to set the accordion to have all items expanded based on a media query. 其次,我希望能够将手风琴设置为基于媒体查询扩展所有项目。 Can someone tell me if this would even be possible without major changes to the plugin? 有人可以告诉我,如果不对插件进行重大更改,是否有可能实现?

Here is the jQuery: 这是jQuery:

(function($) {

  var allPanels = $('.accordion > dd').hide();

  $('.accordion > dt > a').click(function() {
    allPanels.slideUp();
    $(this).parent().next().slideDown();
    return false;
  });

})(jQuery);

And the HTML: 和HTML:

<dl class="accordion">

<dt><a href="">Panel 1</a></dt>
<dd>Pellentesque fermentum dolor. Aliquam quam lectus, facilisis auctor, ultrices ut, elementum vulputate, nunc.</dd>

<dt><a href="">Panel 2</a></dt>
<dd>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci. Aenean dignissim pellentesque felis.</dd>

<dt><a href="">Panel 3</a></dt>
<dd>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus hendrerit. Pellentesque aliquet nibh nec urna. In nisi neque, aliquet vel, dapibus id, mattis vel, nisi. Sed pretium, ligula sollicitudin laoreet viverra, tortor libero sodales leo, eget blandit nunc tortor eu nibh. Nullam mollis. Ut justo. Suspendisse potenti.</dd>

</dl>

And the CSS: 和CSS:

.accordion {
  margin: 50px;
}

.accordian dt, dd {
  padding: 10px;
  border: 1px solid black;
  border-bottom: 0; 
}

.accordian dt:last-of-type, .accordian dd:last-of-type {
  border-bottom: 1px solid black; 
}

.accordian a {
  display: block;
  color: black;
  font-weight: bold;
}

.accordian dd {
  border-top: 0; 
  font-size: 12px; 
}
.accordian dd:last-of-type {
  border-top: 1px solid white;
  position: relative;
  top: -1px;
}

Check if the panel is open before you do anything, and then either close that one panel, or close the others and open the new panel. 在执行任何操作之前,请检查面板是否已打开,然后关闭一个面板,或关闭其他面板并打开新面板。

(function($) {

  var allPanels = $('.accordion > dd').hide();

  $('.accordion > dt > a').click(function() {
    var $targetPanel = $(this).parent().next();
      if ($targetPanel.is(':visible')) {
        $targetPanel.slideUp();
      } else {          
        allPanels.slideUp();
        $targetPanel.slideDown();
      }
    return false;
  });

})(jQuery);
(function($) {

  var allPanels = $('.accordion > dd');
  allPanels.hide();

  $('.accordion > dt > a').click(function(e) {
    e.preventDefault;
    allPanels.slideUp();
    $(this).parent().next().slideDown();
  });

})(jQuery);

I haven't test this yet, but makes more sense to me. 我还没有测试,但是对我来说更有意义。

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

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