爬行的蜗牛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

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

原生HTML的<details>折叠面板

[复制链接]

152

主题

48

回帖

1137

积分

管理员

积分
1137
发表于 昨天 22:12 | 显示全部楼层 |阅读模式
借助HTML5的<details>标签,开发者可以不借助JavaScript或其他语言就实现原生的折叠面板。CSS则可以进一步美化外观,并且这一方案具有良好的性能和兼容性。

效果演示
640.gif

HTML
  1. <h1>Accordion with <code>&lt;details&gt;</code> element</h1>
  2. <p>Making the native <abbr>HTML</abbr> <code>&lt;details&gt;</code> element behave like a typical collapsible accordion with smooth animations.</p>

  3. <p><input type="checkbox" id="accordion-toggle"><label for="accordion-toggle">Keep animations but remove auto-collapse effect </label></p>

  4. <div class="container collapse">
  5.   <details>
  6.     <summary>Accordion heading 1</summary>
  7.     <div class="details-wrapper">
  8.       <div class="details-styling">        
  9.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa quis fuga saepe sunt delectus, quo assumenda dolorum officiis odit optio modi, aspernatur necessitatibus libero, itaque repellendus. Incidunt blanditiis magni velit.</p>
  10.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa quis fuga saepe sunt delectus, quo assumenda dolorum officiis odit optio modi, aspernatur necessitatibus libero, itaque repellendus. Incidunt blanditiis magni velit.</p>
  11.       </div>
  12.     </div>
  13.   </details>
  14.   <details>
  15.     <summary>Accordion heading 2</summary>
  16.     <div class="details-wrapper">
  17.       <div class="details-styling">        
  18.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa quis fuga saepe sunt delectus, quo assumenda dolorum officiis odit optio modi, aspernatur necessitatibus libero, itaque repellendus. Incidunt blanditiis magni velit.</p>
  19.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa quis fuga saepe sunt delectus, quo assumenda dolorum officiis odit optio modi, aspernatur necessitatibus libero, itaque repellendus. Incidunt blanditiis magni velit.</p>
  20.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa quis fuga saepe sunt delectus, quo assumenda dolorum officiis odit optio modi, aspernatur necessitatibus libero, itaque repellendus. Incidunt blanditiis magni velit.</p>
  21.       </div>
  22.     </div>
  23.   </details>
  24.   <details>
  25.     <summary>Accordion heading 3</summary>
  26.     <div class="details-wrapper">   
  27.       <div class="details-styling">        
  28.         <p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Ipsa quis fuga saepe sunt delectus, quo assumenda dolorum officiis odit optio modi, aspernatur necessitatibus libero, itaque repellendus. Incidunt blanditiis magni velit.</p>
  29.       </div>
  30.     </div>
  31.   </details>
  32. </div>

  33. <p>Using the <a href="https://github.com/javan/details-element-polyfill">details-element-polyfill</a> for IE/Edge support</p>
复制代码
  1. // Classes set via JS
  2. $accordionClass: '.collapse-init';
  3. $panelClass: '.panel-active';
  4. $contentsClass: 'summary + *';

  5. /*
  6.   Please wrap your collapsible content in a single element or so help me
  7.   Must add a transition or it breaks because that's the whole purpose of this
  8.   Only one transition-duration works (see explanation on line #141 in JS)
  9.   You can add more to an inner wrapper (.details-styling)
  10. */
  11. //Simplified: .details-wrapper{}
  12. #{$accordionClass $contentsClass} {
  13.   transition: all 0.25s ease-in-out;
  14.   overflow: hidden; // because we're animating height
  15. }

  16. /*
  17.   Closed state. Any CSS transitions work here
  18.   The JS has a height calculation to make sliding opened/closed easier, but it's not necessary
  19.   Remove the height prop for a simple toggle on/off (after all that work I did for you?)
  20. */
  21. //Simplified: :not(.panel-active) .details-wrapper {}
  22. #{$accordionClass} :not(#{$panelClass}) #{$contentsClass} {  
  23.   height: 0;
  24.   opacity: 0;
  25.   transform: scale(0.9);
  26.   transform-origin: bottom center;
  27. }

  28. // Let's get rid of the default arrows so we can style our own, as we must find whatever little joy we can in this garbage web
  29. #{$accordionClass} {
  30.   summary { list-style: none; } // Spec
  31.   summary::-webkit-details-marker { display: none; } // Chrome
  32.   summary::before { display: none; } // Polyfill
  33.    
  34.   // Should we do this? No idea
  35.   summary { cursor: pointer; }
  36. }

  37. /*
  38.   This element exists so .details-wrapper has no extra margin/padding that can screw with the smooth height collapse
  39.   You can style .details-wrapper decoratively but avoid anything that modifies the box and add it to .details-styling instead
  40.   Otherwise, this element is totally optional. Remove if you hate divitis
  41. */
  42. .details-styling {
  43.   padding: 1em;
  44. }

  45. //======= Non-essential page styling, ignore
  46. $hue: 260;

  47. $background: hsl($hue, 90, 98);
  48. $text: hsl($hue, 20, 25);

  49. $primary: hsl($hue, 85, 50);
  50. $link: hsl($hue, 75, 65);
  51. $border: hsl($hue, 20, 85);

  52. ::selection {
  53.   $rot: 140;
  54.   
  55.   background: hsl($hue + $rot, 95, 70);
  56.   color: adjust-hue($text, $rot);
  57. }

  58. html {
  59.   background: $background;
  60.   color: $text;
  61. }

  62. body {
  63.   font: 1em/1.4 'Quicksand', sans-serif;
  64.   margin: 0 auto;
  65.   max-width: 960px;
  66.   padding: 5vw;
  67. }

  68. h1 {
  69.   font-size: 2em;
  70.   margin-bottom: 1em;
  71.   text-align: center;
  72.   
  73.   + p {
  74.     margin-left: auto;
  75.     margin-right: auto;
  76.     max-width: 50ch;
  77.   }
  78.   
  79.   ~ p {
  80.     font-size: 1.2em;
  81.     text-align: center;
  82.   }
  83. }

  84. p {
  85.   margin-top: 0;
  86.   margin-bottom: 1em;
  87.   
  88.   &:last-child { margin-bottom: 0; }
  89. }

  90. code {
  91.   $rot: 90;
  92.   
  93.   background: hsla($hue + $rot, 70, 70, 0.1);
  94.   color: hsl($hue + $rot - 5, 75, 65);
  95. }

  96. a {
  97.   color: $link;
  98.   box-shadow: inset 0 -3px lighten($link, 20);
  99.   font-weight: 700;
  100.   text-decoration: none;
  101.   transition: 0.2s;
  102.   
  103.   &:hover,
  104.   &:focus {
  105.     box-shadow: inset 0 -1.2em $link;
  106.     color: $background;
  107.   }
  108. }

  109. abbr {
  110.   font-variant: small-caps;
  111.   text-transform: lowercase;
  112.   font-size: 1.2em;
  113. }

  114. [type=checkbox] {
  115.   opacity: 0;
  116.   position: absolute;
  117.   width: 0;
  118.   height: 0;
  119.   
  120.   + label {
  121.     background: lighten($primary, 45);
  122.     border-left: 4px solid $primary;
  123.     cursor: pointer;
  124.     display: block;
  125.     font-size: 1rem;
  126.     font-weight: 700;
  127.     text-align: left;
  128.     transition: 0.1s;
  129.     padding: 0.75em 1em;
  130.    
  131.     &::before {
  132.       border: 2px solid;
  133.       border-radius: 2px;
  134.       color: $primary;
  135.       content: '';
  136.       display: inline-block;
  137.       margin-right: 0.75ch;
  138.       transition: 0.1s;
  139.       width: 1ch;
  140.       height: 1ch;
  141.       vertical-align: baseline;
  142.     }
  143.   }
  144.   
  145.   &:focus + label {
  146.     outline: 2px solid $primary;   
  147.   }
  148.   
  149.   &:checked + label::before {
  150.     background: currentColor;
  151.     box-shadow: inset 0 0 0 2px #fff;
  152.   }
  153. }

  154. .container {
  155.   box-shadow: 0.2em 1em 2em -1em $border;
  156.   margin: 2.4em 0;
  157. }

  158. //==== Accordion element styling
  159. details {
  160.   $b: 6px;
  161.   
  162.   background: #fff;
  163.   border: 1px solid $border;
  164.   border-bottom: 0;
  165.   list-style: none;
  166.   
  167.   &:first-child {
  168.     border-radius: $b $b 0 0;
  169.   }
  170.   
  171.   &:last-child {
  172.     border-bottom: 1px solid $border;
  173.     border-radius: 0 0 $b $b;
  174.   }
  175. }

  176. summary {
  177.   $arrow-size: 0.5em;
  178.   
  179.   display: block;
  180.   transition: 0.2s;
  181.   font-weight: 700;
  182.   padding: 1em;
  183.    
  184.   &:focus {
  185.     outline: 2px solid $primary;
  186.   }
  187.   
  188.   #{$accordionClass} &::after {
  189.     border-right: 2px solid;
  190.     border-bottom: 2px solid;
  191.     content: '';
  192.     float: right;
  193.     width: $arrow-size;
  194.     height: $arrow-size;
  195.     margin-top: $arrow-size/2;
  196.     transform: rotate(45deg);
  197.     transition: inherit;
  198.   }
  199.   
  200.   [open] & {
  201.     background: $primary;
  202.     color: $background;
  203.    
  204.     &::after {
  205.       margin-top: $arrow-size;
  206.       transform: rotate(225deg);
  207.     }
  208.   }
  209. }
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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