如何折叠由div组成的jQuery手风琴菜单?

时间:2015-11-08 23:22:09

标签: javascript jquery html animation accordion

我见过许多类似的问题,但仍未找到解决问题的方法。

我为菜单中的每个项目制作了一个带有重复jQuery代码的手风琴菜单。我想知道用更少的jQuery代码是否有更有效的方式。

最好不要更改HTML标记

    $('.what-we-do-toggle1').click(function() {
        $('.what-we-do-text1').animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
        $('.what-we-do-text2').slideUp(200);
        $('.what-we-do-text3').slideUp(200);
        $('.what-we-do-text4').slideUp(200);
        $('.what-we-do-text5').slideUp(200);
        $('.what-we-do-text6').slideUp(200);
    });    
    $('.what-we-do-toggle2').click(function() {
        $('.what-we-do-text2').animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
        $('.what-we-do-text1').slideUp(200);
        $('.what-we-do-text3').slideUp(200);
        $('.what-we-do-text4').slideUp(200);
        $('.what-we-do-text5').slideUp(200);
        $('.what-we-do-text6').slideUp(200);
    });    
    $('.what-we-do-toggle3').click(function() {
        $('.what-we-do-text3').animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
        $('.what-we-do-text1').slideUp(200);
        $('.what-we-do-text2').slideUp(200);
        $('.what-we-do-text4').slideUp(200);
        $('.what-we-do-text5').slideUp(200);
        $('.what-we-do-text6').slideUp(200);
    });    
    $('.what-we-do-toggle4').click(function() {
        $('.what-we-do-text4').animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
        $('.what-we-do-text1').slideUp(200);
        $('.what-we-do-text2').slideUp(200);
        $('.what-we-do-text3').slideUp(200);
        $('.what-we-do-text5').slideUp(200);
        $('.what-we-do-text6').slideUp(200);
    });    
    $('.what-we-do-toggle5').click(function() {
        $('.what-we-do-text5').animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
        $('.what-we-do-text1').slideUp(200);
        $('.what-we-do-text2').slideUp(200);
        $('.what-we-do-text3').slideUp(200);
        $('.what-we-do-text4').slideUp(200);
        $('.what-we-do-text6').slideUp(200);
    });    
    $('.what-we-do-toggle6').click(function() {
        $('.what-we-do-text6').animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
        $('.what-we-do-text1').slideUp(200);
        $('.what-we-do-text2').slideUp(200);
        $('.what-we-do-text3').slideUp(200);
        $('.what-we-do-text4').slideUp(200);
        $('.what-we-do-text5').slideUp(200);
    });

http://codepen.io/berry807/pen/KdGeVG

3 个答案:

答案 0 :(得分:2)

不改变标记

$('#what-we-do .row > div').click(function() {
    $('#what-we-do .row > div p').stop().slideUp(); // slide all up
    $(this).find('p').stop().slideToggle(); // slide this one down
});

http://codepen.io/anon/pen/mezKBB

答案 1 :(得分:0)

这样的事情:

jQuery(document).ready(function($){
    $('#what-we-do .row > div').on('click', function(){
        var $this = $(this);
        var $content = $this.children('p');
        if(!$this.hasClass('toggled'){
            $('#what-we-do .row > .toggled > p').slideUp();
            $('#what-we-do .row > .toggled').removeClass('toggled');

            $this.addClass('toggled');
            $content.slideDown();

        }else{
            $this.removeClass('toggled');
            $content.slideUp();
        }
    });
});

请注意我没有测试过,但它应该这样工作。

答案 2 :(得分:0)

没有chaning html-structure:

   $('#what-we-do .container .row div').on('click', function() {
      for (var i = 1; i <= 6; i++) {
        $(this).find($('.what-we-do-text' + i)).animate({
            height: "toggle",
            opacity: "toggle"
        }, 200);
      } 
   });

http://codepen.io/anon/pen/YyJvEV