2016-08-26 5 views
4

これはこれほど難しいとは思えませんが、私は数時間作業していて、この点まで私を連れてきました。私はどこにいなければならない。アコーディオンの可視性に基づいたトグルクラス

FAQページにはアコーディオンがあります。この部分は動作します。私が1つを開くと、他の開いているドロップダウンが閉じられます。

私の問題は、私のヘッダーの右側に、素晴らしいフォントを使用して引っ張っている矢印があることです。

は、ここに私のjQueryのです:

$('.faq_page').find('.faq_header').click(function(){ 
 

 
     //Expand or collapse this panel 
 
     $(this).next().slideToggle('fast'); 
 

 
     $(this).find('.faq_control i').toggleClass('fa-caret-down fa-caret-up'); 
 
     
 

 
     //Hide the other panels 
 
     $(".faq_body").not($(this).next()).slideUp('fast'); 
 

 
    });
.width_container:after, 
 
.width_container_small:after { 
 
    visibility: hidden; 
 
    display: block; 
 
    font-size: 0; 
 
    content: " "; 
 
    clear: both; 
 
    height: 0; 
 
} 
 

 
.width_container, 
 
.width_container_small { display: block; } 
 
/* start commented backslash hack \*/ 
 
* html .width_container, 
 
* html .width_container_small { height: 1%; } 
 
.width_container, 
 
.width_container_small { display: block; } 
 
/* close commented backslash hack */ 
 

 
/********** FAQ Page **********/ 
 

 
.faq_page { 
 
\t margin: 80px 0 80px; 
 
} 
 

 
.faq_box { 
 
\t -webkit-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
-moz-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
-ms-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
-o-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 

 
\t margin-bottom: 40px; 
 
} 
 

 
.faq_header { 
 
\t background: #231f20; 
 
\t padding: 20px 0; 
 
} 
 

 
.faq_title { 
 
\t float: left; 
 
\t width: 95%; 
 
} 
 

 
.faq_title h2 { 
 
\t color: #fff; 
 
\t margin: 0; 
 
\t font-size: 22px; 
 
\t font-weight: 300; 
 
\t text-align: left; 
 
\t font-family: 'Open Sans', sans-serif; 
 
\t padding-left: 20px; 
 
} 
 

 
.faq_control { 
 
\t float: right; 
 
\t width: 5%; 
 
} 
 

 
.faq_control i { 
 
\t color: #fff; 
 
\t font-size: 40px; 
 
\t line-height: 40px; 
 

 
} 
 

 
.faq_body { 
 
\t background: #e8e8e8; 
 
\t padding: 20px; 
 
\t display: none; 
 
} 
 

 
.faq_active { 
 
\t display: block; 
 
} 
 

 
.faq_body p { 
 
\t margin: 0; 
 
\t font-size: 16px; 
 
\t line-height: 36px; 
 
\t letter-spacing: 2px; 
 
\t font-weight: 400; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous"> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class="faq_page"> 
 
\t \t <div class="width_container_small"> 
 

 
\t \t \t <div class="faq_box"> 
 
\t \t \t \t <div class="faq_header"> 
 
\t \t \t \t \t <div class="width_container"> 
 
\t \t \t \t \t \t <div class="faq_title"> 
 
\t \t \t \t \t \t \t <h2>This is a Frequently Aksed Question?</h2> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="faq_control"> 
 
\t \t \t \t \t \t \t <i class="fa fa-caret-up" aria-hidden="true"></i> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="faq_body faq_active"> 
 
\t \t \t \t \t <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="faq_box"> 
 
\t \t \t \t <div class="faq_header"> 
 
\t \t \t \t \t <div class="width_container"> 
 
\t \t \t \t \t \t <div class="faq_title"> 
 
\t \t \t \t \t \t \t <h2>This is a Frequently Aksed Question?</h2> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="faq_control"> 
 
\t \t \t \t \t \t \t <i class="fa fa-caret-down" aria-hidden="true"></i> 
 

 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="faq_body"> 
 
\t \t \t \t \t <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="faq_box"> 
 
\t \t \t \t <div class="faq_header"> 
 
\t \t \t \t \t <div class="width_container"> 
 
\t \t \t \t \t \t <div class="faq_title"> 
 
\t \t \t \t \t \t \t <h2>This is a Frequently Aksed Question?</h2> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="faq_control"> 
 
\t \t \t \t \t \t \t <i class="fa fa-caret-down" aria-hidden="true"></i> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="faq_body"> 
 
\t \t \t \t \t <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t </div> 
 
\t </section>

それはそれが必要のように私の矢印クラスを切り替えることができます単一のボックスをクリックします。コンテンツが表示されているときは上、下が表示されていないときは下が表示されます。しかし、1つのボックスを開いて次のボックスをクリックすると、最初のボックスは閉じられますが、そのボックスはクリックされていないため、矢印は変更されません。

この結果、ボックスはスクリプトによって閉じられます(直接クリックせずに)、矢印の方向に並んでいません。

ボックスが開いている場合は矢印をポイントし、ボックスが閉じている場合はピリオドをピリオドにしたいと思います。クラス名がボックスが可視かどうかに依存するようにする方法はわかりません。私が試したいくつかのことが、この作品を作りましたが、私がクリックしていたボックスのためだけでした。

答えて

1

jsを少し更新し、アコーディオンのトグル機能をjQuery関数に分割しました。ここでのアイデアは、イベントとトグルを実際に行うコードを分離して、ダイレクトクリックの操作なしでアコーディオンを操作できるようにすることです。

クリックハンドラでは、私はトグル機能を呼び出して、他の開いたアコーディオン(「.faq_body:visible」と表示されています)を見つけて、それらのトグル機能を呼び出して閉じます。正しいアイコン。

$('.faq_page').find('.faq_header').click(function(){ 
 
     $(this).toggleAccordion(); 
 

 
     //Hide the other panels 
 
     //:visible finds all open panels, .prev finds their headers, 
 
     //.not ensures we aren't closing the one we just opened 
 
     $(".faq_body:visible").prev('.faq_header').not($(this)).toggleAccordion(); 
 
    }); 
 

 
$.fn.toggleAccordion = function() { 
 
    //Expand or collapse this panel 
 
    $(this).next().slideToggle('fast'); 
 

 
    $(this).find('.faq_control i').toggleClass('fa-caret-down fa-caret-up'); 
 
}
.width_container:after, 
 
.width_container_small:after { 
 
    visibility: hidden; 
 
    display: block; 
 
    font-size: 0; 
 
    content: " "; 
 
    clear: both; 
 
    height: 0; 
 
} 
 

 
.width_container, 
 
.width_container_small { display: block; } 
 
/* start commented backslash hack \*/ 
 
* html .width_container, 
 
* html .width_container_small { height: 1%; } 
 
.width_container, 
 
.width_container_small { display: block; } 
 
/* close commented backslash hack */ 
 

 
/********** FAQ Page **********/ 
 

 
.faq_page { 
 
\t margin: 80px 0 80px; 
 
} 
 

 
.faq_box { 
 
\t -webkit-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
-moz-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
-ms-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
-o-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 

 
\t margin-bottom: 40px; 
 
} 
 

 
.faq_header { 
 
\t background: #231f20; 
 
\t padding: 20px 0; 
 
} 
 

 
.faq_title { 
 
\t float: left; 
 
\t width: 95%; 
 
} 
 

 
.faq_title h2 { 
 
\t color: #fff; 
 
\t margin: 0; 
 
\t font-size: 22px; 
 
\t font-weight: 300; 
 
\t text-align: left; 
 
\t font-family: 'Open Sans', sans-serif; 
 
\t padding-left: 20px; 
 
} 
 

 
.faq_control { 
 
\t float: right; 
 
\t width: 5%; 
 
} 
 

 
.faq_control i { 
 
\t color: #fff; 
 
\t font-size: 40px; 
 
\t line-height: 40px; 
 

 
} 
 

 
.faq_body { 
 
\t background: #e8e8e8; 
 
\t padding: 20px; 
 
\t display: none; 
 
} 
 

 
.faq_active { 
 
\t display: block; 
 
} 
 

 
.faq_body p { 
 
\t margin: 0; 
 
\t font-size: 16px; 
 
\t line-height: 36px; 
 
\t letter-spacing: 2px; 
 
\t font-weight: 400; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous"> 
 

 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<section class="faq_page"> 
 
\t \t <div class="width_container_small"> 
 

 
\t \t \t <div class="faq_box"> 
 
\t \t \t \t <div class="faq_header"> 
 
\t \t \t \t \t <div class="width_container"> 
 
\t \t \t \t \t \t <div class="faq_title"> 
 
\t \t \t \t \t \t \t <h2>This is a Frequently Aksed Question?</h2> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="faq_control"> 
 
\t \t \t \t \t \t \t <i class="fa fa-caret-up" aria-hidden="true"></i> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="faq_body faq_active"> 
 
\t \t \t \t \t <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="faq_box"> 
 
\t \t \t \t <div class="faq_header"> 
 
\t \t \t \t \t <div class="width_container"> 
 
\t \t \t \t \t \t <div class="faq_title"> 
 
\t \t \t \t \t \t \t <h2>This is a Frequently Aksed Question?</h2> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="faq_control"> 
 
\t \t \t \t \t \t \t <i class="fa fa-caret-down" aria-hidden="true"></i> 
 

 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="faq_body"> 
 
\t \t \t \t \t <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t \t <div class="faq_box"> 
 
\t \t \t \t <div class="faq_header"> 
 
\t \t \t \t \t <div class="width_container"> 
 
\t \t \t \t \t \t <div class="faq_title"> 
 
\t \t \t \t \t \t \t <h2>This is a Frequently Aksed Question?</h2> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="faq_control"> 
 
\t \t \t \t \t \t \t <i class="fa fa-caret-down" aria-hidden="true"></i> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t </div> 
 
\t \t \t \t </div> 
 
\t \t \t \t <div class="faq_body"> 
 
\t \t \t \t \t <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 

 
\t \t </div> 
 
\t </section>

+0

おかげで、これは本当によく働きます。 – JSum

1

私の提案は、に基づいています。

  • 現在の "faq_box"
  • を取得し、残りの "faq_box"
  • は、他のよくある質問ボックスを非表示に設定を取得します現在の "faq_box" togglの正しいfa-caret-down
  • Eの可視性とFA-キャレット

$(function() { 
 
    $('.faq_page').find('.faq_header').on('click', function(e) { 
 
    var currFaqBox = $(this).closest('.faq_box'); 
 
    var otherFaqBox = currFaqBox.siblings().not(currFaqBox); 
 

 
    // hide the other faq box 
 
    otherFaqBox.find('.faq_body').slideUp('fast'); 
 
    otherFaqBox.find('.faq_control i').addClass('fa-caret-down').removeClass('fa-caret-up'); 
 

 
    // toggle current faq box 
 
    currFaqBox.find('.faq_body').slideToggle('fast'); 
 
    currFaqBox.find('.faq_control i').toggleClass('fa-caret-down fa-caret-up'); 
 
    }); 
 
});
.width_container:after, 
 
.width_container_small:after { 
 
    visibility: hidden; 
 
    display: block; 
 
    font-size: 0; 
 
    content: " "; 
 
    clear: both; 
 
    height: 0; 
 
} 
 

 
.width_container, 
 
.width_container_small { display: block; } 
 
/* start commented backslash hack \*/ 
 
* html .width_container, 
 
* html .width_container_small { height: 1%; } 
 
.width_container, 
 
.width_container_small { display: block; } 
 
/* close commented backslash hack */ 
 

 
/********** FAQ Page **********/ 
 

 
.faq_page { 
 
    margin: 80px 0 80px; 
 
} 
 

 
.faq_box { 
 
    -webkit-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
    -moz-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
    box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
    -ms-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 
    -o-box-shadow: 0px 0px 10px 0px rgba(35,31,32,1); 
 

 
    margin-bottom: 40px; 
 
} 
 

 
.faq_header { 
 
    background: #231f20; 
 
    padding: 20px 0; 
 
} 
 

 
.faq_title { 
 
    float: left; 
 
    width: 95%; 
 
} 
 

 
.faq_title h2 { 
 
    color: #fff; 
 
    margin: 0; 
 
    font-size: 22px; 
 
    font-weight: 300; 
 
    text-align: left; 
 
    font-family: 'Open Sans', sans-serif; 
 
    padding-left: 20px; 
 
} 
 

 
.faq_control { 
 
    float: right; 
 
    width: 5%; 
 
} 
 

 
.faq_control i { 
 
    color: #fff; 
 
    font-size: 40px; 
 
    line-height: 40px; 
 

 
} 
 

 
.faq_body { 
 
    background: #e8e8e8; 
 
    padding: 20px; 
 
    display: none; 
 
} 
 

 
.faq_active { 
 
    display: block; 
 
} 
 

 
.faq_body p { 
 
    margin: 0; 
 
    font-size: 16px; 
 
    line-height: 36px; 
 
    letter-spacing: 2px; 
 
    font-weight: 400; 
 
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-T8Gy5hrqNKT+hzMclPo118YTQO6cYprQmhrYwIiQ/3axmI1hQomh7Ud2hPOy8SP1" crossorigin="anonymous"> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 

 
<section class="faq_page"> 
 
    <div class="width_container_small"> 
 

 
     <div class="faq_box"> 
 
      <div class="faq_header"> 
 
       <div class="width_container"> 
 
        <div class="faq_title"> 
 
         <h2>This is a Frequently Aksed Question?</h2> 
 
        </div> 
 
        <div class="faq_control"> 
 
         <i class="fa fa-caret-up" aria-hidden="true"></i> 
 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="faq_body faq_active"> 
 
       <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
      </div> 
 
     </div> 
 

 
     <div class="faq_box"> 
 
      <div class="faq_header"> 
 
       <div class="width_container"> 
 
        <div class="faq_title"> 
 
         <h2>This is a Frequently Aksed Question?</h2> 
 
        </div> 
 
        <div class="faq_control"> 
 
         <i class="fa fa-caret-down" aria-hidden="true"></i> 
 

 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="faq_body"> 
 
       <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
      </div> 
 
     </div> 
 

 
     <div class="faq_box"> 
 
      <div class="faq_header"> 
 
       <div class="width_container"> 
 
        <div class="faq_title"> 
 
         <h2>This is a Frequently Aksed Question?</h2> 
 
        </div> 
 
        <div class="faq_control"> 
 
         <i class="fa fa-caret-down" aria-hidden="true"></i> 
 
        </div> 
 
       </div> 
 
      </div> 
 
      <div class="faq_body"> 
 
       <p>orem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque mollis vitae sapien quis vestibulum. Integer mollis erat et enim placerat ullamcorper. Vivamus malesuada blandit justo, ut scelerisque nibh tempor vel. Cras mattis, erat eget facilisis suscipit, est magna elementum ex, quis facilisis erat tortor et metus. Vestibulum ante This is a</p> 
 
      </div> 
 
     </div> 
 

 
    </div> 
 
</section>

関連する問題