Accordion / Collapsible - 图1

Accordion / Collapsible

Accordion Layout

Common Accordion layout could be treat as the following:

  1. <div class="accordion-list">
  2. <div class="accordion-item">
  3. <div class="accordion-item-toggle">...</div>
  4. <div class="accordion-item-content">...</div>
  5. </div>
  6. <div class="accordion-item accordion-item-opened">
  7. <div class="accordion-item-toggle">...</div>
  8. <div class="accordion-item-content">...</div>
  9. </div>
  10. <div class="accordion-item">
  11. <div class="accordion-item-toggle">...</div>
  12. <div class="accordion-item-content">...</div>
  13. </div>
  14. ...
  15. </div>

Where

  • accordion-list - list of accordion items set. Optional class
    • accordion-item - single accordion item. Required
      • accordion-item-toggle - link to expand/collapse accordion item content. Required
      • accordion-item-content - hidden accordion item content. Required
    • accordion-item-opened - single opened accordion item

Collapsible Layout

If you want to use single separate collapsible element you may just omit “accordion-list” wrapper element:

  1. <!-- Single collapsible element -->
  2. <div class="accordion-item">
  3. <div class="accordion-item-toggle">...</div>
  4. <div class="accordion-item-content">...</div>
  5. </div>
  6. <!-- Another separate collapsible element -->
  7. <div class="accordion-item">
  8. <div class="accordion-item-toggle">...</div>
  9. <div class="accordion-item-content">...</div>
  10. </div>

Accordion List View

If you use list view as accordion you may use “item-link” element instead of “accordion-toggle”:

  1. <div class="list accordion-list">
  2. <ul>
  3. <li class="accordion-item">
  4. <a href="" class="item-link item-content">
  5. <div class="item-inner">
  6. <div class="item-title">Item 1</div>
  7. </div>
  8. </a>
  9. <div class="accordion-item-content">Item 1 content ...</div>
  10. </li>
  11. <li class="accordion-item">
  12. <a href="" class="item-link item-content">
  13. <div class="item-inner">
  14. <div class="item-title">Item 2</div>
  15. </div>
  16. </a>
  17. <div class="accordion-item-content">Item 2 content ...</div>
  18. </li>
  19. </ul>
  20. </div>

Opposite Accordion

To render accordion chevron icon on opposite side (on left in LTR), it is required to add extra accordion-opposite class to accordion container or accordion list:

  1. <!-- Accordion opposite -->
  2. <div class="accordion-list accordion-opposite">
  3. <div class="accordion-item">
  4. <div class="accordion-item-toggle">...</div>
  5. <div class="accordion-item-content">...</div>
  6. </div>
  7. <div class="accordion-item accordion-item-opened">
  8. <div class="accordion-item-toggle">...</div>
  9. <div class="accordion-item-content">...</div>
  10. </div>
  11. <div class="accordion-item">
  12. <div class="accordion-item-toggle">...</div>
  13. <div class="accordion-item-content">...</div>
  14. </div>
  15. ...
  16. </div>
  17. <!-- Accordion list opposite -->
  18. <div class="list accordion-list accordion-opposite">
  19. <ul>
  20. <li class="accordion-item">
  21. <a href="" class="item-link item-content">
  22. <div class="item-inner">
  23. <div class="item-title">Item 1</div>
  24. </div>
  25. </a>
  26. <div class="accordion-item-content">Item 1 content ...</div>
  27. </li>
  28. <li class="accordion-item">
  29. <a href="" class="item-link item-content">
  30. <div class="item-inner">
  31. <div class="item-title">Item 2</div>
  32. </div>
  33. </a>
  34. <div class="accordion-item-content">Item 2 content ...</div>
  35. </li>
  36. </ul>
  37. </div>

Example

  1. <!-- List View -->
  2. <div class="block-title">List View Accordion</div>
  3. <div class="list accordion-list">
  4. <ul>
  5. <li class="accordion-item"><a href="#" class="item-content item-link">
  6. <div class="item-inner">
  7. <div class="item-title">Item 1</div>
  8. </div></a>
  9. <div class="accordion-item-content">
  10. <div class="block">
  11. <p>Item 1 content. Lorem ipsum dolor sit amet...</p>
  12. </div>
  13. </div>
  14. </li>
  15. <li class="accordion-item"><a href="#" class="item-content item-link">
  16. <div class="item-inner">
  17. <div class="item-title">Item 2</div>
  18. </div></a>
  19. <div class="accordion-item-content">
  20. <div class="block">
  21. <p>Item 2 content. Lorem ipsum dolor sit amet...</p>
  22. </div>
  23. </div>
  24. </li>
  25. ...
  26. </ul>
  27. </div>
  28. <!-- Separate collapsibles - omit "accordion-list" class-->
  29. <div class="block-title">Separate Collapsibles</div>
  30. <div class="list">
  31. <ul>
  32. <li class="accordion-item"><a href="#" class="item-content item-link">
  33. <div class="item-inner">
  34. <div class="item-title">Item 1</div>
  35. </div></a>
  36. <div class="accordion-item-content">
  37. <div class="block">
  38. <p>Item 1 content. Lorem ipsum dolor sit amet...</p>
  39. </div>
  40. </div>
  41. </li>
  42. <li class="accordion-item"><a href="#" class="item-content item-link">
  43. <div class="item-inner">
  44. <div class="item-title">Item 2</div>
  45. </div></a>
  46. <div class="accordion-item-content">
  47. <div class="block">
  48. <p>Item 2 content. Lorem ipsum dolor sit amet...</p>
  49. </div>
  50. </div>
  51. </li>
  52. ...
  53. </ul>
  54. </div>
  55. <!-- Opposite Side -->
  56. <div class="block-title">Opposite Side</div>
  57. <div class="list accordion-list accordion-opposite">
  58. <ul>
  59. <li class="accordion-item">
  60. <a class="item-content item-link" href="#">
  61. <div class="item-inner">
  62. <div class="item-title">Item 1</div>
  63. </div>
  64. </a>
  65. <div class="accordion-item-content">
  66. <div class="block">
  67. <p>Item 1 content. Lorem ipsum dolor sit amet...</p>
  68. </div>
  69. </div>
  70. </li>
  71. <li class="accordion-item">
  72. <a class="item-content item-link" href="#">
  73. <div class="item-inner">
  74. <div class="item-title">Item 2</div>
  75. </div>
  76. </a>
  77. <div class="accordion-item-content">
  78. <div class="block">
  79. <p>Item 2 content. Lorem ipsum dolor sit amet...</p>
  80. </div>
  81. </div>
  82. </li>
  83. ...
  84. </ul>
  85. </div>
  86. <!-- Custom Accordion -->
  87. <div class="block-title">Custom Accordion</div>
  88. <div class="block accordion-list custom-accordion">
  89. <div class="accordion-item">
  90. <div class="accordion-item-toggle">
  91. <i class="icon icon-plus">+</i>
  92. <i class="icon icon-minus">-</i>
  93. <span>Item 1</span>
  94. </div>
  95. <div class="accordion-item-content">
  96. <p>Item 1 content. Lorem ipsum dolor sit amet...</p>
  97. </div>
  98. </div>
  99. <div class="accordion-item">
  100. <div class="accordion-item-toggle">
  101. <i class="icon icon-plus">+</i>
  102. <i class="icon icon-minus">-</i>
  103. <span>Item 2</span>
  104. </div>
  105. <div class="accordion-item-content">
  106. <p>Item 2 content. Lorem ipsum dolor sit amet...</p>
  107. </div>
  108. </div>
  109. ...
  110. </div>

Accordion App Methods

Accordion also has a JavaScript API that allows you to open and close them programmatically. Lets look at some Accordion App methods:

app.accordion.open(el) - open specified accordion item

  • item - HTMLElement or string (with CSS Selector) of accordion-item element (
    ). Required

app.accordion.close(el) - close specified accordion item

  • item - HTMLElement or string (with CSS Selector) of accordion-item element (
    ). Required

app.accordion.toggle(el) - toggle specified accordion item

  • item - HTMLElement or string (with CSS Selector) of accordion-item element (
    ). Required

Accordion Events

Accordion will fire the following DOM events on accordion items and app events on app instance:

DOM Events

EventTargetDescription
accordion:beforeopenAccordion item<div class=”accordion-item”>Event will be triggered right before accordion content starts its opening animation. event.detail.prevent contains function that will prevent accordion from opening when called
accordion:openAccordion item<div class=”accordion-item”>Event will be triggered when accordion content starts its opening animation
accordion:openedAccordion item<div class=”accordion-item”>Event will be triggered after accordion content completes its opening animation
accordion:beforecloseAccordion item<div class=”accordion-item”>Event will be triggered right before accordion content starts its closing animation. event.detail.prevent contains function that will prevent accordion from closing when called
accordion:closeAccordion item<div class=”accordion-item”>Event will be triggered when accordion content starts its closing animation
accordion:closedAccordion item<div class=”accordion-item”>Event will be triggered after accordion content completes its closing animation

App Events

EventArgumentsDescription
accordionBeforeOpen(el, prevent)Event will be triggered right before accordion content starts its opening animation. prevent contains function that will prevent accordion from opening when called
accordionOpen(el)Event will be triggered when accordion content starts its opening animation
accordionOpened(el)Event will be triggered after accordion content completes its opening animation
accordionBeforeClose(el, prevent)Event will be triggered right before accordion content starts its closing animation. prevent contains function that will prevent accordion from closing when called
accordionClose(el)Event will be triggered when accordion content starts its closing animation
accordionClosed(el)Event will be triggered after accordion content completes its closing animation
  1. var app = new Framework7();
  2. var $ = Dom7;
  3. $('.accordion-item').on('accordion:opened', function () {
  4. app.dialog.alert('Accordion item opened');
  5. });
  6. $('.accordion-item').on('accordion:closed', function (e) {
  7. app.dialog.alert('Accordion item closed');
  8. });
  9. app.on('accordionOpened', function (el) {
  10. console.log('The following element opened:');
  11. console.log(el);
  12. });

← App / Core

Action Sheet / Actions →