Advertisement
1xptolevitico69

Javascript accordion + event.target

Jul 17th, 2022
1,227
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 3.56 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.  
  4. <head>
  5.   <link rel="shortcut icon" href="https://1xpto.netlify.app/items/favicon.gif" type="image/x-icon">
  6.   <link rel="name" href="https://www.facebook.com/ManuelJSAndrade/">
  7.   <link rel="name" href="https://www.instagram.com/jolie1xpto/">
  8.   <link rel="tag" href="https://1xpto.netlify.app/articles/html/Javascript accordion + event.target">
  9.   <meta charset="UTF-8">
  10.   <meta http-equiv="X-UA-Compatible" content="IE=edge">
  11.   <meta name="viewport" content="width=device-width, initial-scale=1.0">
  12.   <title>Javascript accordion + event.target</title>
  13.   <style>
  14.     body {
  15.       margin: 0;
  16.       background-color: ivory;
  17.       font-weight: bold;
  18.       font-family: calibri;
  19.     }
  20.  
  21.     .accordion {
  22.       margin: 1px 0;
  23.       background-color: coral;
  24.       cursor: pointer;
  25.       padding: 18px;
  26.       width: 100%;
  27.       border: none;
  28.       outline: none;
  29.       font-size: 30px;
  30.       font-weight: bold;
  31.     }
  32.  
  33.     .accordion:hover {
  34.       background-color: red;
  35.     }
  36.  
  37.     .panel {
  38.       display: none;
  39.       width: 100%;
  40.     }
  41.  
  42.     p {
  43.       padding: 10px 20px;
  44.       text-align: justify;
  45.     }
  46.  
  47.     section {
  48.       width: 50%;
  49.       margin: 20px auto;
  50.     }
  51.  
  52.     @media all and (orientation: portrait) and (max-width: 425px) {
  53.       section {
  54.         width: 100%;
  55.         margin: 0;
  56.       }
  57.     }
  58.   </style>
  59. </head>
  60.  
  61. <body>
  62.   <section>
  63.     <button class="accordion">Kafka</button>
  64.     <div class='panel'>
  65.       <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. 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.</p>
  66.     </div>
  67.  
  68.     <button class="accordion">Cicero</button>
  69.     <div class='panel'>
  70.       <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt.</p>
  71.     </div>
  72.  
  73.     <button class="accordion">Pangram</button>
  74.     <div class='panel'>
  75.       <p>The quick, brown fox jumps over a lazy dog. DJs flock by when MTV ax quiz prog. Junk MTV quiz graced by fox whelps. Bawds jog, flick quartz, vex nymphs. Waltz, bad nymph, for quick jigs vex! Fox nymphs grab quick-jived waltz. Brick quiz whangs jumpy veldt fox. Bright vixens jump; dozy fowl quack.</p>
  76.     </div>
  77.   </section>
  78.  
  79.   <script>
  80.     acc = document.getElementsByClassName("accordion");
  81.     panel = document.getElementsByClassName("panel");
  82.     window.addEventListener("click", function() {
  83.       for (i = 0; i < acc.length; i++) {
  84.        if (acc[i] == event.target) {
  85.          panel[i].style.display = 'block';
  86.        } else {
  87.          panel[i].style.display = 'none';
  88.        }
  89.      }
  90.    });
  91.  </script>
  92. </body>
  93.  
  94. </html>
  95.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement