How to hide and simultaneously show another section (similar to an accordion)

Viewed 45

I need some help creating something similar to an accordion. The goal is that if you click a link in the navigation the one section disappears and the one you clicked appears (at the same position and unnoticeable). If possible it should also automatically scroll to the start of the section(end of the navigation).

Any advice or starting point?

JSFiddle

$(function() {
  var shrinkHeader = 50;
  $(window).scroll(function() {
    var scroll = getCurrentScroll();
    if (scroll >= shrinkHeader) {
      $('#navbar').addClass('shrink');
    } else {
      $('#navbar').removeClass('shrink');
    }
  });

  function getCurrentScroll() {
    return window.pageYOffset || document.documentElement.scrollTop;
  }
});
* {
  margin: 0;
  padding: 0;
  font-family: helvetica;
}

body {
  height: 300vh;
}

h1 {
  text-align: center;
  font-size: 5rem;
  top: 2vh;
  position: relative;
}

h2 {
  text-align: center;
  font-weight: 300;
  margin-top: 3vh;
  font-size: 1rem;
}

ul {
  list-style: none;
  text-align: center;
}

ul li {
  display: inline-table;
  margin-left: 0.25vw;
  padding: 0.25vw;
}

a {
  color: #fff;
  text-transform: uppercase;
  text-decoration: none;
  letter-spacing: 0.15em;
  
  display: inline-block;
  padding: 15px 20px;
  position: relative;
}

#navbar {
  text-align: center;
  width: 100%;
  color: white;
  z-index: 999;
  transition: 0.3s;
}

#navwrapper {
    background: linear-gradient(250deg, #0061ff, #60efff);
    background-size: 400% 400%;
    height: 250px;
}

#navbar.shrink {
  height: 130px;
  line-height: 18px;
  position: fixed;
  background-color: lightgray !important;  
  transition: 0.3s;
}

#navbar.shrink li {
  font-size: 1.2rem;
  margin-top: 1.5vh;
  transition: 0.3s;
}

#navbar.shrink a {
  color: #343a40 !important;
}

#navbar.shrink a:after {    
  background: none repeat scroll 0 0 transparent;
  bottom: 0;
  content: "";
  display: block;
  height: 2px;
  position: absolute;
  background: #343a40 !important;
}

#navbar.shrink h1 {
  font-size: 4rem;
  color: #343a40;
  top: 2vh;
  transition: 0.3s;
}
  <nav id="navwrapper">
    <script class="cssdeck" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
    <script src="javascript/navbar fixed.js"></script>
    <div id="navbar" class="navbar navbar-dark bg-primary navbar-fixed-top">
      <h1 id="titel">HEADLINE</h1>
      <ul>
        <li class="nav-item"><a href="#home" class="nav-link">
            <h2>content1</h2>
          </a></li>
        <li class="nav-item"><a href="#wir-ueber-uns" class="nav-link">
            <h2>content2</h2>
          </a></li>
        <li class="nav-item"><a href="#aktionen" class="nav-link">
            <h2>content3</h2>
          </a></li>
        <li class="nav-item"><a href="#terminvereinbarung" class="nav-link">
            <h2>content4</h2>
          </a></li>
        <li class="nav-item"><a href="#infos" class="nav-link">
            <h2>content5</h2>
          </a></li>
      </ul>
    </div>
  </nav>
  
  <section id="content1">
    <div>
    Lorem ipsum
    </div>
  </section>
  
  <section id="content2">
    <div>
    Lorem ipsum
    </div>
  </section>
  
  <section id="content3">
    <div>
    Lorem ipsum
    </div>
  </section>
  
  <section id="content4">
    <div>
    Lorem ipsum
    </div>
  </section>
  
  <section id="content5">
    <div>
    Lorem ipsum
    </div>
  </section>

1 Answers

Hope this is helps. It took me hours to pull it off. Searching even here at stackoverflow

 <!DOCTYPE html>
 <html>
 <title>Menu animation using javascript</title>
 <meta name="viewport" content="width=device-width, initial-scale=1">

 <style>
 #navi{
     position: absolute;
     top: 40px;
     left: 3%;
     width: 25%;
 }

 .menuItem{
     margin-bottom: 5px;
     width: 100%;
 }

 .menuPage{
     border: 1px solid #000000;
     background-color: #dcdcdc;
     text-align: center;
     padding: 2px 4px;
 }
 </style>

 <body>

 <div id="navi">

     <div id="Ldon" class="menuItem" onclick="openCity('London', 'Ldon')"><p class="menuPage">London</p>
         <div id="London" class="city" style="display:none">
             <h2>London</h2>
             <p>London is the capital city of England.</p>
         </div>
     </div>

     <div id="Pris" class="menuItem" onclick="openCity('Paris', 'Pris')"><p class="menuPage">Paris</p>
         <div id="Paris" class="city" style="display:none">
             <h2>Paris</h2>
             <p>Paris is the capital of France.</p> 
         </div>
     </div>

     <div id="Tkyo" class="menuItem" onclick="openCity('Tokyo', 'Tkyo')"><p class="menuPage">Tokyo</p>
         <div id="Tokyo" class="city" style="display:none">
             <h2>Tokyo</h2>
             <p>Tokyo is the capital of Japan.</p>
         </div>
     </div>

 </div>

 <script>
 function openCity(cityName, newPos) {
 var i;
 var y = document.getElementsByClassName("menuItem");
 var x = document.getElementsByClassName("city");
 var moevePos = document.getElementById(newPos);

 for (i = 0; i < y.length; i++) {
     y[i].style.position = "";  
     y[i].style.top = ""; 
 }

 for (i = 0; i < x.length; i++) {
     x[i].style.display = "none";  
 }

 document.getElementById(cityName).style.display = "block";  
 moevePos.style.position = "absolute";
 moevePos.style.top = "80px";
 }
 </script>

 </body>
 </html>
Related