10-18-transition-to-maxheight.html 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Transition to max-height</title>
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7. <script>
  8. // This JavaScript code adds a class name of "js" to the html element,
  9. // to give us a "hook" in CSS for when JS is available.
  10. var html = document.getElementsByTagName('html')[0];
  11. html.className += ' js';
  12. </script>
  13. <style>
  14. body {
  15. font-family: "Myriad Pro", Frutiger, "Lucida Grande", "Lucida Sans", "Lucida Sans Unicode", Verdana, sans-serif;
  16. background-color: #edf5f8;
  17. margin: 0;
  18. padding: 1em;
  19. }
  20. button {
  21. display: inline-block;
  22. vertical-align: 20%;
  23. cursor: pointer;
  24. border: 0;
  25. padding: .25em 1em;
  26. color: #fff;
  27. border-radius: .25em;
  28. outline: none;
  29. font-size: 12px;
  30. background-color: #173b6d;
  31. background-image: -webkit-linear-gradient(top, #1a4a8e, #173b6d);
  32. background-image: -moz-linear-gradient(top, #1a4a8e, #173b6d);
  33. background-image: -o-linear-gradient(top, #1a4a8e, #173b6d);
  34. background-image: linear-gradient(to bottom, #1a4a8e, #173b6d);
  35. box-shadow: 0 .25em 0 rgba(23, 59, 109, 0.3), inset 0 1px 0 rgba(0, 0, 0, 0.3);
  36. -webkit-transition: all 150ms ease-in;
  37. -moz-transition: all 150ms ease-in;
  38. -o-transition: all 150ms ease-in;
  39. transition: all 150ms ease-in;
  40. }
  41. button:active {
  42. box-shadow: 0 0 0 rgba(23, 59, 109, 0.3), inset 0 1px 0 rgba(0, 0, 0, 0.3);
  43. -webkit-transform: translateY(.25em);
  44. -moz-transform: translateY(.25em);
  45. -ms-transform: translateY(.25em);
  46. -o-transform: translateY(.25em);
  47. transform: translateY(.25em);
  48. }
  49. .expando {
  50. min-width: 9em;
  51. max-width: 25em;
  52. padding: 20px;
  53. border: 1px solid #ccc;
  54. background-color: #fff;
  55. }
  56. .expando-title {
  57. margin: 0;
  58. margin-bottom: .5em;
  59. vertical-align: middle;
  60. }
  61. .expando-trigger {
  62. margin-left: 0.5em;
  63. }
  64. .expando ol {
  65. margin: 0;
  66. }
  67. .expando li {
  68. border-bottom: 1px solid #ccc;
  69. line-height: 1.5; /* 24 px */
  70. }
  71. /**
  72. * 1. We'll discuss the will-change property briefly in chapter 12!
  73. */
  74. .js .expando-list {
  75. overflow: hidden;
  76. will-change: transform, opacity; /* 1 */
  77. -webkit-transition: all .25s ease-in-out;
  78. -moz-transition: all .25s ease-in-out;
  79. -o-transition: all .25s ease-in-out;
  80. transition: all .25s ease-in-out;
  81. max-height: 0;
  82. opacity: 0;
  83. }
  84. .js .is-expanded .expando-list {
  85. max-height: 24em;
  86. opacity: 1;
  87. }
  88. </style>
  89. </head>
  90. <body>
  91. <div class="expando">
  92. <h2 class="expando-title">Top menu choices</h2>
  93. <ol>
  94. <li>Capricciosa</li>
  95. <li>Margherita</li>
  96. <li>Vesuvio</li>
  97. </ol>
  98. <ol class="expando-list" start="4" aria-label="Top menu choices, continued.">
  99. <li>Calzone</li>
  100. <li>Quattro Stagioni</li>
  101. <li>Pescatore</li>
  102. <li>Bolognese</li>
  103. <li>Shawarma</li>
  104. <li>Mexicana</li>
  105. <li>Fungi</li>
  106. </ol>
  107. </div>
  108. <script>
  109. // This script handles the toggling of classes and attributes on the list.
  110. // Grab the first `.expando` element:
  111. var expando = document.querySelector('.expando'),
  112. // grab the title element:
  113. expandoTitle = expando.querySelector('.expando-title'),
  114. // grab the list itself:
  115. expandoList = expando.querySelector('.expando-list'),
  116. expandedClass = 'is-expanded';
  117. // Set the aria-hidden attribute on the expando-list
  118. expandoList.setAttribute('aria-hidden', true);
  119. // Create the "show all" button
  120. var trigger = document.createElement('button'),
  121. triggerText = document.createTextNode('Toggle full list');
  122. trigger.appendChild(triggerText);
  123. // Set the chosen class name for the trigger
  124. trigger.className = 'expando-trigger';
  125. // Grab the title inside the expando and add the button to it:
  126. var expandoTitle = expando.querySelector('.expando-title');
  127. expandoTitle.appendChild(trigger);
  128. // A small function to toggle the class name "expanded" on the expando,
  129. // when clicked:
  130. var toggleExpanded = function (e) {
  131. if (/(^|\s)is-expanded(\s|$)/.test(expando.className)) {
  132. expando.className = expando.className.replace(' is-expanded', '');
  133. expandoList.setAttribute('aria-hidden', 'true');
  134. } else {
  135. expando.className += ' is-expanded';
  136. expandoList.setAttribute('aria-hidden', 'false');
  137. }
  138. }
  139. // Add event listener (IE9+ and other modern browsers)
  140. if (trigger.addEventListener) {
  141. trigger.addEventListener('click', toggleExpanded, false);
  142. } else {
  143. // For IE8:
  144. if (trigger.attachEvent) {
  145. trigger.attachEvent('onclick', toggleExpanded);
  146. }
  147. }
  148. </script>
  149. </body>
  150. </html>