Search code examples
javascriptjqueryhtmlcsstoggleclass

ToggleClass to link to individual divs off-canvas?


I have multiple images on a page, and I'd like each image on click to link to a separate page off-canvas. Currently, each image links to the same page, and I'd like to figure out how to hit each one separately. I've simplified the code below, but here's a link to the full Pen: http://codepen.io/anon/pen/KwQYvm?editors=110

<div class="container">

  <section id="slider">
    <div id="section-1" class="fullpage">
      <a href="#slider">
      <h1>Header</h1>
      <a href="#" class="menu-close">⇉</a>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Quo aliquid iste consectetur incidunt dolorem inventore rerum, tempore quis nulla iure eveniet nostrum, saepe itaque mollitia modi ex. Cum, nam odio.</p>
    </div>

    <div id="section-2" class="fullpage">
      <a href="#slider">
      <h1>Header</h1>
      <a href="#" class="menu-close">⇉</a>
      <p>porem ipsum dolor sit amet, consectetur adipisicing elit. Quo aliquid iste consectetur incidunt dolorem inventore rerum, tempore quis nulla iure eveniet nostrum, saepe itaque mollitia modi ex. Cum, nam odio.</p>
    </div>
 </section>

 <ul id="images">
   <li><a href="#section-1" class="menu-link"><img src="https://placekitten.com/g/200/300" alt="" /></a></li>
   <li><a href="#section-2" class="menu-link"><img src="https://placekitten.com/g/200/300" alt="" /></a></li>
 </ul>

</div><!--container-->

The CSS:

.container {
    background: gray;
    -webkit-transition: 1.15s ease;
    -moz-transition: 1.15s ease;
    -o-transition: 1.15s ease;
    transition: 1.15s ease;
}

#slider {
    position: absolute;
    top: 0;
    bottom: 0;
    left: -100%;
    width: 100%;
    height: 100%;

    -webkit-transform: translate(0px, 0px);
    -moz-transform: translate(0px, 0px);
    -o-transform: translate(0px, 0px);
    -ms-transform: translate(0px, 0px);
    transform: translate(0px, 0px);
    -webkit-transition: 1.15s ease;
    -moz-transition: 1.15s ease;
    -o-transition: 1.15s ease;
    transition: 1.15s ease;
}

#slider > div {
    position: absolute;
}

.container.active {
    -webkit-transform: translate(100%, 0);
    -moz-transform: translate(100%, 0);
    -o-transform: translate(100%, 0);
    -ms-transform: translate(100%, 0);
    transform: translate(100%, 0);
}

.fullpage {
    height: 100vh;
}

And the JS:

$('.menu-link').click(function() {
  $('#slider').toggleClass('active');
  $('.container').toggleClass('active');
});

$('.menu-close').click(function() {
  $('#slider').toggleClass('active');
  $('.container').toggleClass('active');
});

Solution

  • What you can do:

    1) hide both sections adding hidden class to each

    .hidden{  visibility:hidden;  }
    ...
    <div id="section-1" class="fullpage hidden">
    <div id="section-2" class="fullpage hidden">
    

    2)When .menu-link is clicked you need to show the corresponding section. Here you can use the fact that your link points to it:

    $('.menu-link').click(function() {
      $('#slider').toggleClass('active');
      $('.container').toggleClass('active');
      $($(this).attr('href')).removeClass('hidden');//here you find the section and make it visible
    });
    

    3) You need to hide it back when menu-close is clicked and for menu-close the target section is its parent,so you can do this:

    $('.menu-close').click(function() {
      $('#slider').toggleClass('active');
      $('.container').toggleClass('active');
      $(this).parent().addClass('hidden');//here you hide the section
    });