Advertisement
1xptolevitico69

Awesome Javascript collapsible accordion (no frameworks) pure JS

Jan 6th, 2022
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.60 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.   <title>Awesome Javascript collapsible accordion (no frameworks) pure JS
  6.   </title>
  7.   <meta charset="UTF-8">
  8.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  9.   <style>
  10.     body {
  11.       margin: 0;
  12.       background-color: ivory;
  13.       font-weight: bold;
  14.       font-family: verdana;
  15.     }
  16.  
  17.     .accordion {
  18.       margin: 1px 0;
  19.       background-color: coral;
  20.       cursor: pointer;
  21.       padding: 18px;
  22.       width: 100%;
  23.       border: none;
  24.       text-align: left;
  25.       outline: none;
  26.       font-size: 30px;
  27.       font-weight: bold;
  28.       position: relative;
  29.     }
  30.  
  31.     .accordion:hover {
  32.       background-color: red;
  33.     }
  34.  
  35.     .panel {
  36.       display: none;
  37.       width: 100%;
  38.     }
  39.  
  40.     audio {
  41.       background-color: rgba(128, 128, 128, 0.1);
  42.       position: relative;
  43.       width: 80%;
  44.       left: 10%;
  45.       border: 1px solid red;
  46.       display: block;
  47.       margin: 10px 0;
  48.       height: 30px;
  49.     }
  50.  
  51.     p {
  52.       padding: 10px 20px;
  53.       text-align: justify;
  54.     }
  55.  
  56.     video {
  57.       width: 100%;
  58.     }
  59.  
  60.  
  61.     .subscribe img{
  62.       position: absolute;
  63.       top: 10px;
  64.       right: 10px;
  65.       width: 150px;
  66.       z-index:1;
  67.     }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.   </style>
  74. </head>
  75.  
  76. <body>
  77.  
  78.   <a class='subscribe' href='https://www.youtube.com/channel/UCqLpDK0eOsG1eEeF9jOUZkw' target='_blank'><img src='https://1xpto.netlify.app/pics/clone.png'/></a>
  79.  
  80.  
  81.  
  82.  
  83.   <button class="accordion">Section 1</button>
  84.   <div class='panel'>
  85.     <p>One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin. He lay on his armour-like back, and if he lifted his head a little he could see his brown belly, slightly domed and divided by arches into stiff sections. The bedding was hardly able to cover it and seemed ready to slide off any moment. His many legs, pitifully thin compared with the size of the rest of him, waved about helplessly as he looked. "What's happened to me?" he thought. It wasn't a dream. </p>
  86.   </div>
  87.  
  88.   <button class="accordion">Section 2</button>
  89.   <div class='panel'>
  90.     <video autoplay muted loop>
  91.       <source src="https://1xpto.netlify.app/video/video.mp4" type="video/mp4">
  92.     </video>
  93.   </div>
  94.  
  95.   <button class="accordion">Section 3</button>
  96.   <div class='panel'>
  97.     <audio id='audio' controls>
  98.       <source src="https://1xpto.netlify.app/Audio/bass.mp3" type="audio/mpeg">
  99.     </audio>
  100.     <audio id='audio' controls>
  101.       <source src="https://1xpto.netlify.app/Audio/audio1.mp3" type="audio/mpeg">
  102.     </audio>
  103.   </div>
  104.  
  105.   <script>
  106.     acc = document.getElementsByClassName("accordion");
  107.     panel = document.getElementsByClassName("panel");
  108.     audio = document.getElementsByTagName("audio");
  109.     window.addEventListener("click", function(e) {
  110.       for (i = 0; i < acc.length; i++) {
  111.        if (acc[i] == e.target) {
  112.          panel[i].style.display = 'block';
  113.        } else if (acc[i] != e.target) {
  114.          panel[i].style.display = 'none';
  115.        }
  116.      }
  117.    });
  118.    window.addEventListener("play", function(e) {
  119.      for (i = 0; i < audio.length; i++) {
  120.        if (audio[i] == e.target) {
  121.          audio[i].play();
  122.        } else if (audio != e.target) {
  123.          audio[i].pause();
  124.        }
  125.      }
  126.    }, true);
  127.    document.addEventListener("click", function(e) {
  128.      for (a = 0; a < acc.length; a++) {
  129.        if (acc[a] == e.target) {
  130.          audio[a].pause();
  131.        } else if (acc[a] != e.target) {
  132.          audio[a].pause();
  133.        }
  134.      }
  135.    }, true);
  136.  </script>
  137. </body>
  138.  
  139. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement