Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

accordion.js 2.8 KiB

3 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /**
  2. * Accordion-folding functionality.
  3. *
  4. * Markup with the appropriate classes will be automatically hidden,
  5. * with one section opening at a time when its title is clicked.
  6. * Use the following markup structure for accordion behavior:
  7. *
  8. * <div class="accordion-container">
  9. * <div class="accordion-section open">
  10. * <h3 class="accordion-section-title"></h3>
  11. * <div class="accordion-section-content">
  12. * </div>
  13. * </div>
  14. * <div class="accordion-section">
  15. * <h3 class="accordion-section-title"></h3>
  16. * <div class="accordion-section-content">
  17. * </div>
  18. * </div>
  19. * <div class="accordion-section">
  20. * <h3 class="accordion-section-title"></h3>
  21. * <div class="accordion-section-content">
  22. * </div>
  23. * </div>
  24. * </div>
  25. *
  26. * Note that any appropriate tags may be used, as long as the above classes are present.
  27. *
  28. * @since 3.6.0.
  29. */
  30. ( function( $ ){
  31. $( document ).ready( function () {
  32. // Expand/Collapse accordion sections on click.
  33. $( '.accordion-container' ).on( 'click keydown', '.accordion-section-title', function( e ) {
  34. if ( e.type === 'keydown' && 13 !== e.which ) { // "return" key
  35. return;
  36. }
  37. e.preventDefault(); // Keep this AFTER the key filter above
  38. accordionSwitch( $( this ) );
  39. });
  40. });
  41. /**
  42. * Close the current accordion section and open a new one.
  43. *
  44. * @param {Object} el Title element of the accordion section to toggle.
  45. * @since 3.6.0
  46. */
  47. function accordionSwitch ( el ) {
  48. var section = el.closest( '.accordion-section' ),
  49. sectionToggleControl = section.find( '[aria-expanded]' ).first(),
  50. container = section.closest( '.accordion-container' ),
  51. siblings = container.find( '.open' ),
  52. siblingsToggleControl = siblings.find( '[aria-expanded]' ).first(),
  53. content = section.find( '.accordion-section-content' );
  54. // This section has no content and cannot be expanded.
  55. if ( section.hasClass( 'cannot-expand' ) ) {
  56. return;
  57. }
  58. // Add a class to the container to let us know something is happening inside.
  59. // This helps in cases such as hiding a scrollbar while animations are executing.
  60. container.addClass( 'opening' );
  61. if ( section.hasClass( 'open' ) ) {
  62. section.toggleClass( 'open' );
  63. content.toggle( true ).slideToggle( 150 );
  64. } else {
  65. siblingsToggleControl.attr( 'aria-expanded', 'false' );
  66. siblings.removeClass( 'open' );
  67. siblings.find( '.accordion-section-content' ).show().slideUp( 150 );
  68. content.toggle( false ).slideToggle( 150 );
  69. section.toggleClass( 'open' );
  70. }
  71. // We have to wait for the animations to finish
  72. setTimeout(function(){
  73. container.removeClass( 'opening' );
  74. }, 150);
  75. // If there's an element with an aria-expanded attribute, assume it's a toggle control and toggle the aria-expanded value.
  76. if ( sectionToggleControl ) {
  77. sectionToggleControl.attr( 'aria-expanded', String( sectionToggleControl.attr( 'aria-expanded' ) === 'false' ) );
  78. }
  79. }
  80. })(jQuery);