jquery手风琴开/关

时间:2016-02-25 14:44:18

标签: javascript jquery

我正在使用具有特殊功能的jquery手风琴。在99%的情况下,它的功能正常。我有一个用例,我需要在页面加载时打开顶部/第一个手风琴,同时保持功能(在这种情况下加/减相应的状态打开或关闭使用font-awesome)。

我可以通过硬编码类“open”和“display:block;”来打开手风琴。通过这样做,它搞砸了我的字体真棒状态。

JSFiddle

HTML

<div class="accordion-container">
  <a class="accordion-toggle" href="#"><span class="toggle-head"><h2 class="t-h2">Web Ready Lorem Ipsum</h2>

    </span><span class="toggle-icon"><i class="fa fa-plus"></i></span></a>
  <div class="accordion-content">
    <p>The images below provide examples of safe sleep environments that follow the safe sleep strategies explained in Safe to Sleep<sup>®</sup> materials.</p>
    <p>These low-resolution images are available for your convenience. If you choose to use the images, you are agreeing to: </p>
    <ul class="sts-bullet">
      <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
      <li>Duis malesuada purus sit amet tellus semper tempus.</li>
      <li>Etiam iaculis est ac erat mollis, vitae vulputate ante sagittis.</li>
      <li>Mauris interdum nulla interdum libero hendrerit interdum.</li>
      <li>Cras aliquet arcu vitae mattis congue.Cras aliquet arcu vitae mattis congue.</li>
      <li>Vivamus bibendum arcu vel enim luctus, eu dignissim erat efficitur.</li>
    </ul>
  </div>
</div>
<div class="accordion-container">
  <a class="accordion-toggle" href="#"><span class="toggle-head"><h2 class="t-h2">Web-Ready Lorem Ipsum is simply dummy text 2</h2>

    </span><span class="toggle-icon"><i class="fa fa-plus"></i></span></a>
  <div class="accordion-content">
    <p>The images below provide examples of safe sleep environments that follow the safe sleep strategies explained in Safe to Sleep<sup>®</sup> materials.</p>
    <p>These low-resolution images are available for your convenience. If you choose to use the images, you are agreeing to: </p>
    <ul class="sts-bullet">
      <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
      <li>Duis malesuada purus sit amet tellus semper tempus.</li>
      <li>Etiam iaculis est ac erat mollis, vitae vulputate ante sagittis.</li>
      <li>Mauris interdum nulla interdum libero hendrerit interdum.</li>
      <li>Cras aliquet arcu vitae mattis congue.Cras aliquet arcu vitae mattis congue.</li>
      <li>Vivamus bibendum arcu vel enim luctus, eu dignissim erat efficitur.</li>
    </ul>
  </div>
</div>
<div class="accordion-container">
  <a class="accordion-toggle" href="#"><span class="toggle-head"><h2 class="t-h2">Web-Ready Lorem Ipsum is simply dummy text 3</h2>

    </span><span class="toggle-icon"><i class="fa fa-plus"></i></span></a>
  <div class="accordion-content">
    <p>The images below provide examples of safe sleep environments that follow the safe sleep strategies explained in Safe to Sleep<sup>®</sup> materials.</p>
    <p>These low-resolution images are available for your convenience. If you choose to use the images, you are agreeing to: </p>
    <ul class="sts-bullet">
      <li>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</li>
      <li>Duis malesuada purus sit amet tellus semper tempus.</li>
      <li>Etiam iaculis est ac erat mollis, vitae vulputate ante sagittis.</li>
      <li>Mauris interdum nulla interdum libero hendrerit interdum.</li>
      <li>Cras aliquet arcu vitae mattis congue.Cras aliquet arcu vitae mattis congue.</li>
      <li>Vivamus bibendum arcu vel enim luctus, eu dignissim erat efficitur.</li>
    </ul>
  </div>
</div>

CSS

/*ACCORDION*/
.accordion-container {
    width: 100%;
    margin-top: 1em;
    /*clear: both;*/
}

.toggle-head {
    display: block;
    max-width: 95%;
    font-weight: 700;
}

.accordion-toggle {
    position: relative;
    display: block;
    padding: 10px;
    font-size: 1em;
    font-weight: 400;
    background: #f3f3f3;
    color: #000 !important;
    text-decoration: none !important;
}

.accordion-toggle.open {
    background: #f3f3f3;
    color: #fff;
}

.accordion-toggle:hover {
    background: #E8E8E8;
}

.accordion-toggle span.toggle-icon {
    position: absolute;
    right: .5em;
    font-size: 1.5em;
    color: #65308a;
    top: .5em;
}

.accordion-content {
    display: none;
    padding: 20px;
    overflow: auto;
    background-color: transparent;
}

的jQuery

$(document).ready(function() {
  $('.accordion-toggle').on('click', function(event) {
    event.preventDefault();
    // create accordion variables
    var accordion = $(this);
    var accordionContent = accordion.next('.accordion-content');
    var accordionToggleIcon = $(this).children('.toggle-icon');

    // toggle accordion link open class
    accordion.toggleClass("open");
    // toggle accordion content
    accordionContent.slideToggle(250);

    // change plus/minus icon
    if (accordion.hasClass("open")) {
      accordionToggleIcon.html("<i class='fa fa-minus'></i>");
    } else {
      accordionToggleIcon.html("<i class='fa fa-plus'></i>");
    }

  });
});

3 个答案:

答案 0 :(得分:1)

您可以强制点击,以便重复使用您的功能。为你要打开的手风琴添加一个id ...

  <a class="accordion-toggle" id="ac1" .... > </a>

然后在脚本的末尾添加

$('#ac1').click();

您还可以使用trigger()方法触发单击。使用trigger()而不是click()将消除不明确的调用。

$('#ac1').trigger('click'); 

答案 1 :(得分:1)

只需点击第一个元素:

    $(document).ready(function() {
      var toggles = $('.accordion-toggle')
      toggles.on('click', function(event) {
           event.preventDefault();
           // create accordion variables
           var accordion = $(this);
           var accordionContent = accordion.next('.accordion-content');
           //// ...............

      });
      // Fire click! /////////////////
      toggles.first().trigger('click');
      /////////////////////////////////
   });

答案 2 :(得分:0)

enter image description here&GT;

  

class =“toggle-head”&gt; Web Ready Lorem Ipsum -

     

如上所示命名将要生效的所有元素,然后在

的末尾执行此操作
{{1}}

$(document).ready(function(){

检查图像以获取小提琴的详细信息

complete image of page