2016-07-05 7 views
0

折りたたみエキスパンドエフェクトのガイダンスが必要です。私のコードは以下の通りです。Jquery collapseエキスパンドエフェクト

<div class="buttons"> 
<a class="km-collapse-forms expand-all" href="#">Expand All</a> 
<a class="km-collapse-forms collapse-all hide" href="#">Collapse All</a>      
</div> 


$args = array(...); 

$comments_query = new WP_Comment_Query; 
$comments  = $comments_query->query($args); 

foreach ($comments as $comment) { 

<a class="respond-to-messages expand-form" href="#<?php echo $comment->comment_ID; ?>">Respond to message</a> 
<a class="respond-to-messages collapse-form hide" href="#<?php echo $comment->comment_ID; ?>">Hide form</a> 

<div id="comment-<?php echo $comment->comment_ID; ?>" class="comment-respond-form post-id-<?php echo $comment->comment_post_ID; ?> hide"> 
    <table> 
     <tr> 
      <form id="custom-comments-form" action="" method="post"> 

       //form fields 

       <button class="uk-button" type="submit">Send</button> 
      </form> 
     </tr> 
    </table> 
    </div> 

} 

jqueryの:私はそれは同様に動作するようにしたいと思いながら

jQuery(function ($) { 
    $(document).ready(function(){ 

     $(".km-collapse-forms.expand-all").show();  

     $('.km-collapse-forms.expand-all').on('click',function(){ 

      // Expand/Collapse All 
      $(".km-collapse-forms.expand-all").addClass("hide"); 
      $(".km-collapse-forms.collapse-all").removeClass("hide"); 

      // Change text respond/hide form 
      $(".respond-to-messages.expand-form").addClass("hide"); 
      $(".respond-to-messages.collapse-form").removeClass("hide"); 

      $(".comment-respond-form").slideDown("slow"); 

     });  

     $('.km-collapse-forms.collapse-all').on('click',function(){ 

      $(".km-collapse-forms.collapse-all").addClass("hide"); 
      $(".km-collapse-forms.expand-all").removeClass("hide"); 

      $(".respond-to-messages.collapse-form").addClass("hide"); 
      $(".respond-to-messages.expand-form").removeClass("hide"); 

      $(".comment-respond-form").slideUp("slow"); 

     }); 



     // respond/hide form text :: single comment 
     // This is where I'm having trouble with 
     $('.respond-to-messages.expand-form').on('click',function(){ 

      $(this).next(".respond-to-messages.collapse-form").removeClass("hide"); 
      $(this).next(".respond-to-messages.expand-form").addClass("hide"); 

      $(this).next(".comment-respond-form").removeClass("hide"); 
      $(this).next(".comment-respond-form").slideDown("slow"); 

     }); 


     $('.respond-to-messages.collapse-form').on('click',function(){  
      $(this).next(".respond-to-messages.collapse-form, .comment-respond-form").addClass("hide"); 
      $(this).next(".respond-to-messages.expand-form").removeClass("hide"); 

      $(this).prev(".comment-respond-form").addClass("hide"); 
      $(this).next(".comment-respond-form").slideUp("slow"); 

     }); 



    }); 
}); 

意図したとおりにexpand/collapse all作業が、各個別のRespond to comment/hide formはしていません。

ご迷惑をおかけして申し訳ありません。

+0

は、あなたのコードでJsFiddleを作成することができますしてください。 – andybeli

+0

[link] https://jsfiddle.net/kiarashi/5jkdnzt4/6/ – kiarashi

答えて

0

コードがちょっと乱雑でした。私はすぐにそれをきれいにしようとしましたが、それをより良くするためにいくつかのことがあります。 Javascriptの「ベストプラクティス」をご覧ください。

ヒント::

  • 不要なHTML要素
  • の関数を使用し
  • キャッシュjQueryのセレクタ
  • の多くは、JavaScriptのデザインパターン

作業溶液を考えますのjsFiddle

JS:

jQuery(function ($) { 
    $(document).ready(function() { 

     var $listElem = $('.accordion-elem'); 
     function showAll() { 
      $listElem.each(function() { 
      $(this).removeClass('hiddenElements'); 
      $(this).find('.respond-to-messages').text('Hide form'); 
      $(this).find('.comment-respond-form').slideDown("slow"); 
      }); 
     } 

     function hideAll() { 
      $listElem.each(function() { 
      $(this).addClass('hiddenElements'); 
      $(this).find('.respond-to-messages').text('Respond to message'); 
      $(this).find('.comment-respond-form').slideUp("slow"); 
      }); 
     } 

     $('.all-button').on('click',function() { 
       console.log("trigger"); 
      if ($(this).hasClass('expanded')) { 
       hideAll(); 
       $(this).removeClass('expanded'); 
       $(this).text('Expand All'); 
      } else { 
       showAll(); 
       $(this).addClass('expanded'); 
       $(this).text('Collapse All'); 
      } 
     }); 

     $('.respond-to-messages').on('click', function() { 
      var $targetLi = $(this).closest('li'); 
      if($targetLi.hasClass('hiddenElements')) { 
      //Show 
      $targetLi.removeClass('hiddenElements'); 
      $targetLi.find('.respond-to-messages').text('Hide form'); 
      $targetLi.find('.comment-respond-form').slideDown("slow"); 
      } else { 
      //hide 
      $targetLi.addClass('hiddenElements'); 
      $targetLi.find('.respond-to-messages').text('Respond to message'); 
      $targetLi.find('.comment-respond-form').slideUp("slow"); 
      } 
     }); 


    }); 
}); 

HTML:

<div class="buttons"> 
    <a class="all-button expanded" href="#">Collapse All</a>    
</div> 

<ul> 
<li class="accordion-elem"> Lorem ipsum<br /><br /> 
    <a class="respond-to-messages" href="#">Hide form</a> 
    <div id="comment-5" class="comment-respond-form"> 
<table> 
     <tr> 
      <form id="custom-comments-form" action="" method="post"> 
       <textarea class="fes-cmt-body" name="newcomment_body" cols="50" rows="8"></textarea> 
       <button class="uk-button" type="submit">Send</button> 
      </form> 
     </tr> 
     </table></div> 
    </li> 

    <li class="accordion-elem"> Lorem ipsum<br /><br /> 
     <a class="respond-to-messages" href="#">Hide form</a> 
     <div id="comment-6" class="comment-respond-form"> 
     <table> 
     <tr> 
      <form id="custom-comments-form" action="" method="post"> 
       <textarea class="fes-cmt-body" name="newcomment_body" cols="50" rows="8"></textarea> 
       <button class="uk-button" type="submit">Send</button> 
      </form> 
     </tr> 
     </table></div> 
    </li> 
    <li class="accordion-elem"> Lorem ipsum<br /><br /> 
     <a class="respond-to-messages" href="#">Hide form</a> 
     <div id="comment-7" class="comment-respond-form"> 
     <table> 
     <tr> 
      <form id="custom-comments-form" action="" method="post"> 
       <textarea class="fes-cmt-body" name="newcomment_body" cols="50" rows="8"></textarea> 
       <button class="uk-button" type="submit">Send</button> 
      </form> 
     </tr> 
     </table></div> 
    </li> 
</ul> 
+0

私はjqueryを初めて使うので、私のコードは初心者レベルです^^; '$(this).text'はWordpressでテキストを翻訳できる必要があるため使用できません。だから意図的に 'addClass/removeClass'を使ったのです。実際のコードが' ul/li'ではなくテーブルであるため、 '$ targetLi'も使用できません。 JsFiddleの中に追加して、わかりやすくしましたが、代わりにもっと複雑に思えました。 ^^; – kiarashi

+0

私のコードを使用してロジックを理解し、どこで間違いを起こしたかを確認できます。マイナーチェンジでは、あなたの仕事を得ることができます。 – andybeli

+0

私はすでにそれに乗っています。 :D – kiarashi