简体   繁体   English

Jquery Accordion with Font Awesome Toggle

[英]Jquery Accordion with Font Awesome Toggle

I've been looking through the boards, and haven't found a solution to help with my issue at hand. 我一直在寻找董事会,并没有找到解决方案来帮助解决我的问题。 I have an accordion feature that features font-awesome. 我有一个手风琴功能,功能很棒。 I want to be able to toggle between the font awesome classes fa-angle-up/fa-angle-down when the accordion button is clicked. 我希望能够在单击手风琴按钮时在字体真棒类fa-angle-up / fa-angle-down之间切换。 This is simple enough, but where I'm running into a problem is that the first div in the accordion should be open on page load, while the others are closed. 这很简单,但是我遇到的问题是手风琴中的第一个div应该在页面加载时打开,而其他的则关闭。 The code below is allowing the divs to toggle correctly, when one opens the other closes, but the font awesome toggle isn't firing correctly. 下面的代码允许div正确切换,当一个打开另一个关闭,但字体真棒切换没有正确触发。 Right now when I click on a button the first div closes/font awesome toggles correctly, but the button of the div to open, and the rest of the buttons in the accordion, font awesome icon all toggle class. 现在,当我点击按钮时,第一个div关闭/字体真棒切换正确,但div的按钮打开,手风琴中的其余按钮,字体真棒图标全部切换类。

I'm pretty novice with jquery, so all help is appreciated. 我是jquery的新手,所以所有的帮助都表示赞赏。 Like I said I've looked through the boards to see if any other post related to accordions/font awesome toggles could help me, but everything I tried wasn't working, however I possibly could have been implementing it wrong since I'm not the best with jquery. 就像我说的那样,我已经通过电路板查看是否有任何其他与手风琴/字体真棒切换有关的帖子可以帮助我,但我尝试的一切都没有用,但是我可能会错误地实现它,因为我不是最好用jquery。

Demo here of what I currently have: http://jsbin.com/zaqocu/1/ 在这里演示我目前所拥有的: http//jsbin.com/zaqocu/1/

Check out toggleClass() . 查看toggleClass()

For example, 例如,

jquery jQuery的

$('mything').click(function(){
    $(this).toggleClass('myclassOne myclassTwo');
});

So when you click, it will remove the current class you have, then add the new one. 因此,当您单击时,它将删除您当前的类,然后添加新的类。 Click again and it will flip-flop. 再次点击它会触发。

Using your jsbin example. 使用你的jsbin示例。 I just cleared the classes. 我刚刚清理了课程。 I'm not a fan of using toggles, though I'm sure they'd work this way. 我不喜欢使用切换,但我确信他们会以这种方式工作。 Basically, any time you click on a headline, default ALL of the arrows to down, then if you are showing a headline, switch that class to up. 基本上,每当您点击标题时,默认所有箭头都会向下,然后如果您显示标题,请将该类切换为标题。

Here's the bin: http://jsbin.com/xuzorokaho/1/ 这是垃圾箱: http//jsbin.com/xuzorokaho/1/

I believe if I used toggle for this, it would toggle all arrows up or down depending on their last state, which could be down and supposed to stay down. 我相信如果我曾经为此进行过切换,它会根据最后的状态向上或向下切换所有箭头,这可能会下降并且应该保持不变。

$(document).ready(function(){
    $(".accordion-toggle").click(function() {
        $(".accordion-toggle i").removeClass('fa-angle-up');
        $(".accordion-toggle i").addClass('fa-angle-down');
        if($(this).next("div").is(":visible")){
            $(this).next("div").slideUp("slow");
        } else {
            $(this).find('i').removeClass('fa-angle-down')
            $(this).find('i').addClass('fa-angle-up');
            $(".accordion-content").slideUp("slow");
            $(this).next("div").slideToggle("slow");
        }
    });
});

here is a working code : 这是一个有效的代码:

$(document).ready(function(){
$(".accordion-toggle").click(function() {


      if($(this).next("div").is(":visible")){
        $(this).next("div").slideUp("slow");
        $(this).children().toggleClass("fa-angle-up fa-angle-down");
      } else {
        $(".accordion-toggle").next("div").slideUp("slow");
        $(".accordion-toggle i").attr("class", "fa fa-angle-down");
        $(this).next("div").slideDown("slow");
        $(this).children().toggleClass("fa-angle-up fa-angle-down");
      }
});
});

You need to close all the tab before open the one who is clicked. 在打开单击的选项卡之前,您需要关闭所有选项卡。

Have a good one. 祝你有个好的一天。

http://jsbin.com/cizoziqiji/2/ http://jsbin.com/cizoziqiji/2/

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

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