爬行的蜗牛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: golang Linux PHP
查看: 3|回复: 0

无障碍折叠面板

[复制链接]

152

主题

48

回帖

1137

积分

管理员

积分
1137
发表于 昨天 22:14 | 显示全部楼层 |阅读模式
可访问性是UI设计中的重要因素。这个折叠面板不仅注重视觉上的无障碍设计,还支持键盘操作,用户可以使用TAB键和ENTER键导航各个部分,提升用户体验。

效果演示
640.gif

HTML
  1. <html lang="en">
  2.   <head>
  3.     <link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
  4.     <link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" integrity="sha384-wvfXpqpZZVQGK6TAh5PVlGOfQNHSoD2xbE+QkPxCAFlNEevoEH3Sl0sibVcOQVnN" crossorigin="anonymous">
  5.     <link href="https://fonts.googleapis.com/css?family=Lobster+Two" rel="stylesheet">
  6.   </head>

  7.   <body>
  8.     <h1>Accessible Accordion</h1>
  9.         
  10.     <!-- Accordion 1 -->
  11.     <section class="accordion" role="tablist" aria-live="polite" data-behavior="accordion">

  12.       <article class="accordion__item js-show-item-default" data-binding="expand-accordion-item">
  13.         <span id="tab1" tabindex="0" class="accordion__title" aria-controls="panel1" role="tab" aria-selected="false" aria-expanded="false" data-binding="expand-accordion-trigger">
  14.           <h5>Use the tab key to navigate...</h5>
  15.         </span>

  16.         <div id="panel1" class="accordion__content" role="tabpanel" aria-hidden="true" aria-labelledby="tab1" data-binding="expand-accordion-container">
  17.           <div class="accordion__content-inner">
  18.             <p>You can cycle through all the different accordion items through tabbing.</p>
  19.           </div>
  20.         </div>

  21.       </article>

  22.       <article class="accordion__item js-show-item-default" data-binding="expand-accordion-item">
  23.         <span id="tab2" tabindex="0" class="accordion__title" aria-controls="panel2" role="tab" aria-selected="false" aria-expanded="false" data-binding="expand-accordion-trigger">
  24.           <h5>Or, you can use your mouse.</h5>
  25.         </span>

  26.         <div id="panel2" class="accordion__content" role="tabpanel" aria-hidden="true" aria-labelledby="tab2" data-binding="expand-accordion-container">
  27.           <div class="accordion__content-inner">
  28.             <p>This accordion can be used by both mouse and keyboard-only users.</p>
  29.           </div>
  30.         </div>
  31.       </article>
  32.       
  33.       <article class="accordion__item js-show-item-default" data-binding="expand-accordion-item">
  34.         <span id="tab3" tabindex="0" class="accordion__title" aria-controls="panel3" role="tab" aria-selected="false" aria-expanded="false" data-binding="expand-accordion-trigger">
  35.           <h5>Click here to view more</h5>
  36.         </span>

  37.         <div id="panel3" class="accordion__content" role="tabpanel" aria-hidden="true" aria-labelledby="tab3" data-binding="expand-accordion-container">
  38.           <div class="accordion__content-inner">
  39.             <p>You can also use either the space bar or enter key to expand/collapse these panels.</p>
  40.           </div>
  41.         </div>
  42.       </article>
  43.       
  44.       <article class="accordion__item js-show-item-default" data-binding="expand-accordion-item">
  45.         <span id="tab4" tabindex="0" class="accordion__title" aria-controls="panel4" role="tab" aria-selected="false" aria-expanded="false" data-binding="expand-accordion-trigger">
  46.           <h5>ARIA</h5>
  47.         </span>

  48.         <div id="panel4" class="accordion__content" role="tabpanel" aria-hidden="true" aria-labelledby="tab4" data-binding="expand-accordion-container">
  49.           <div class="accordion__content-inner">
  50.             <p>This accordion makes use of ARIA tags which help aid accessibility.</p>
  51.           </div>
  52.         </div>
  53.       </article>

  54.     </section>

  55.     <!-- Accordion 2 -->
  56.     <section class="accordion" role="tablist" aria-live="polite" data-behavior="accordion">
  57.       <article class="accordion__item js-show-item-default" data-binding="expand-accordion-item">
  58.         <span id="tab5" tabindex="0" class="accordion__title" aria-controls="panel5" role="tab" aria-selected="false" aria-expanded="false" data-binding="expand-accordion-trigger">
  59.           <h5>Heading 1</h5>
  60.         </span>

  61.         <div id="panel5" class="accordion__content" role="tabpanel" aria-hidden="true" aria-labelledby="tab5" data-binding="expand-accordion-container">
  62.           <div class="accordion__content-inner">
  63.             <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
  64.           </div>
  65.         </div>

  66.       </article>

  67.       <article class="accordion__item js-show-item-default" data-binding="expand-accordion-item">
  68.         <span id="tab6" tabindex="0" class="accordion__title" aria-controls="panel6" role="tab" aria-selected="false" aria-expanded="false" data-binding="expand-accordion-trigger">
  69.           <h5>Heading 2</h5>
  70.         </span>

  71.         <div id="panel6" class="accordion__content" role="tabpanel" aria-hidden="true" aria-labelledby="tab6" data-binding="expand-accordion-container">
  72.           <div class="accordion__content-inner">
  73.             <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam.</p>
  74.           </div>
  75.         </div>
  76.       </article>

  77.     </section>
  78.   </body>
  79. </html>
复制代码
  1. body {
  2.   background-color: #F6F7EB;
  3.   font-family: 'Open Sans', sans-serif;
  4. }

  5. h1 {
  6.   font-family: 'Lobster Two', cursive;
  7.   text-align: center;
  8.   margin-top: 30px;
  9.   font-size: 50px;
  10.   color: #2b3a40;
  11. }

  12. .accordion {
  13.   background-color: #fefffa;
  14.   max-width: 600px;
  15.   margin: 50px auto;
  16.   border-top: 6px solid #44BBA4;
  17.   line-height: 1.6;
  18.   box-shadow: 5px 5px 10px 0px #a4bac1;
  19.   
  20.   &__item {
  21.     border-bottom: 1px solid #dce7eb;
  22.   }

  23.   &__title {
  24.     padding: 15px 15px 15px 40px;
  25.     display: block;
  26.     position: relative;
  27.     font-weight: 400;

  28.     &:before {
  29.       font-family: FontAwesome;
  30.       content: "\f055";
  31.       font-size: 20px;
  32.       position: absolute;
  33.       left: 15px;
  34.       top: 12px;
  35.       color: #44BBA4;
  36.     }
  37.    
  38.     h5 {
  39.       border-bottom: 1px solid #fefffa;
  40.       display:inline-block;
  41.     }

  42.     &:hover,
  43.     &:focus {
  44.       cursor: pointer;
  45.       outline: none;
  46.       h5 {
  47.         border-bottom-color: #a8bdc4;
  48.         display:inline-block;
  49.       }
  50.     }

  51.     .is-expanded & {
  52.       &:before {
  53.         content: "\f056";
  54.       }

  55.     }
  56.   }

  57.     &__content-inner {
  58.         padding: 0 40px 10px 40px;
  59.     }

  60.   &__content {
  61.     transition: height 0.3s ease-out;
  62.     height: 0;
  63.     overflow: hidden;
  64.   }
  65. }
复制代码
  1. var accordion = $('body').find('[data-behavior="accordion"]');
  2. var expandedClass = 'is-expanded';

  3. $.each(accordion, function () { // loop through all accordions on the page

  4.   var accordionItems = $(this).find('[data-binding="expand-accordion-item"]');

  5.   $.each(accordionItems, function () { // loop through all accordion items of each accordion
  6.     var $this = $(this);
  7.     var triggerBtn = $this.find('[data-binding="expand-accordion-trigger"]');
  8.    
  9.     var setHeight = function (nV) {
  10.       // set height of inner content for smooth animation
  11.       var innerContent = nV.find('.accordion__content-inner')[0],
  12.           maxHeight = $(innerContent).outerHeight(),
  13.           content = nV.find('.accordion__content')[0];

  14.       if (!content.style.height || content.style.height === '0px') {
  15.         $(content).css('height', maxHeight);
  16.       } else {
  17.         $(content).css('height', '0px');
  18.       }
  19.     };
  20.    
  21.     var toggleClasses = function (event) {
  22.       var clickedItem = event.currentTarget;
  23.       var currentItem = $(clickedItem).parent();
  24.       var clickedContent = $(currentItem).find('.accordion__content')
  25.       $(currentItem).toggleClass(expandedClass);
  26.       setHeight(currentItem);
  27.       
  28.       if ($(currentItem).hasClass('is-expanded')) {
  29.         $(clickedItem).attr('aria-selected', 'true');
  30.         $(clickedItem).attr('aria-expanded', 'true');
  31.         $(clickedContent).attr('aria-hidden', 'false');

  32.       } else {
  33.         $(clickedItem).attr('aria-selected', 'false');
  34.         $(clickedItem).attr('aria-expanded', 'false');
  35.         $(clickedContent).attr('aria-hidden', 'true');
  36.       }
  37.     }
  38.    
  39.     triggerBtn.on('click', event, function (e) {
  40.       e.preventDefault();
  41.       toggleClasses(event);
  42.     });

  43.     // open tabs if the spacebar or enter button is clicked whilst they are in focus
  44.     $(triggerBtn).on('keydown', event, function (e) {
  45.       if (e.keyCode === 13 || e.keyCode === 32) {
  46.         e.preventDefault();
  47.         toggleClasses(event);
  48.       }
  49.     });
  50.   });

  51. });

复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表