Accordion - Nascondere automaticamente tab non selezionate

di il
1 risposte

Accordion - Nascondere automaticamente tab non selezionate

Buongiorno a tutti,

come posso modificare il codice nel link sotto per fare in modo che aprendo una Section le eventuali altre aperte si chiudano automaticamente?
https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_accordion_symbol

Grazie

1 Risposte

  • Re: Accordion - Nascondere automaticamente tab non selezionate

    Ciao, ti rendi la vita piu difficile a programmare con vanilla/plain Javascript, ti semplifichi utilizzando Jquery :
    
    <!DOCTYPE html>
    <html>
    <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
    .accordion {
      background-color: #eee;
      color: #444;
      cursor: pointer;
      padding: 18px;
      width: 100%;
      border: none;
      text-align: left;
      outline: none;
      font-size: 15px;
      transition: 0.4s;
    }
    
    .active, .accordion:hover {
      background-color: #ccc;
    }
    
    .accordion:after {
      content: '\002B';
      color: #777;
      font-weight: bold;
      float: right;
      margin-left: 5px;
    }
    
    .active:after {
      content: "\2212";
    }
    
    .panel {
      padding: 0 18px;
      background-color: white;
      max-height: 0;
      overflow: hidden;
      transition: max-height 0.2s ease-out;
    }
    
    .expanded {
    	max-height: 86px;
    }
    </style>
    
    </head>
    
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
    
    <script type="text/javascript">
    $(document).ready(function()
    {
    	var acc = document.getElementsByClassName("accordion");
    	var i;
    
    	for (i = 0; i < acc.length; i++) {
    	  acc[i].addEventListener("click", function() {
    	  
    		$(this).addClass("active");   
    		$(".accordion").not(this).removeClass("active");
    		var currentPanel = $($(this)).next();
    		currentPanel.toggleClass("expanded");
    		$(".panel").not(currentPanel).removeClass("expanded");
    		
    		
    		
    		
    	  });
    	}
    });
    </script>
    
    
    
    <body>
    
      <h2>Accordion with symbols</h2>
      <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>
      <button class="accordion">Section 1</button>
      <div class="panel">
        <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>
      </div>
    
      <button class="accordion">Section 2</button>
      <div class="panel">
        <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>
      </div>
    
      <button class="accordion">Section 3</button>
      <div class="panel">
        <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>
      </div>
    
    </body>
    </html>
    
    
Devi accedere o registrarti per scrivere nel forum
1 risposte