可访问内容切换插件 jQuery切换面板

  • 源码大小:9.46KB
  • 所需积分:1积分
  • 源码编号:19JP-3401
  • 浏览次数:1230次
  • 最后更新:2023年06月09日
  • 所属栏目:手风琴效果
本站默认解压密码:19jp.com 或 19jp_com

简介

TogglePanel是一个轻量级的jQuery插件,它提供了一种优雅的方式来在页面上创建可访问的、移动友好的可切换内容(如剧透和手风琴),并支持ARIA属性、角色和键盘交互。

更多功能:

  • 自定义切换模式:滑动、切换或功能。
  • 自动关闭移动设备上的所有面板。
  • 在打开的面板上自动设置焦点。
  • 自定义触发事件:点击或鼠标悬停。
  • 自定义ARIA标签。

如何使用它:

1.启用一个按钮来显示/隐藏DIV元素。

  1. <button type="button" id="trigger-example">Toggle Panel-Example</button>
  2. <div id="panel-example" class="panel">
  3. Content To Toggle
  4. </div>

2.在页面加载时隐藏DIV元素。

  1. .panel {
  2. display: none;
  3. }
  4.  
  5. .tgp__panel--is-opened {
  6. display: block !important;
  7. }

3.确定是否在页面加载时打开DIV面板。

  1. <button type="button" data-tgp-panel-id="trigger-example" data-tgp-opened="true">Toggle Panel-Example</button>
  2. <div id="panel-example" class="panel">
  3. Content To Toggle
  4. </div>

4.在jQuery之后加载TogglePanel插件。

  1. <script src="/path/to/cdn/jquery.min.js"></script>
  2. <script src="/path/to/jquery.toggle-panel.js"></script>

5.初始化插件并完成。

  1. $('#trigger-example').togglePanel({
  2. // options here
  3. });

6.使用这个插件来创建一个可访问的手风琴接口。

  1. <div id="wrapper">
  2. <div>
  3. <button type="button" id="trigger-1">Toggle Panel 1</button>
  4. <div id="panel-1" class="panel">
  5. Accordion 1
  6. </div>
  7. </div>
  8. <div>
  9. <button type="button" id="trigger-2">Toggle Panel 2</button>
  10. <div id="panel-2" class="panel">
  11. Accordion 2
  12. </div>
  13. </div>
  14. <div>
  15. <button type="button" id="trigger-3">Toggle Panel 3</button>
  16. <div id="panel-3" class="panel">
  17. Accordion 1
  18. </div>
  19. </div>
  20. </div>
  1. $('button').togglePanel({
  2. panel: 'next',
  3. mode: 'toggle',
  4. wrapper: $('#wrapper'),
  5. connect: true,
  6. });

7.所有默认插件选项。

  1. $('#trigger-example').togglePanel({
  2.  
  3. // CSS class prefix
  4. prefix : 'tgp',
  5.  
  6. // wrapper of connected panels
  7. wrapper: false,
  8.  
  9. // if true, only one panel can be opened
  10. connect: false,
  11.  
  12. // next panel or an unique panel ID
  13. panel: 'next',
  14.  
  15. // trigger event: 'click' or 'mouseover'
  16. event: 'click',
  17.  
  18. // If 'panel' setting = "function", this function will be called
  19. findPanel: function() {},
  20. // 'slide', 'toggle', or 'custom'
  21. mode: 'slide',
  22. // auto move focus to the opened panel
  23. autoFocus: true,
  24.  
  25. // allow the trigger button to close its panel
  26. selfClose: true,
  27.  
  28. // return focus to the trigger after closing
  29. returnFocus: true,
  30.  
  31. // close panel after mouse leaving with delay
  32. autoHide: false,
  33.  
  34. // delay in ms
  35. delay: 300,
  36.  
  37. // disable clicks on the first levels
  38. disableFirstLevel: false,
  39.  
  40. // remove titles from trigger buttons
  41. removeTitle: false,
  42.  
  43. // close all panels on mobile devices
  44. smallScreenBreakpoint: 767,
  45.  
  46. // labels for accessibility
  47. panelLabel: '',
  48. closeLabel: 'Collapse',
  49. openLabel: 'Expand',
  50.  
  51. // If 'mode' setting = 'custom', these functions will be called
  52. customShow: function() {},
  53. customHide: function() {},
  54.  
  55. });

8.回调。

  1. $('#trigger-example').togglePanel({
  2.  
  3. onShow: function () {},
  4. onShowEnd: function () {},
  5. onHide: function () {},
  6. onHideEnd: function () {}
  7.  
  8. });

9.API方法。

  1. // remove auto focus on init
  2. $('button[id^=trigger]').trigger('no-autofocus.tgp');
  3.  
  4. // show panel
  5. $('button[id^=trigger]').trigger('show.tgp');
  6.  
  7. // hide panel
  8. $('button[id^=trigger]').trigger('hide.tgp');
  9.  
  10. // destroy
  11. $('button[id^=trigger]').trigger('destroy.tgp');

预览截图