手风琴

CSS3和JS响应式垂直手风琴特效

阿里云


这是一款使用 CSS3 和 JS 制作的响应式垂直手风琴特效。该垂直手风琴特效在展开手风琴项时,标题的图标和相应的列表内容使用 CSS3 animation 来制作动画特效。

HTML 结构:

该垂直手风琴列表的 HTML 结构如下。

也想出现在这里?联系我们
创客主机
  1. <div class="container">
  2.   <div class="accordion">
  3.     <dl>
  4.       <dt>
  5.         <a href="#accordion1" aria-expanded="false" aria-controls="accordion1"
  6.            class="accordion-title accordionTitle js-accordionTrigger">标题1</a>
  7.       </dt>
  8.       <dd class="accordion-content accordionItem is-collapsed" id="accordion1" aria-hidden="true">
  9.         <p>...</p>
  10.       </dd>
  11.       <dt>
  12.         <a href="#accordion2" aria-expanded="false" aria-controls="accordion2"
  13.            class="accordion-title accordionTitle js-accordionTrigger">标题2</a>
  14.       </dt>
  15.       <dd class="accordion-content accordionItem is-collapsed" id="accordion2" aria-hidden="true">
  16.         <p>...</p>
  17.       </dd>
  18.       <dt>
  19.         <a href="#accordion3" aria-expanded="false" aria-controls="accordion3"
  20.            class="accordion-title accordionTitle js-accordionTrigger">标题3</a>
  21.       </dt>
  22.       <dd class="accordion-content accordionItem is-collapsed" id="accordion3" aria-hidden="true">
  23.         <p>...</p>
  24.       </dd>
  25.     </dl>
  26.   </div>
  27. </div>

CSS 样式:

该手风琴特效主要执行两个 animation 动画:animateIn 和 animateOut,分别用于手风琴项的展开和收缩时的动画效果。

  1. .animateIn {
  2.   -webkit-animation: accordionIn 0.45s normal ease-in-out both 1;
  3.           animation: accordionIn 0.45s normal ease-in-out both 1;
  4. }
  5.  
  6. .animateOut {
  7.   -webkit-animation: accordionOut 0.45s alternate ease-in-out both 1;
  8.           animation: accordionOut 0.45s alternate ease-in-out both 1;
  9. }
  10.  
  11. @-webkit-keyframes accordionIn {
  12.   0% {
  13.     opacity: 0;
  14.     -webkit-transform: scale(0.9) rotateX(-60deg);
  15.             transform: scale(0.9) rotateX(-60deg);
  16.     -webkit-transform-origin: 50% 0;
  17.             transform-origin: 50% 0;
  18.   }
  19.   100% {
  20.     opacity: 1;
  21.     -webkit-transform: scale(1);
  22.             transform: scale(1);
  23.   }
  24. }
  25.  
  26. @keyframes accordionIn {
  27.   0% {
  28.     opacity: 0;
  29.     -webkit-transform: scale(0.9) rotateX(-60deg);
  30.             transform: scale(0.9) rotateX(-60deg);
  31.     -webkit-transform-origin: 50% 0;
  32.             transform-origin: 50% 0;
  33.   }
  34.   100% {
  35.     opacity: 1;
  36.     -webkit-transform: scale(1);
  37.             transform: scale(1);
  38.   }
  39. }
  40. @-webkit-keyframes accordionOut {
  41.   0% {
  42.     opacity: 1;
  43.     -webkit-transform: scale(1);
  44.             transform: scale(1);
  45.   }
  46.   100% {
  47.     opacity: 0;
  48.     -webkit-transform: scale(0.9) rotateX(-60deg);
  49.             transform: scale(0.9) rotateX(-60deg);
  50.   }
  51. }
  52. @keyframes accordionOut {
  53.   0% {
  54.     opacity: 1;
  55.     -webkit-transform: scale(1);
  56.             transform: scale(1);
  57.   }
  58.   100% {
  59.     opacity: 0;
  60.     -webkit-transform: scale(0.9) rotateX(-60deg);
  61.             transform: scale(0.9) rotateX(-60deg);
  62.   }
  63. }

JavaScript 代码:

该手风琴特效使用 JavaScript 来监听手风琴项的鼠标点击或移动触摸事件,并执行相应的操作。

  1. (function(){
  2.   var d = document,
  3.   accordionToggles = d.querySelectorAll('.js-accordionTrigger'),
  4.   setAria,
  5.   setAccordionAria,
  6.   switchAccordion,
  7.   touchSupported = ('ontouchstart' in window),
  8.   pointerSupported = ('pointerdown' in window);
  9.  
  10.   skipClickDelay = function(e){
  11.     e.preventDefault();
  12.     e.target.click();
  13.   }
  14.  
  15.     setAriaAttr = function(el, ariaType, newProperty){
  16.     el.setAttribute(ariaType, newProperty);
  17.   };
  18.   setAccordionAria = function(el1, el2, expanded){
  19.     switch(expanded) {
  20.       case "true":
  21.         setAriaAttr(el1, 'aria-expanded', 'true');
  22.         setAriaAttr(el2, 'aria-hidden', 'false');
  23.         break;
  24.       case "false":
  25.         setAriaAttr(el1, 'aria-expanded', 'false');
  26.         setAriaAttr(el2, 'aria-hidden', 'true');
  27.         break;
  28.       default:
  29.         break;
  30.     }
  31.   };
  32. //function
  33. switchAccordion = function(e) {
  34.   console.log("triggered");
  35.   e.preventDefault();
  36.   var thisAnswer = e.target.parentNode.nextElementSibling;
  37.   var thisQuestion = e.target;
  38.   if(thisAnswer.classList.contains('is-collapsed')) {
  39.     setAccordionAria(thisQuestion, thisAnswer, 'true');
  40.   } else {
  41.     setAccordionAria(thisQuestion, thisAnswer, 'false');
  42.   }
  43.     thisQuestion.classList.toggle('is-collapsed');
  44.     thisQuestion.classList.toggle('is-expanded');
  45.     thisAnswer.classList.toggle('is-collapsed');
  46.     thisAnswer.classList.toggle('is-expanded');
  47.  
  48.     thisAnswer.classList.toggle('animateIn');
  49.   };
  50.   for (var i=0,len=accordionToggles.length; i<len; i++) {
  51.     if(touchSupported) {
  52.       accordionToggles[i].addEventListener('touchstart', skipClickDelay, false);
  53.     }
  54.     if(pointerSupported){
  55.       accordionToggles[i].addEventListener('pointerdown', skipClickDelay, false);
  56.     }
  57.     accordionToggles[i].addEventListener('click', switchAccordion, false);
  58.   }
  59. })();

完整的 CSS 代码请参考下载文件。

CSS3 和 JS 响应式垂直手风琴特效

已有 444 人购买
查看演示升级 VIP立刻购买

演示地址 下载地址
收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!