【问题标题】:Accordion takes 2 clicks to initiate + icon rotationAccordion 需要 2 次点击来启动 + 图标旋转
【发布时间】:2021-11-09 05:21:44
【问题描述】:

我拼凑了一些我在我的网站上为 FAQ 手风琴找到的代码。 我不确定如何通过 1 次单击使标题展开,因为它目前需要 2 次。 我还希望图标在展开/折叠时旋转。 它目前只是旋转开/关悬停。 我包括一个我目前拥有的简短示例。

任何帮助将不胜感激,谢谢。

  $('.js-question').on('click', function(e) {
    var $answer = $(this).next(),
      actveClass = 'active',
      isActive = $answer.hasClass(actveClass);
    $('.answer').slideUp().addClass(actveClass);
    if (isActive) {
      $answer.toggleClass(actveClass);
      $answer.slideToggle();
    }
  });
.faqContainer {
    width: 100%;
    margin-right: auto;
    margin-left: auto;
    height: 100%;
}

.question {
    font-family: Arial MT Pro!important;
    font-size: 14px;
    color: #000;
    letter-spacing: 3px;
    font-weight: 300;
    cursor: pointer;
    overflow: hidden;
    display: table-cell;
    width: 100%;
    vertical-align: middle;
}
    
.answer {
    font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
    font-size: 10px;
    line-height: 1.7;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #333;
    overflow: hidden;
    display: none;
    margin: 10px 0 15px
}
    
.answer a{
    font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
    font-size: 10px;
    line-height: 1.7;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #333!important;
}
    
.answer:hover a{
    font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
    font-size: 10px;
    line-height: 1.7;
    text-transform: uppercase;
    color: #ababab!important;
    letter-spacing: 1px;
    transition: all 0.4s ease-in-out 0s
}

.faqContainer hr {
    opacity: 0;
}

.bracket-button .outer {
    letter-spacing: 0px;
    font-family: 'Arial MT Pro';
    position: absolute;
    margin-top: 0px;
    margin-left: 8px;
}
    
.bracket-button .inner {
    display: inline-block;
    background: url(https://cdn.shopify.com/s/files/1/1547/5153/t/167/assets/cross.svg?v=1631342608) center no-repeat;
    background-size: cover;
    height: 10px;
    width: 10px;
    transform: rotate(45deg);
    transition: all 0.5s cubic-bezier(0.215,  0.61,  0.355,  1);
    padding: 0px;
    -webkit-backface-visibility: hidden;
}
    
.bracket-button:hover .inner {
    transform: rotate(135deg);
    transition: all 0.5s cubic-bezier(0.215,  0.61,  0.355,  1);
    padding: 0px;
}
    
.bracket-button.active .inner {
    transform: rotate(0deg);
    background: url(https://cdn.shopify.com/s/files/1/1547/5153/t/167/assets/cross.svg?v=1631342608) center no-repeat;
    background-size: cover;
    background-position-y: 0px;
}
    
.bracket-button.active:hover .inner {
    transform: rotate(90deg);
}
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
  <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
<div class="faqContainer">
<p class="question js-question bracket-button">LOGISTICS<span class="outer"><span class="inner"></span></span></p>
<div class="answer">Pre-order items will ship roughly 2-3 weeks from the end date specified in the product description.<br />This can sometimes take longer due to print shop schedules/holidays.<br />All in stock orders are shipped within 48 hours of purchase, usually less.<br />Orders placed Friday evenings will typically ship the following Monday.<br />Most domestic orders are delivered within a week.<br />Please allow additional time in transit for delivery.<br /> If your order contains pre-order and in stock items,<br />your order will ship when all items are on-hand.<br />Please place separate orders if you need the in stock item(s) sooner.</div>
<hr />
<p class="question js-question bracket-button">DISCOUNTS<span class="outer"><span class="inner"></span></span></p>
<div class="answer">Domestic orders $75+ receive free shipping. <br /> Canada orders $125+ receive free shipping. <br /> Everywhere else orders $150+ receive free shipping.</div>
<hr />
<p class="question js-question bracket-button">CANCELLATIONS<span class="outer"><span class="inner"></span></span></p>
<div class="answer">If you canceled an order, expect to see the funds back in your account<br />within 2-3 business days, depending on your bank.</div>
<hr />
</div>

【问题讨论】:

    标签: javascript html css accordion rotatetransform


    【解决方案1】:

    您正在向您的职能部门发送相互矛盾的订单。在这里$('.answer').slideUp().addClass(actveClass);,您正在向上滑动所有答案并将活动类添加到所有答案中(我相信您想删除它)。

    然后,您询问目标答案是否具有活动类(答案显然是肯定的,无论如何,因为您之前已将其添加到所有元素中)。

    这个条件if (isActive) {,即使你之前的代码是正确的,也完全没有必要。您可以做什么从除目标答案之外的所有答案中删除活动类,然后仅切换单击的答案,您已经在此处定位$answer = $(this).next()

    所以工作代码是:

    $('.js-question').on('click', function(e) {
        var $answer = $(this).next(),
          actveClass = 'active';    
        $('.answer').not($answer).slideUp().removeClass(actveClass); /* remove instead of add and apply in every answer except the clicked one*/
        /* remove unnecessary conditional */  
          $answer.toggleClass(actveClass);
          $answer.slideToggle();
      });
    .faqContainer {
        width: 100%;
        margin-right: auto;
        margin-left: auto;
        height: 100%;
    }
    
    .question {
        font-family: Arial MT Pro!important;
        font-size: 14px;
        color: #000;
        letter-spacing: 3px;
        font-weight: 300;
        cursor: pointer;
        overflow: hidden;
        display: table-cell;
        width: 100%;
        vertical-align: middle;
    }
        
    .answer {
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 10px;
        line-height: 1.7;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: #333;
        overflow: hidden;
        display: none;
        margin: 10px 0 15px
    }
        
    .answer a{
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 10px;
        line-height: 1.7;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: #333!important;
    }
        
    .answer:hover a{
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 10px;
        line-height: 1.7;
        text-transform: uppercase;
        color: #ababab!important;
        letter-spacing: 1px;
        transition: all 0.4s ease-in-out 0s
    }
    
    .faqContainer hr {
        opacity: 0;
    }
    
    .bracket-button .outer {
        letter-spacing: 0px;
        font-family: 'Arial MT Pro';
        position: absolute;
        margin-top: 0px;
        margin-left: 8px;
    }
        
    .bracket-button .inner {
        display: inline-block;
        background: url(https://cdn.shopify.com/s/files/1/1547/5153/t/167/assets/cross.svg?v=1631342608) center no-repeat;
        background-size: cover;
        height: 10px;
        width: 10px;
        transform: rotate(45deg);
        transition: all 0.5s cubic-bezier(0.215,  0.61,  0.355,  1);
        padding: 0px;
        -webkit-backface-visibility: hidden;
    }
        
    .bracket-button:hover .inner {
        transform: rotate(135deg);
        transition: all 0.5s cubic-bezier(0.215,  0.61,  0.355,  1);
        padding: 0px;
    }
        
    .bracket-button.active .inner {
        transform: rotate(0deg);
        background: url(https://cdn.shopify.com/s/files/1/1547/5153/t/167/assets/cross.svg?v=1631342608) center no-repeat;
        background-size: cover;
        background-position-y: 0px;
    }
        
    .bracket-button.active:hover .inner {
        transform: rotate(90deg);
    }
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
      <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
    <div class="faqContainer">
    <p class="question js-question bracket-button">LOGISTICS<span class="outer"><span class="inner"></span></span></p>
    <div class="answer">Pre-order items will ship roughly 2-3 weeks from the end date specified in the product description.<br />This can sometimes take longer due to print shop schedules/holidays.<br />All in stock orders are shipped within 48 hours of purchase, usually less.<br />Orders placed Friday evenings will typically ship the following Monday.<br />Most domestic orders are delivered within a week.<br />Please allow additional time in transit for delivery.<br /> If your order contains pre-order and in stock items,<br />your order will ship when all items are on-hand.<br />Please place separate orders if you need the in stock item(s) sooner.</div>
    <hr />
    <p class="question js-question bracket-button">DISCOUNTS<span class="outer"><span class="inner"></span></span></p>
    <div class="answer">Domestic orders $75+ receive free shipping. <br /> Canada orders $125+ receive free shipping. <br /> Everywhere else orders $150+ receive free shipping.</div>
    <hr />
    <p class="question js-question bracket-button">CANCELLATIONS<span class="outer"><span class="inner"></span></span></p>
    <div class="answer">If you canceled an order, expect to see the funds back in your account<br />within 2-3 business days, depending on your bank.</div>
    <hr />
    </div>

    更新以使图标正常工作

    .answer 的方法相同。您从除单击按钮之外的所有按钮中删除活动类,然后切换单击按钮类。

    $('.js-question').on('click', function(e) {
      var $answer = $(this).next(),
        actveClass = 'active';
      $('.bracket-button').not($(this)).removeClass(actveClass);
      $('.answer').not($answer).slideUp().removeClass(actveClass);
      $(this).toggleClass(actveClass);
      $answer.toggleClass(actveClass);
      $answer.slideToggle();
    });
    .faqContainer {
        width: 100%;
        margin-right: auto;
        margin-left: auto;
        height: 100%;
    }
    
    .question {
        font-family: Arial MT Pro!important;
        font-size: 14px;
        color: #000;
        letter-spacing: 3px;
        font-weight: 300;
        cursor: pointer;
        overflow: hidden;
        display: table-cell;
        width: 100%;
        vertical-align: middle;
    }
        
    .answer {
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 10px;
        line-height: 1.7;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: #333;
        overflow: hidden;
        display: none;
        margin: 10px 0 15px
    }
        
    .answer a{
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 10px;
        line-height: 1.7;
        text-transform: uppercase;
        letter-spacing: 1px;
        color: #333!important;
    }
        
    .answer:hover a{
        font-family: Arial,"Helvetica Neue",Helvetica,sans-serif;
        font-size: 10px;
        line-height: 1.7;
        text-transform: uppercase;
        color: #ababab!important;
        letter-spacing: 1px;
        transition: all 0.4s ease-in-out 0s
    }
    
    .faqContainer hr {
        opacity: 0;
    }
    
    .bracket-button .outer {
        letter-spacing: 0px;
        font-family: 'Arial MT Pro';
        position: absolute;
        margin-top: 0px;
        margin-left: 8px;
    }
        
    .bracket-button .inner {
        display: inline-block;
        background: url(https://cdn.shopify.com/s/files/1/1547/5153/t/167/assets/cross.svg?v=1631342608) center no-repeat;
        background-size: cover;
        height: 10px;
        width: 10px;
        transform: rotate(45deg);
        transition: all 0.5s cubic-bezier(0.215,  0.61,  0.355,  1);
        padding: 0px;
        -webkit-backface-visibility: hidden;
    }
        
    .bracket-button:hover .inner {
        transform: rotate(135deg);
        transition: all 0.5s cubic-bezier(0.215,  0.61,  0.355,  1);
        padding: 0px;
    }
        
    .bracket-button.active .inner {
        transform: rotate(0deg);
        background: url(https://cdn.shopify.com/s/files/1/1547/5153/t/167/assets/cross.svg?v=1631342608) center no-repeat;
        background-size: cover;
        background-position-y: 0px;
    }
        
    .bracket-button.active:hover .inner {
        transform: rotate(90deg);
    }
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.1/jquery-ui.min.js"></script>
    <div class="faqContainer">
      <p class="question js-question bracket-button">LOGISTICS<span class="outer"><span class="inner"></span></span>
      </p>
      <div class="answer">Pre-order items will ship roughly 2-3 weeks from the end date specified in the product description.<br />This can sometimes take longer due to print shop schedules/holidays.<br />All in stock orders are shipped within 48 hours of purchase, usually less.<br
        />Orders placed Friday evenings will typically ship the following Monday.<br />Most domestic orders are delivered within a week.<br />Please allow additional time in transit for delivery.<br /> If your order contains pre-order and in stock items,<br
        />your order will ship when all items are on-hand.<br />Please place separate orders if you need the in stock item(s) sooner.</div>
      <hr />
      <p class="question js-question bracket-button">DISCOUNTS<span class="outer"><span class="inner"></span></span>
      </p>
      <div class="answer">Domestic orders $75+ receive free shipping. <br /> Canada orders $125+ receive free shipping. <br /> Everywhere else orders $150+ receive free shipping.</div>
      <hr />
      <p class="question js-question bracket-button">CANCELLATIONS<span class="outer"><span class="inner"></span></span>
      </p>
      <div class="answer">If you canceled an order, expect to see the funds back in your account<br />within 2-3 business days, depending on your bank.</div>
      <hr />
    </div>

    【讨论】:

    • 您好,感谢您的回复。正如原始代码所示,我实际上希望能够打开和关闭每个选项卡。正如您所指出的,您的解决方案修复了必须单击两次选项卡才能激活它,但它们无法关闭。结合上述内容,标题右侧的图标将在打开/关闭时旋转(而不仅仅是在悬停时)。我不确定 CSS 缺少什么来解决这部分问题。
    • 对不起,我没有理解你。我已经编辑了小提琴以按预期工作。您所要做的就是将更改应用于除单击的.answers 之外的所有$('.answer').not($answer).slideUp().removeClass(actveClass);。其余的解释仍然适用。 ;)
    • 好的,所以您的图标的问题是您没有将活动类添加到按钮中,而是将其添加到答案中,因此.bracket-button.active 不适用于 jQuery 函数就像现在一样。您还需要切换按钮类以正确使用您的 CSS。让我看看。 ;)
    • 好的,我也添加了一个带有按钮类切换的新小提琴。不知道是不是你想要达到的效果,但是现在jquery已经准备好了,你可以通过CSS来调整它。
    猜你喜欢
    • 2016-12-23
    • 1970-01-01
    • 2015-09-11
    • 2013-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    相关资源
    最近更新 更多