键盘可访问手风琴插件 wfAccordion

  • 源码大小:175.93KB
  • 所需积分:1积分
  • 源码编号:19JP-3191
  • 浏览次数:1228次
  • 最后更新:2023年05月16日
  • 所属栏目:手风琴效果
我要下载
加入收藏
本站默认解压密码:19jp.com 或 19jp_com

简介

wfAccordion插件可以很容易地帮助开发人员为分段内容或常见问题创建响应灵敏且可访问的accordions。

工作原理:

  • 增强具有ARIA属性的触发器和面板:咏叹调膨胀,隐藏的咏叹调,aria已禁用.
  • 创建用于ARIA关系的唯一ID:aria控件,标记的咏叹调.
  • 在标题和面板之间创建ARIA关系。
  • 点击/点击更新ARIA状态。
  • 遵循WAI-ARIA Authoring practices 1.2 for Accorsions中规定的最佳实践。
  • 使用<button>增强给定的占位符元素,以获得更好的键盘支持。
  • 兼容jQuery和Vanilla JavaScript。

键盘支持:

  • 使用向上/向下箭头以及Ctrl+Page up/Page down键打开/关闭手风琴面板
  • 使用结束键聚焦第一个手风琴。
  • 使用Home键聚焦最后一个手风琴。

如何使用它:

1.要开始,请下载并导入wfAccordion。

  1. <!-- jQuery -->
  2. <script src="/path/to/jquery.min.js"></script>
  3. <script src="/path/to/wf.accordion.js"></script>
  1. <!-- Vanilla JavaScript -->
  2. <script src="/dist/wf.accordion.min.js" defer></script>
  1. // ES Module
  2. import 'webfactory-accordion/dist/wf.accordion.min';

2.构建手风琴的HTML结构。

  1. <div class="wf-accordion-group js-accordion-group">
  2. <div class="wf-accordion js-accordion">
  3. <div class="wf-accordion__header js-accordion__header">
  4. <h3 class="wf-accordion__trigger js-accordion__trigger">This is an accordion</h3>
  5. </div>
  6. <div class="wf-accordion__panel js-accordion__panel">
  7. <p>Accordions are a common design pattern in web design. They are often used to hide large chunks of content from the user intially. The aim is to provide a general overview of the content to the user, so that he can decide which part of it is interesting to him and can expand the content via interaction.</p>
  8. </div>
  9. </div>
  10. <div class="wf-accordion js-accordion" data-wf-accordion-expanded="">
  11. <div class="wf-accordion__header js-accordion__header">
  12. <h3 class="wf-accordion__trigger js-accordion__trigger">This accordion is expanded initially</h3>
  13. </div>
  14. <div class="wf-accordion__panel js-accordion__panel">
  15. <p>Add "data-wf-accordion-expanded" to the accordion's root element to expand its panel initially.</p>
  16. </div>
  17. </div>
  18. <div class="wf-accordion js-accordion" data-wf-accordion-disabled="">
  19. <div class="wf-accordion__header js-accordion__header">
  20. <h3 class="wf-accordion__trigger js-accordion__trigger">This accordion has been disabled by adding "data-wf-accordion-disabled" to its root element</h3>
  21. </div>
  22. <div class="wf-accordion__panel js-accordion__panel">
  23. <p>Add "data-wf-accordion-disabled" to the accordion's root element to disable it.</p>
  24. </div>
  25. </div>
  26. <div class="wf-accordion js-accordion">
  27. <div class="wf-accordion__header js-accordion__header">
  28. <h3 class="wf-accordion__trigger js-accordion__trigger">
  29. <span class="some-important-modifier-class">This accordion's trigger element has <em>nested</em> <strong>elements</strong> (e.g. a <code>strong</code> or <code>span</code>)</span>
  30. </h3>
  31. </div>
  32. <div class="wf-accordion__panel js-accordion__panel">
  33. <p>You can use nested elements inside the element with <code>.js-accordion__trigger</code>.</p>
  34. </div>
  35. </div>
  36. <div class="wf-accordion js-accordion">
  37. <div class="wf-accordion__header js-accordion__header">
  38. <div class="wf-accordion__trigger js-accordion__trigger">
  39. <h3>This accordion's trigger element has a nested heading that will become the parent element of the button.</h3>
  40. </div>
  41. </div>
  42. <div class="wf-accordion__panel js-accordion__panel">
  43. <p>To result in valid HTML, the final DOM needs to avoid nesting block-level elements in buttons.</p>
  44. </div>
  45. </div>
  46. </div>

3.初始化手风琴。

  1. $(function(){
  2. // jQuery Only
  3. $('.js-accordion-group').wfAccordion();
  4. });

4.将您自己的CSS样式应用于手风琴。

  1. /**
  2. * Accordion example styles
  3. */
  4.  
  5. .wf-accordion-group {
  6. margin-top: 15px;
  7. }
  8.  
  9. /* All elements succeeding an accordion group use margin-top to create white space */
  10. .wf-accordion-group+* {
  11. margin-top: 30px;
  12. }
  13.  
  14. /* All accordions have borders… */
  15. .wf-accordion {
  16. border-top: 1px solid #929292;
  17. border-bottom: 1px solid #929292;
  18. }
  19.  
  20. /* …unless they directly succeed another accordion, in which case we reset the top-border
  21. to avoid duplicate white space */
  22. .wf-accordion+.wf-accordion {
  23. border-top-width: 0;
  24. }
  25.  
  26. .wf-accordion__header {
  27. color: #616161;
  28. }
  29.  
  30. /* Please note: The trigger element is a <button> create via JS. To achieve consistent aesthetics,
  31. the native button styles have to be resetted here */
  32. .wf-accordion__trigger {
  33. /* baseline resets */
  34. background: transparent;
  35. border-width: 0;
  36. border-radius: 0;
  37. box-sizing: border-box;
  38. cursor: pointer;
  39. display: inline-block;
  40. font-size: inherit;
  41. letter-spacing: inherit;
  42. line-height: inherit;
  43. margin: 0;
  44. padding: 0;
  45. text-align: left;
  46. text-decoration: none;
  47. /* end of baseline resets */
  48.  
  49. /* additional styles for the demo */
  50. display: block;
  51. font-family: Arial, sans-serif;
  52. padding: 10px 1.25em 10px 8px;
  53. position: relative;
  54. width: 100%;
  55. }
  56.  
  57. .wf-accordion__trigger::after {
  58. content: '';
  59. border: solid #929292;
  60. border-width: 0 2px 2px 0;
  61. height: 0.5em;
  62. position: absolute;
  63. right: 10px;
  64. top: 50%;
  65. transform: translateY(-60%) rotate(45deg);
  66. width: 0.5em;
  67. }
  68.  
  69. .wf-accordion__trigger[aria-disabled=true] {
  70. cursor: not-allowed;
  71. opacity: 0.5;
  72. }
  73.  
  74. .wf-accordion__trigger[aria-expanded=true]::after {
  75. transform: translateY(-30%) rotate(-135deg);
  76. }
  77.  
  78. .wf-accordion__trigger:hover,
  79. .wf-accordion__trigger:focus {
  80. background: #f5f5f5;
  81. color: #161616;
  82. }
  83.  
  84. .wf-accordion__trigger:hover::after,
  85. .wf-accordion__trigger:focus::after {
  86. border-color: #161616;
  87. }
  88.  
  89. .wf-accordion__panel {
  90. background-color: #fff;
  91. padding: 10px 8px;
  92. }
  93.  
  94. .wf-accordion__panel[aria-hidden=true] {
  95. display: none;
  96. }

5.可用配置。

  1. $('.js-accordion-group').wfAccordion({
  2.  
  3. // default selectors
  4. accordionGroup: '.js-accordion-group',
  5. accordionRoot: '.js-accordion',
  6. accordionHeader: '.js-accordion__header',
  7. accordionTrigger: '.js-accordion__trigger',
  8. accordionPanel: '.js-accordion__panel',
  9.  
  10. // disables the automated hash update when triggering an accordion
  11. disableHashUpdate: false,
  12. });

更新日志:

版本4.0.2(2023-02-09)

  • 确保从字符串中生成有效的HTML ID

4.0版(2023-01-27)

  • 删除jQuery依赖项
  • 更改初始化
  • 处理手风琴触发器占位符中的嵌套HTML
  • 将标题处理为(或嵌套在)可折叠触发器占位符

2021-07-07

  • 将手风琴触发器按钮类型设置为“按钮”

2021-07-01

  • 折叠手风琴时删除手风琴URL哈希
  • 支持带有嵌套HTML的触发器占位符

2021-02-17

  • 所有DOM操作完成时引发事件

2019-08-24

  • 避免竞争条件并简化选择器

 

预览截图