Guest User

Code accordion

a guest
Sep 4th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 2.99 KB | None | 0 0
  1. <meta name="viewport" content="width=device-width, initial-scale=1">
  2. <style>
  3. .accordion {
  4. background-color: #ffffff;
  5. color: #444;
  6. cursor: pointer;
  7. padding: 18px;
  8. width: 100%;
  9. border: none;
  10. text-align: left;
  11. outline: none;
  12. font-size: 15px;
  13. transition: 0.7s;
  14. }
  15.  
  16. .active, .accordion:hover {
  17. background-color: #ffffff;
  18. }
  19.  
  20. .accordion svg {
  21.   font-weight: bold;
  22.   float: right;
  23.   margin-left: 5px;
  24.   width: 10px;
  25. }
  26.  
  27. .active svg {
  28.  transform: rotate(180deg);
  29. }
  30.  
  31. .panel {
  32. padding: 0 18px;
  33. background-color: white;
  34. max-height: 0;
  35. overflow: hidden;
  36. transition: max-height 0.8s ease-out;
  37. border-bottom: 3px solid transparent;
  38. }
  39. </style>
  40. <h2>Accordion with symbols</h2>
  41. <p>In this example we have added a "plus" sign to each button. When the user clicks on the button, the "plus" sign is replaced with a "minus" sign.</p>
  42. <button class="accordion">Section 1
  43. <svg aria-hidden="true" focusable="false" role="presentation" class="icon icon-chevron-down" viewBox="0 0 9 9"><path d="M8.542 2.558a.625.625 0 0 1 0 .884l-3.6 3.6a.626.626 0 0 1-.884 0l-3.6-3.6a.625.625 0 1 1 .884-.884L4.5 5.716l3.158-3.158a.625.625 0 0 1 .884 0z" fill="#fff"></path></svg>
  44. </button>
  45. <div class="panel">
  46. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  47. </div>
  48. <button class="accordion">Section 2
  49.  <svg aria-hidden="true" focusable="false" role="presentation" class="icon icon-chevron-down" viewBox="0 0 9 9"><path d="M8.542 2.558a.625.625 0 0 1 0 .884l-3.6 3.6a.626.626 0 0 1-.884 0l-3.6-3.6a.625.625 0 1 1 .884-.884L4.5 5.716l3.158-3.158a.625.625 0 0 1 .884 0z" fill="#fff"></path></svg>
  50. </button>
  51. <div class="panel">
  52. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  53. </div>
  54. <button class="accordion">Section 3
  55.   <svg aria-hidden="true" focusable="false" role="presentation" class="icon icon-chevron-down" viewBox="0 0 9 9"><path d="M8.542 2.558a.625.625 0 0 1 0 .884l-3.6 3.6a.626.626 0 0 1-.884 0l-3.6-3.6a.625.625 0 1 1 .884-.884L4.5 5.716l3.158-3.158a.625.625 0 0 1 .884 0z" fill="#fff"></path></svg>
  56. </button>
  57. <div class="panel">
  58. <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.</p>
  59. </div>
  60. <script>
  61. var acc = document.getElementsByClassName("accordion");
  62. var i;
  63.  
  64. for (i = 0; i < acc.length; i++) {
  65. acc[i].addEventListener("click", function() {
  66. this.classList.toggle("active");
  67. var panel = this.nextElementSibling;
  68. if (panel.style.maxHeight){
  69. panel.style.maxHeight = null;
  70. } else {
  71. panel.style.maxHeight = panel.scrollHeight + "px";
  72. }
  73. });
  74. }
  75. </script>
Advertisement
Add Comment
Please, Sign In to add comment