Advertisement
Guest User

Untitled

a guest
Jun 11th, 2025
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.81 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Smooth Expand/Collapse Component</title>
  7. <style>
  8. * {
  9. box-sizing: border-box;
  10. }
  11.  
  12. body {
  13. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  14. line-height: 1.6;
  15. margin: 0;
  16. padding: 40px 20px;
  17. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  18. min-height: 100vh;
  19. }
  20.  
  21. .container {
  22. max-width: 600px;
  23. margin: 0 auto;
  24. background: white;
  25. border-radius: 12px;
  26. box-shadow: 0 20px 40px rgba(0,0,0,0.1);
  27. overflow: hidden;
  28. }
  29.  
  30. .accordion-item {
  31. border-bottom: 1px solid #e2e8f0;
  32. }
  33.  
  34. .accordion-item:last-child {
  35. border-bottom: none;
  36. }
  37.  
  38. .accordion-header {
  39. background: #f8fafc;
  40. padding: 20px 24px;
  41. cursor: pointer;
  42. user-select: none;
  43. display: flex;
  44. justify-content: space-between;
  45. align-items: center;
  46. transition: all 0.3s ease;
  47. position: relative;
  48. }
  49.  
  50. .accordion-header:hover {
  51. background: #f1f5f9;
  52. }
  53.  
  54. .accordion-header.active {
  55. background: #3b82f6;
  56. color: white;
  57. }
  58.  
  59. .accordion-title {
  60. font-size: 18px;
  61. font-weight: 600;
  62. margin: 0;
  63. }
  64.  
  65. .accordion-icon {
  66. width: 24px;
  67. height: 24px;
  68. display: flex;
  69. align-items: center;
  70. justify-content: center;
  71. transition: transform 0.3s ease;
  72. }
  73.  
  74. .accordion-icon::before {
  75. content: '+';
  76. font-size: 20px;
  77. font-weight: bold;
  78. line-height: 1;
  79. }
  80.  
  81. .accordion-header.active .accordion-icon {
  82. transform: rotate(45deg);
  83. }
  84.  
  85. .accordion-content {
  86. max-height: 0;
  87. overflow: hidden;
  88. transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  89. }
  90.  
  91. .accordion-content.expanded {
  92. max-height: 1000px; /* Large enough value for most content */
  93. }
  94.  
  95. .accordion-body {
  96. padding: 24px;
  97. color: #4a5568;
  98. line-height: 1.7;
  99. }
  100.  
  101. .accordion-body h3 {
  102. color: #2d3748;
  103. margin-top: 0;
  104. margin-bottom: 16px;
  105. }
  106.  
  107. .accordion-body p {
  108. margin-bottom: 16px;
  109. }
  110.  
  111. .accordion-body ul {
  112. margin-bottom: 16px;
  113. padding-left: 20px;
  114. }
  115.  
  116. .accordion-body li {
  117. margin-bottom: 8px;
  118. }
  119.  
  120. /* Alternative smooth technique using grid */
  121. .accordion-content-alt {
  122. display: grid;
  123. grid-template-rows: 0fr;
  124. transition: grid-template-rows 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  125. }
  126.  
  127. .accordion-content-alt.expanded {
  128. grid-template-rows: 1fr;
  129. }
  130.  
  131. .accordion-content-alt .accordion-body {
  132. overflow: hidden;
  133. }
  134.  
  135. /* Demo styles */
  136. .demo-section {
  137. margin: 40px 0;
  138. }
  139.  
  140. .demo-title {
  141. text-align: center;
  142. color: white;
  143. font-size: 24px;
  144. font-weight: 600;
  145. margin-bottom: 20px;
  146. }
  147.  
  148. .code-block {
  149. background: #1a202c;
  150. color: #e2e8f0;
  151. padding: 16px;
  152. border-radius: 8px;
  153. font-family: 'Courier New', monospace;
  154. font-size: 14px;
  155. overflow-x: auto;
  156. }
  157. </style>
  158. </head>
  159. <body>
  160. <div class="demo-section">
  161. <h1 class="demo-title">Smooth Expand/Collapse Components</h1>
  162. </div>
  163.  
  164. <div class="container">
  165. <div class="accordion-item">
  166. <div class="accordion-header" onclick="toggleAccordion(this)">
  167. <h3 class="accordion-title">What is this component?</h3>
  168. <div class="accordion-icon"></div>
  169. </div>
  170. <div class="accordion-content">
  171. <div class="accordion-body">
  172. <p>This is a pure HTML and CSS accordion component that smoothly animates the expansion and collapse of content panels, regardless of their height.</p>
  173. <p>The animation uses CSS transitions with a <code>max-height</code> property that transitions from 0 to a large value, creating a smooth reveal effect.</p>
  174. </div>
  175. </div>
  176. </div>
  177.  
  178. <div class="accordion-item">
  179. <div class="accordion-header" onclick="toggleAccordion(this)">
  180. <h3 class="accordion-title">How does it work?</h3>
  181. <div class="accordion-icon"></div>
  182. </div>
  183. <div class="accordion-content">
  184. <div class="accordion-body">
  185. <h3>Technical Implementation</h3>
  186. <p>The smooth animation is achieved through several CSS techniques:</p>
  187. <ul>
  188. <li><strong>Max-height transition:</strong> We transition from <code>max-height: 0</code> to a large value</li>
  189. <li><strong>Overflow hidden:</strong> Prevents content from showing during animation</li>
  190. <li><strong>Cubic-bezier easing:</strong> Creates natural, smooth motion</li>
  191. <li><strong>JavaScript toggle:</strong> Adds/removes the expanded class</li>
  192. </ul>
  193. <div class="code-block">
  194. .accordion-content {
  195. max-height: 0;
  196. overflow: hidden;
  197. transition: max-height 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  198. }
  199.  
  200. .accordion-content.expanded {
  201. max-height: 1000px;
  202. }
  203. </div>
  204. </div>
  205. </div>
  206. </div>
  207.  
  208. <div class="accordion-item">
  209. <div class="accordion-header" onclick="toggleAccordion(this)">
  210. <h3 class="accordion-title">Features & Benefits</h3>
  211. <div class="accordion-icon"></div>
  212. </div>
  213. <div class="accordion-content">
  214. <div class="accordion-body">
  215. <h3>Key Features</h3>
  216. <ul>
  217. <li>Smooth animations regardless of content height</li>
  218. <li>Pure CSS implementation (minimal JavaScript)</li>
  219. <li>Accessible keyboard navigation</li>
  220. <li>Responsive design</li>
  221. <li>Customizable styling</li>
  222. <li>No external dependencies</li>
  223. </ul>
  224. <p>This component works well for FAQs, navigation menus, content sections, and any interface where you need to show/hide content with smooth animations.</p>
  225. <p>The max-height approach is reliable across all modern browsers and provides consistent performance even with varying content sizes.</p>
  226. </div>
  227. </div>
  228. </div>
  229.  
  230. <div class="accordion-item">
  231. <div class="accordion-header" onclick="toggleAccordion(this)">
  232. <h3 class="accordion-title">Alternative Grid Technique</h3>
  233. <div class="accordion-icon"></div>
  234. </div>
  235. <div class="accordion-content-alt">
  236. <div class="accordion-body">
  237. <h3>CSS Grid Alternative</h3>
  238. <p>Here's an alternative approach using CSS Grid that can be even smoother:</p>
  239. <div class="code-block">
  240. .accordion-content-alt {
  241. display: grid;
  242. grid-template-rows: 0fr;
  243. transition: grid-template-rows 0.4s cubic-bezier(0.4, 0, 0.2, 1);
  244. }
  245.  
  246. .accordion-content-alt.expanded {
  247. grid-template-rows: 1fr;
  248. }
  249. </div>
  250. <p>This method uses CSS Grid's fractional units to create truly dynamic height animations without needing to guess a max-height value.</p>
  251. </div>
  252. </div>
  253. </div>
  254. </div>
  255.  
  256. <script>
  257. function toggleAccordion(header) {
  258. const content = header.nextElementSibling;
  259. const isExpanded = content.classList.contains('expanded');
  260.  
  261. // Close all other accordion items
  262. document.querySelectorAll('.accordion-content, .accordion-content-alt').forEach(item => {
  263. item.classList.remove('expanded');
  264. });
  265.  
  266. document.querySelectorAll('.accordion-header').forEach(item => {
  267. item.classList.remove('active');
  268. });
  269.  
  270. // Toggle current item
  271. if (!isExpanded) {
  272. content.classList.add('expanded');
  273. header.classList.add('active');
  274. }
  275. }
  276.  
  277. // Optional: Add keyboard support
  278. document.addEventListener('keydown', function(e) {
  279. if (e.key === 'Enter' || e.key === ' ') {
  280. if (e.target.classList.contains('accordion-header')) {
  281. e.preventDefault();
  282. toggleAccordion(e.target);
  283. }
  284. }
  285. });
  286.  
  287. // Make headers focusable for accessibility
  288. document.querySelectorAll('.accordion-header').forEach(header => {
  289. header.setAttribute('tabindex', '0');
  290. header.setAttribute('role', 'button');
  291. header.setAttribute('aria-expanded', 'false');
  292. });
  293. </script>
  294. </body>
  295. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement